Need help with simple code question PLEASE

Pages: 12
Hi, I have a mid-term for my intro to programming course this thurs and was given an example of what to expect on the test. I needed help on the following problem. I have wrote what I did but I don't know what to do next. Thanks.

Write a complete class Vehicle containing private data members x, y, where x is an array of integers and y is a static double. Write a member function that displays the values of x and y on the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

class Vehicle
{
private:
	int x[];
	static double y;
};

int main()
{
	cout << x[] << y;
	return 0;
}
Last edited on
Where is your member function?
This is all I wrote, cause I'm really confused on what to do next. How would I write the member function? I need it to display the values of x and y on the screen. Also, to point out, this is not any assignment question or anything that is getting marked, it is just practice which is similar to what could be on the test. I'm assuming the code should be simple cause he is gonna make us hand write the code.
Last edited on
Member functions are like normal functions, except they are scoped within the class. You can define it inline and make it really simple:
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Vehicle
{
    void display() //member function
    {
        for(int v : x) //C++11 range-based for-loop
        {
            std::cout << v << " ";
        }
        std::cout << std::endl << y << std::endl;
    }

private:
	int x[10]; //you have to put a size here
	static double y;
};
Your int x[] is private, so you can't access it directly through main(). Without doing your homework for you, this should point you in the right direction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

class Vehicle
{
private:
	int x[2];
	static double y;

public:
	Vehicle() { 
		x[0] = 32;
		x[1] = 14;
	}

	void display() {
		std::cout << x[0] << std::endl;
	}
};



int main()
{
	Vehicle truck;

	//Display 32 (value of x[0]) to console window
	truck.display();

	return 0;
}


Last edited on
@HellfireXP, did you even read the first post at all?
Thanks for ur solutions but I don't think that I have gone as far as to learn in line functions yet. Would the following work, how can I fix it? I no it doesn't work but how can I correct it. Thanks.

#include <iostream>
using namespace std;

class Vehicle
{
private:
int x[];
static double y;
public:
void output ();
};

void Vehicle::output ()
{
cout << x [ ] << y;
}

You should review array syntax. Both the way you declare and use x are invalid.
I did but I will again later. Can u correct what I'm doing wrong tho. I need help displaying the array values and the y value as it says in the question. Thanks.
1. Your array needs to have an actual size, you can't just write [] with nothing between the square brackets.
2. You need to loop through the array.

I already showed you how to do both of those things.
Yea, thanks for that, but I haven't learned about inline yet.
Maybe this will give you an idea on how to get to / manipulate and display your data.
It's not the greatest or the prettiest... but it works...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

class Vehicle{
private:
int pos;
int x[5];      // how ever big you need it.
const static double y=3.14;    // read only, doesn't change. You may assign it here

public:
double getY(){return y;}                 // returns value of y
void setPOS(int POS){pos=POS;}      // place value into pos
int  getPOS(){return pos;}                 // returns value of pos
void setX(int POS, int _X){pos=POS; x[pos] = _X;}  // place position and value into x
int getX(int pos){return x[pos];}             // returns value of x
};

int main(){
    Vehicle truck;                   // declare class ; object truck

     for (int position=0;position<4;position++){
        truck.setX(position , 25);
     }
    truck.setX ( 3 , 50 );         // at element 3, place value of 50

for (int i=0; i<4;  i++){
        cout << "\nThe value of x[" << i <<"] = " << truck.getX(i) << endl;
}
       cout << "\nThe value of y is "<< truck.getY() << endl;

cout << "\nI sold my truck for " << truck.getX(truck.getPOS()) << " dollars and made a profit of " << truck.getY() <<"."<< endl;
//                                                   get values                                                                               get value
return 0;
}
Last edited on
Yea thats pretty scary. lol. Could u tell me what im doing wrong in the following? Even if i assign a number like 5 to const int arrayLength, it doesnt work. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

const int arrayLength;

class Vehicle
{
private:
	int x[];
	static double y;

public:
	void output();
};


void Vehicle::output()
{
	for (int i = 0; i < arrayLength; i++)
	{
		cout << x[i] << endl;
	}
	cout << y << endl;
}
Last edited on
Line 9: How big do you think your array is?
Last edited on
Like LB Has said before, you can't do this int x[];

You need to specifiy how big the array is. For example int x[5];
Yea, but even after specifying it to int x[5] ; while also doing const int arrayLength = 5; , it still doesnt work, it gives errors still.
Don't make it const.

Look at my code again... You have a complete class, you have a print to screen function, you have examples of set and get function... I don't know what else I can say brother...
Last edited on
LB, yes I read it, thanks. Did you? He's in a beginner programming course, he does not need a complicated answer. As the series of posts that have transpired since prove much of what you and pearlyman post are over his head. KISS (Keep it simple stupid) is often the best approach for new programmers rather than just trying to show off what you know.

Also, since he's in school, if you can give him a little bit of code that helps demonstrate the areas he doesn't understand, this will go alot further than just writing his code entirely. You know, teach a man to fish... and all that stuff...
Last edited on
Well hell, never let it be said I kept the man from fishing. I don't like to see anyone go hungry.... No sir... we'll have you fattened up in no time. Let's take another look at that code I gave you.

#include <iostream> /* Lines beginning with a hash sign (#) are directives read and interpreted by what is known
as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In
this case, the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code,
known as header iostream, that allows to perform standard input and output operations, such as writing the output
of this program to the screen.*/

