First time with classes

Pages: 12
http://prntscr.com/bzkd40

I'm starting on this program, but I'm getting an error. These are the only libraries I'm supposed to use.

Didn't the constructor instructions say to assign yearModel to yearModel and make to make?

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

using namespace std;

class Car
{
private:
    //private fields to hold data
    int yearModel ;
    string make;
    int speed;

    //constructor
public:

    Car(int ym, string mk, int speed)
    {
        yearModel=ym;
        make=mk;
        speed=0;
    }

};
Yes, but they also didn't specify that your constructor is supposed to take a third argument called "speed" which shadows the member variable of the same name.

-Albatross
When I changed it to spd I still got an error. (I changed the speed argument to spd).

http://prntscr.com/bzkg8s
Last edited on
More to the point, your constructor isn't supposed to take a third argument at all. However... that error seems completely unrelated to the code shown.

Did you write a main function? Additionally, did you start a Windows console project by accident?

-Albatross

Full confession, I only just read the actual problem prompt rather than skimming over it. Is that a Java textbook, or an object-oriented programming textbook that assumes Java use?
It's a C++ book. : )

I think I know what the issue is.

I made two projects in code blocks.

I moved all the files in one file out of the file and into the other file so all the files would be in one folder.

See, I have to make a program and a class, but I have to put them in the same folder, but they have to be separate files.

I'm having a hard time doing this.

I fixed it, though, so let me see if I get the same error.

It fixed it! Alright, let me see if I need help with anything else.
Last edited on
Don't know why I'm getting errors.

My professor told us to make our program by following this, so I'm not sure what's going on.
http://prntscr.com/bzkx1a
http://prntscr.com/bzkx4p

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

using namespace std;

class Car
{
private:
    //private fields to hold data
    int yearModel ;
    string make;
    int speed;

    //constructor
public:

    Car(int ym, string mk)
    {
        yearModel=ym;
        make=mk;
        speed=0;
    }

void setYearModel(int ym)
{
    yearModel=ym;
}

void setMake (string mk)
{
    make=mk;
}

int setYearModel()
{
    return yearModel;
}

string make()
{
    return make;
}
};
There's no way that's a C++ book. "Field" and "method" are not C++ terms, and Integer and String are names of Java classes where there are no identically named classes in the C++ standard library.

That said, you have a member function on line 39 with the same name as the member object on line 11. I'd also strongly advise renaming setYearModel (the version on line 34, that is).

-Albatross

More fine print, but the C++ version of the CellPhone class raised a yellow flag for me. Was this something written by your professor? Going with a theme here, it looks like it was written by someone with a background in Java trying to write code in C++, and I say that because it has some minor issues.
Well, that's what we were told, and the language champion in the screenshot I just showed you teaches us in C++. The book I use is programming logic and design by Tony Gaddis.

No, this was not something written by him. : )

Fixed all the errors. Renaming really helped. The class at times teaches weird, but oh well. That's his policies. I'm curious, why does it look like java to you?
closed account (E0p9LyTq)
Real simple mistake at line 39, equally easy to fix:

1
2
3
4
string make()
{
    return make;
}


could be:

1
2
3
4
string getMake() const
{
    return make;
}


You've overloaded setYearModel(), once as a setter, once as a getter.
Last edited on
Well, were this written by someone familiar with C++ and willing to teach proper methodologies, the member functions that return something without modifying it would be marked as const. That might seem subtle, but the moment you try to use a const instance of the class, you won't be able to use its member functions, even those that don't modify the object's data. That's a bit silly.

Another issue (though this one is a comparatively minor until your objects start getting huge) is that setMake (and your constructor) take std::strings by value. In Java, all the objects get passed around by reference by default. This is not the case in C++, and the std::strings passed by value get copied for no good reason.

The C++ idiom for taking a object in a function without copying the whole object looks a little something like const type& name. With this, setMake would end up looking like void setMake(const string& mk). Note that there are cases where you would actually want to make a copy of a object, and that for your primitive types (int, float, pointers) there's no real point to doing this (which is why I didn't use setYearModel as an example).

-Albatross
Last edited on
closed account (E0p9LyTq)
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
#include <iostream>
#include <string>

class Car
{
public:
   Car(int ym, std::string mk)
   {
      std::cout << "Creating your car.....\n\n";
      yearModel = ym;
      make = mk;
      speed = 0;
   }
   ~Car() { std::cout << "\nSending your car to the junk yard....\n"; }

public:
   int getYearModel() const     { return yearModel; }
   void setYearModel(int ym)    { yearModel = ym; }

   std::string getMake() const  { return make; }
   void setMake(std::string mk) { make = mk; }

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

private:
   int yearModel;
   std::string make;
   int speed;

};


int main()
{
   Car myCar(2011, "Jeep Patriot");

   std::cout << "Your car is a " << myCar.getYearModel() << " " << myCar.getMake() << ".\n";
}


Creating your car.....

Your car is a 2011 Jeep Patriot.

Sending your car to the junk yard....
Finished my class.

http://prntscr.com/bzncoz

