Car Class (OOP)

Does anyone know why I keep getting logical errors in my code?

Car.h
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
#ifndef CAR_H
#define CAR_H
#include <iostream>
using namespace std;
class Car
{
private:
	int yearModel;
	string make;
	int speed;
public:
	Car(int y, string m)
	{
		int s = 0;
		y = yearModel;
		m = make;

		s = speed;
	}

	void accelerate()
	{
		speed += 5;
	}

	void brake()
	{
		speed -= 5;
	}

	int getyearModel() const
	{
		return yearModel;
	}
	
	string getMake() const
	{
		return make;
	}

	int getSpeed() const
	{
		return speed;
	}

};
#endif



Car.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Car.h"
#include <memory>
#include <iostream>
using namespace std;

int main()
{
	Car sedan(2012,"Toyota");
	sedan.accelerate();
	sedan.accelerate();
	sedan.accelerate();
	sedan.brake();
	
	printf("Car Information\n");
	cout << string(10, '-') << endl;
	cout << "Maker: " << sedan.getMake() << endl;
	cout << "Year: " << sedan.getyearModel() << endl;
	cout << "Speed: " << sedan.getSpeed() << endl;
}
]
Hello agirideep,

The problem is in line 50.

If that does not work explain your problem better. I have no idea where to start looking.

Why are you using "printf" in a C++ program?

Lines 15 - 18 you do not need a "cout" and "endl" for each line. That is what the insertion operator (>>) is for.

Andy
Lines 14 through 18 assign local variables instead of class member data.
Hello agirideep,

Expanding on what mbozzi said your overloaded ctor should look line this:
1
2
3
4
5
6
7
8
Car(int y, string m)
{
    yearModel = y;

    make = m;

    speed = 0;
}


Assignment is from right to left. And you do not have to define a local variable "s" just to set your speed.

The other way to write your ctor is with the initialization list
 
Car(int y, string m) : yearModle(y), make(m), speed(0) {}

This does the same as what you wrote, but with 1 line.

As I mentioned earlier in "main" the "cout" can be written as:
14
15
16
17
18
19
std::cout
    << "Car Information\n"
    << string(15, '-')  // <--- Changed to 15.
    << "\nMaker: " << sedan.getMake()
    << "\nYear: " << sedan.getyearModel()
    << "\nSpeed: " << sedan.getSpeed() << '\n';

Notice there is no need for the odd "printf"

Last point: never put using namespace std; in a header file. See http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ It is best not to use this line at all anywhere.

But if you feel that you need to try this:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

//#include <memory>  // <--- Not used or needed.

using namespace std;

#include "Car.hpp"

int main()


Andy
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
#ifndef CAR_H
#define CAR_H
#include <iostream>

using namespace std;
class Car
{
private:
    int yearModel;
    string make;
    int speed{0}; // <--
public:
    Car(int y, string m)
    {
        y = yearModel;
        m = make;
    }

Car(int y, string m, int spd) // Another way
    {
        y = yearModel;
        m = make;
        setSpeed(spd); // <-- or, speed = spd;
    }

    void accelerate()
    {
        speed += 5;
    }

    void brake()
    {
        speed -= 5;
    }

    int getyearModel() const
    {
        return yearModel;
    }
    
    string getMake() const
    {
        return make;
    }

    int getSpeed() const
    {
        return speed;
    }
    
    void setSpeed(int spd) // <--
    {
        speed = spd;
    }

};
#endif

int main()
{
    Car sedan(2012,"Toyota");
    
    cout
    << "Car Information\n"
    << string(10, '-') << '\n'
    << "Maker: " << sedan.getMake() << '\n'
    << "Year: " << sedan.getyearModel() << '\n'
    << "Speed: " << sedan.getSpeed() << '\n';
    
    sedan.accelerate();
    cout << "Speed: " << sedan.getSpeed() << '\n';
    
    sedan.accelerate();
    cout << "Speed: " << sedan.getSpeed() << '\n';
    
    sedan.accelerate();
    cout << "Speed: " << sedan.getSpeed() << '\n';
    
    sedan.brake();
    cout << "Speed: " << sedan.getSpeed() << '\n';
}


That is what the insertion operator (>>) is for


<< is the insertion operator.
Perhaps:

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>
#include <string>

class Car {
	int yearModel {};
	std::string make;
	int speed {};

public:
	Car(int y, const std::string& m) : yearModel(y), make(m) {}

	void accelerate() { speed += 5; }
	void brake() { 	speed -= 5; }
	int getyearModel() const { return yearModel; }
	std::string getMake() const { return make; }
	int getSpeed() const { return speed; }
};

int main()
{
	Car sedan(2012, "Toyota");

	sedan.accelerate();
	sedan.accelerate();
	sedan.accelerate();
	sedan.brake();

	const auto sze {printf("Car Information\n")};

	std::cout << std::string(sze - 1, '-') << '\n' <<
		"Maker: " << sedan.getMake() << '\n' <<
		"Year:  " << sedan.getyearModel() << '\n' <<
		"Speed: " << sedan.getSpeed() << '\n';
}

Last edited on
Topic archived. No new replies allowed.