using namespace std; /*A namespace is a declarative region that provides a scope to the identifiers (the names
of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to
prevent name collisions that can occur. */

class Vehicle{ // This line declares our class Vehicle and it has an opening bracket

private: // This is where you put your members. Variables only accessible by the class Vehicle

int pos; /* position of the element (x[0] [1] [2] [3] [4]
This variable is used to keep track of which element you are using in the array */

int x[5]; /* I pulled the number 5 out of thin air. I figured for this example I would just use 5 elements for
the array x*/

const static double y=3.14; /* Required by this assignment. Const(ant) means it doesn't change, thus you can't
write a mutate method for it. It will always be 3.14 */


public: /* This is where you put the methods that you can use in your program to access and change the private members.*/

double getY(){return y;} /* This will return the value 3.14 whenever you use truck.getY(); It returns 3.14 because
it's constant. It never changes.*/

void setPOS(int POS){pos=POS;} /* You can use truck.setPOS(#) to pick the element you want to work with.
example truck.setPOS(3) will choose element #3. Remember, elements in arrays start at zero, not at one...
So your array is actually x[0], x[1], x[2], x[3], x[4] instead of x[1], x[2], x[3], x[4], x[5].*/


int getPOS(){return pos;} /* When you use truck.getPOS(#) You get the value assigned to 'pos' which we are using to keep track of which element we are working with. POS is for
position */

void setX(int POS, int _X){pos=POS; x[pos] = _X;} /* You can use this method to place a value into the array x in the element of your choice. For example truck.setX(2,10); would place the value of 10 onto the 2nd element of array x. */

int getX(int pos){return x[pos];} /* This little gem lets you get the value from array x[#]. For example
int b=truck.getX(4) will take the value from X[4] and assign it to b. You can use it in a cout statement as well.
cout << truck.getX(1) will print the value of X[1].*/

}; // This ends the Class declaration - Now for the fun stuff

int main(){ // our main function, where the program actually starts with an opening bracket

Vehicle truck; /* We make an object called truck to represent our class. We refer to this as calling an instance
of the class. You can make as many of these as you want, truck, car, bike, boat. Each of them will have
their own set variables, just like truck. They have the same array, y will still be 3.14. You access them but
the object name, a dot, and the variable method (or function).

truck.getPOS[3] will be different from car.getPOS[3], but you can access them in the same way. */

for (int position=0;position<4;position++){ /* a for loop. We start off at 0 because that's where the array
element begins. It will count to 4, 0-4 = 5 elements the size of the array x. */

truck.setX(position , 25); /* Here we invoke the object.setX method. With each loop, the variable position
changes the element (x[0], x[1],x[3] and so on, and gives that element the value 25 */
} // This ends the loop
truck.setX ( 3 , 50 ); // now, just to make one different, I change this element to 50.

//Here is your function to print the values of Class vehicle (object truck) to the screen.

for (int i=0; i<4; i++){ // Another for loop

cout << "\nThe value of x[" << i <<"] = " << truck.getX(i) << endl; /* This will print
The value of x[0] = 25 through The value of x[4] = 25 (Except x[3] that I changed to 50.) */

} // This ends the loop

cout << "\nThe value of y is "<< truck.getY() << endl; /* This prints
The value of y is 3.14 to the screen. We know this, because y is constant, and can not change. */

cout << "\nI sold my truck for " << truck.getX(truck.getPOS()) << " dollars and made a profit of " << truck.getY() <<"."<< endl;
/* I threw this in for gooses and giggles. When last we left setPOS, it was on #3.. so truck.getX(truck.getPOS()) will return
that value. Which is 50. We know y is 3.14, and thus we are able to print,
I sold my truck for 50 dollars and made a profit of 3.14. */

return 0; // This ends the C++ program
} // This is the closing bracket to main

Now... that's not so scary is it? RAVSHAN02 I didn't make this post to embarrass you, no one thinks you're stupid. I understand it's hard to learn a new language. I get aggravated mostly by people who don't search for their answers. Searching and writing code is the best way to learn c++. 95% of the time, you can go to google type in c++ and the error code, or the command, and get a treasure trove of knowledge. I myself am a beginner programmer. You probably know more crap than I do. I'm self taught, and I see it like this. If I can teach this stuff to myself, anyone in school that has it explained to them should be able to ace it. Maybe I'm wrong. But I'm certain 75% of the questions here could be resolved with a simple google search.
Last edited on
RAVSHAN02 wrote:
Write a complete class Vehicle containing private data members x, y, where x is an array of integers and y is a static double. Write a member function that displays the values of x and y on the screen.
HellfireXP wrote:
Your int x[] is private, so you can't access it directly through main().
LB wrote:
@HellfireXP, did you even read the first post at all?
HellfireXP wrote:
LB, yes I read it, thanks. Did you?
I did. The part I quoted and bolded above is the part believe you have ignored.
HellfireXP wrote:
He's in a beginner programming course, he does not need a complicated answer. As the series of posts that have transpired since prove much of what you and pearlyman post are over his head. KISS (Keep it simple stupid) is often the best approach for new programmers rather than just trying to show off what you know.

Also, since he's in school, if you can give him a little bit of code that helps demonstrate the areas he doesn't understand, this will go alot further than just writing his code entirely. You know, teach a man to fish... and all that stuff...
I gave him the simplest correct answer and explained it with comments. I can't get any simpler than that.
Pages: 12