This is what my class methods are supposed to look like (It's a UML diagram), and I think I did what I was supposed to with my own names.





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

using namespace std;

class Car
{
private:
    //private fields to hold data
    int yearModel ;
    string make;
    int speed;

    //constructor
public:

    Car(int ym, string mk)
    {
        yearModel=ym;
        make=mk;
        speed=0;
    }

void setYearModel(int ym)
{
    yearModel=ym;
}

void setMake (string mk)
{
    make=mk;
}

int returnYearModel()
{
    return yearModel;
}

string returnMake()
{
    return make;
}

void accelerate(int speed)
{
    speed=speed+5;
}

void brake(int speed)
{
    speed=speed-5;
}
};
closed account (E0p9LyTq)
You do not need to pass speed, a class data member, into a class method:
1
2
3
4
5
6
7
8
9
void accelerate()
{
    speed += 5;
}

void brake()
{
    speed -= 5;
}


If you wanted to have a variable speed change you could overload accelerate() and brake() to accept a different value:
1
2
3
4
5
6
7
8
9
void accelerate(int value)
{
    speed += value;
}

void brake(int value)
{
    speed -= value;
}


Your class as currently defined needs a way to access the current speed, a getter:
1
2
3
4
int returnSpeed() const
{
   return speed;
}


You really should declare all your getter methods as const, as I did above, so if you should accidentally make changes to the data member the method is flagged with an error. That helps trouble-shooting.
I would, but my professor won't allow it. He strictly wants it to be like the example I showed you guys.

I fixed everything you told me to.

Now I move on to making the program.

Though, how do I get the code from the class to the other file with only an iostream library (string may be included of course)?

As I've showed, my professor recommends us to have a file for class and a file for the program we make.

http://prntscr.com/bznlov

And in my book it says we have to call the brake and accelerator methods

http://prntscr.com/bznlyw

but since I have two files I have no idea how to get the class methods to my other program

Any ideas? We never learnt it in our school.

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

using namespace std;

class Car
{
private:
    //private fields to hold data
    int yearModel ;
    string make;
    int speed;

    //constructor
public:

    Car(int ym, string mk)
    {
        yearModel=ym;
        make=mk;
        speed=0;
    }

void setYearModel(int ym)
{
    yearModel=ym;
}

void setMake (string mk)
{
    make=mk;
}

int returnYearModel()
{
    return yearModel;
}

string returnMake()
{
    return make;
}
int returnSpeed()
{
   return speed;
}

void accelerate()
{
    speed += 5;
}

void brake()
{
    speed -= 5;
}
};




closed account (E0p9LyTq)
car.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Car
{
private:
    //private fields to hold data
    int yearModel ;
    string make;
    int speed;

    //constructor
public:
    Car(int ym, string mk);

void setYearModel(int ym);
void setMake (string mk);
int returnYearModel();
string returnMake();
int returnSpeed();
void accelerate();
void brake();
};


car.cpp:
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
#include <string>
#include "car.h"

Car::Car(int ym, string mk)
{
    yearModel=ym;
    make=mk;
    speed=0;
}

void Car::setYearModel(int ym)
{
    yearModel=ym;
}

void Car::setMake (string mk)
{
    make=mk;
}

int Car::returnYearModel()
{
    return yearModel;
}

string Car::returnMake()
{
    return make;
}

int Car::returnSpeed()
{
   return speed;
}

void Car::accelerate()
{
    speed += 5;
}

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


your main source:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include "car.h"

int main()
{
   Car myCar(2011, "Jeep Patriot");

   // the rest of the code to use your class
}
I apologize, but can you explain what's happening? Not quite clicking.

All I got out of this is we included the cpp name to use, but I'm not sure what you're doing.
Last edited on
closed account (E0p9LyTq)
What am I doing?

As I've showed, my professor recommends us to have a file for class and a file for the program we make.
Alright, let me explain, ha.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Car
{
private:
    //private fields to hold data
    int yearModel ;
    string make;
    int speed;

    //constructor
public:
    Car(int ym, string mk);

void setYearModel(int ym);
void setMake (string mk);
int returnYearModel();
string returnMake();
int returnSpeed();
void accelerate();
void brake();
};


I'm not sure how this piece of code is relevant or what you're doing here.

I understand car.h. How did you find car.h? How did you get the name? Just wondering for future reference in other programs.

1
2
3
4
5
6
int main()
{
   Car myCar(2011, "Jeep Patriot");

   // the rest of the code to use your class
}


Not quite understanding this part, but I do see how we took the code from the other program and translated it here.


: )
Last edited on
closed account (E0p9LyTq)
I split creating your class into a class declaration (car.h) and a class definition (car.cpp).

I include "car.h" so your main source knows what your Car class is.

Convention is to name your class files the same as the class. Your class is called "Car," so "car.h" and "car.cpp."

You include standard library header files in your source, <iostream> for example, you can include your custom header files as well. Just use quotes "" instead of less-than/greater-than.

In main() I am simply constructing an instance of your Car class, as specified in your class declaration/definition. main() knows about your Car class because the class header file was included.
Pages: 12