#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.
*/
usingnamespace 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;
}
@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?
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.
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?
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.