First time making my own Class

I have to make a class called Car with the member variables yearModel make and speed

My constructor has to set speed to 0 and accept the year model and make as arguments. These objects should be assigned to the object's yearModel and make member variables.

Appropriate accessor functions to get the values stored in an object's yearModel make and speed member variables.


I now have it running but getting bad output. My make comes up with a funny symbol and my speed shows up zero... if I comment out the speed = 0; part the speed shows up as a crazy negative number.



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

const int size = 21;

class Car
{
	private:
		int yearModel;
		char make[size];
		int speed;
	public:
		Car(int, int, char[]);
		
		void setyearModel(int);
		void setmake(char []);
		void setspeed(int);
		
		int getyearModel() const;
		char getmake() const;
		int getspeed() const;
};

Car::Car(int yr, int sp, char mk[])
{
	speed = 0;

	yearModel = yr;

	make[size] = mk[size];

}



void Car::setyearModel(int yr)
{
	yearModel = yr;
}

void Car::setmake(char mk[])
{
	make[size] = mk[size];
}

void Car::setspeed(int sp)
{
	speed = sp;
}



int Car::getyearModel() const
{
	return yearModel;
}

char Car::getmake() const
{
	return *make;
}

int Car::getspeed() const
{
	return speed;
}




int main()
{
	
	int carspeed;
	int caryearModel;
	char carmake[size];
	
	
	
	cout << "What make is the car? ";
	cin.getline(carmake, size);
	
	cout << "What year is the car? ";
	cin >> caryearModel;
	cin.ignore();

	cout << "What is the speed of the car? ";
	cin >> carspeed;
		
	Car carInfo(caryearModel, carspeed, carmake);
	
	cout << endl << endl;

	cout << "Make : " << carInfo.getmake() << endl;
	cout << "Year : " << carInfo.getyearModel() << endl;
	cout << "Speed: " << carInfo.getspeed() << endl;

	cout << endl << endl;



system("pause");
return 0;
}


Last edited on
What do you think make[size] = mk[size]; does exactly?
What PanGalactic is saying that your statement:
make[size] = mk[size];
will assign the value stored in the 21th element of "mk" to the 21th element of variable "make". Which will most likely will be a NULL ('\0') or garbage.

Arrays are to be handled differently and most learners make this mistake. Use strcpy(make, mk); function (string.h header). Which copies the elements of "mk" to "make" one by one.

You could also use a loop like this:
1
2
int counter = 0;
while((make[counter] = mk[counter++]) != '\0') ;


An unassigned variable contains garbage so no wonder you got a negative number.

Also instead of doing
speed = 0;
You should be doing
speed = sp;
In the Car constructor (line: 24)

Apart from that I failed to see any other errors. So good luck on improving.
Last edited on
Why not use a string instead of character array? It will make carMake much easier to deal with.
No, what I am saying is that a) that's no way to do string assignment; and b) make[size] = mk[size] will result in two out-of-bounds array accesses. Since speed is declared after the make array, that assignment is almost certainly affecting speed and certainly not affecting make.
@PanGalactic Wait... no way to do a string assignment?

I've been using Java for a long while now so my C++ isn't that sharp (great joke I heard), but I think its highly unlikely that there would be 'no way' to do anything in programming.

Example
1
2
3
4
int myFunction(string p) {

   classString = p;   // This should work fine
}


Maybe I'm wrong?
I think Pan was talking to unoriginal, not you. Using a string would be a lot easier, but Pan was just referring to how he was mishandling arrays.
I think you misinterpreted my meaning. My sentence said "that's [a contraction of 'that is' where 'that' refers to 'make[size] = mk[size]'] no way to do string assignment."
@Zhuge: yeah -- thanks. :-)
My mistake. Completely valid arguement.
Topic archived. No new replies allowed.