Updating Function parameter. (Classes)

I have to demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times.

My question is how do I get the parameter for accelerate to update to the new speed after the function is called once.

Here is my code.

I would also appreciate if you could look through my class and point out any issues or recommend any best practices.

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
#include <iostream>
#include <string>
using namespace std;

class Car{
    private:
    int year;
    string make;
    int speed;
    public:
    Car(int year, string make){
        speed=0;
        year;
        make;
    }
    int Getyear(){
        return year;
    }
    string Getmake(){
        return make;
    }
    int Getspeed(){
        return speed;
    }
    void accelerate(int speed){
        speed+= 5;
    }
    void brake(int speed){
        speed-=5;
    }
};
int main() {
    
	Car newcar(2015, "Honda");
	for (int i=0; i < 5; i++){
	    accelerate(0);
	}
	cout << Getspeed();
	
	return 0;
}
Here are some changes I would recommend.

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

class Car {
private:
    int year;
    std::string make;
    int speed;
public:

    // http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor
    /*
     * Initialization lists are better in terms of efficiency.
     */
    Car(int year, const std::string &make) : year (year), make (make), speed (0) { }

//    Car(int year, string make) {
//        speed = 0;
//        year;
//        make;
//    }
//    int Getyear() {
//        return year;
//    }
//
//    string Getmake() {
//        return make;
//    }
//
//    int Getspeed() {
//        return speed;
//    }


    /*
     *  Use const for your getters returns
     *  Best practice as a getter should never change a value.
     *  and see note below about Car:: or this
     */
    int get_year() const {
        return year;
    }

    void set_year(int year) {
        Car::year = year;
    }

    const std::string &get_make() const {
        return make;
    }

    void set_make(const std::string &make) {
        Car::make = make;
    }

    int get_speed() const {
        return speed;
    }

    void set_speed(int speed) {
        Car::speed = speed;
    }

/*
     * if your parameter and member variable are the same name
     * you need to tell the compiler what variable is in the class
     * and what variable is being passed in.
     * either way works. See below.
     */
    void accelerate(int speed) {
        //speed += 5;
        this->speed += speed;
    }

    void brake(int speed) {
        //  speed -= 5;
        Car::speed -= speed;
    }
};

/*
 *  use namespace std in main
 *  but do not use it in your class.
 */
using namespace std;

int main() {
    Car newcar (2015, "Honda");
    for (int i = 0; i < 5; i++) {

        /*
         * You need to refer to the object
         * then get you have access to the methods.
         * see below.
         */
        newcar.accelerate (i);
        // accelerate (0);
    }
    cout << newcar.get_speed ();
    //cout << Getspeed ();
    return 0;
}
I'd actually recommend that you strive to use different names for your class member parameters.

Perhaps something like:
1
2
3
    void accelerate(int car_speed) {
        speed += car_speed;
    }

Or if you do have clashing parameter and member variable names use the this pointer instead of the Car::.
1
2
3
    void accelerate(int speed) {
        this->speed += speed;
    }
Last edited on
@Bdanielz
Thanks for introducing me to a new concept! Although I'm kind of confused on why you using const std:: string and the & as part of the argument.
 
Car(int year, const std::string &make)


Is it because we are not using the string library?

What is the purpose of adding the new functions? Are we trying to access the private member functions with this?

Also why is it bad practices to use namespace std in your classes?


@jllb

Thanks for the tips! I'll be sure to incorporate them into my programming.

Although I'm kind of confused on why you using const std:: string and the & as part of the argument.
Car(int year, const std::string &make)


For two reasons.
1. It is best to use const whenever or wherever possible: I was told this once, did not practice it, ran into a problem in a larger project that I could not solve. E.g A program I made was giving me unexpected results. Went over the entire program line by line and I did not see any visible errors. I spent hours and hours troubleshooting / testing then to only find out I needed to include a const on a few functions.

2. if you look at my example program, you can see the how it allow the function to take just a quoted string and a var, where the one without const only allows variables. I like this because I use functions this way to make my prompts. So I can do things like string input = prompt ("Please enter text"); then if I need another prompt I just have to change one piece of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void print_A(const std::string &input) {
    std::cout << input << std::endl;
}

void print_B(std::string &input) {
    std::cout << input << std::endl;
}

int main() {
    std::string var = "test";
    print_A ("test");
    print_A (var);

    //Error-> print_B("test"); 
    print_B (var);

}


What is the purpose of adding the new functions? Are we trying to access the private member functions with this?


Yes. I just made one for each of your class members. Most of the time you just need getters, because all your operations can happen privately and the data is set with your constructor or initialization list.

Also why is it bad practices to use namespace std in your classes?

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice

I use namespace::std all the time, when I am drafting code. But I generally avoid using in my classes (due to having an issue in the past) and just in good practice.
Last edited on
Thanks for all the help! You guys!
Topic archived. No new replies allowed.