Exercise question

Pages: 12
Thank you for the explaination, I understand that.

What if I have this?

1
2
3
4
5
6
7
8
9
10
11
12
class car
{
private:
float number;
};


class bmw : public car
{
private:
float number;
};


Now both classes have the same member. It compiles fine, but what will happen when you use this?

Also, I'm still stuck with the exercise. How can I solve this problem?
I think what you want to do is have a set of options the user can pick. They choose, for example "Add car to database". Read up on linked lists...
http://richardbowles.tripod.com/cpp/linklist/linklist.htm
Make the float in the car protected and not private. That way bmw will inherit it.

http://cplusplus.com/doc/tutorial/inheritance/
http://cplusplus.com/doc/tutorial/polymorphism/



You will notice that your code wont compile any more (it shouldnt) because you allready have a number in bmw - you'll have to delete the one you declare in that class.
Last edited on
I should be able to do it with things I've learnt so far :(

I need to work with members from a child class, while working on the main class. If you can use methods from the child class with polymorphism, but can't we work with members on the same way? I just want to use the member "number", which is a member of bmw, which is a childclass of car, while working with cars.

There must be something I'm understranding wrong? :(

EDIT:
Gregor, when we have this:
1
2
3
4
5
6
7
8
9
10
11
12
class car
{
protected:
float number;
};


class bmw : public car
{
protected:
float number;
};


What happens now?
Last edited on
Read the tutorials. It wont take you long and it explains it great. Than, if you have any questions, I'll be back in a few hours.

EDIT:
Same thing. You allready have a number in bmw.
Last edited on
So why does it compile without errors here? You said it won't compile because you have a already a number in bmw, but it compiles fine here?

I did read all the tutorials already, on this site and others. But I can't find the answer on how I can get my program to work.
I'll post the code when I get back, THINK!
I won't be online anymore when you get back, so my reply will be tomorrow :)

Oh, and I'm thinking. This just doesn't make sense for me anymore :(
Based on your first post:

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
#include <iostream>
#include <vector>
using std::cout;
using std::vector;

class Car{
protected:
  int max_speed,
       horsepower,
       year;
public:
  virtual void GetInfo(){
   cout<<"Car with "<<max_speed<<" and "<<horsepower<<" made in "<<year<<std::endl;
  }
  
  Car(int ms, int hp, int y){
          max_speed=ms;
          horsepower=hp;
          year=y;
  }
};

class Bmw: public Car{
  float number;//Altho I dunno what this is for, I have made it BMW specific
public:
 void GetInfo(){
   cout<<"BMW with "<<number<<" and "<<max_speed<<" and "<<horsepower<<" made in "<<year<<std::endl;
  }
  
  Bmw(int ms, int hp, int y, float n):Car(ms, hp, y){
          number=n ;    
  }
};

class Mercedez: public Car{
  int rear_lights;//Mercedez specific variable
public:
 void GetInfo(){
   cout<<"Mercedez with "<<rear_lights<<" rear lights and "<<max_speed<<" and "<<horsepower<<" made in "<<year<<std::endl;
  }
  
  Mercedez(int ms, int hp, int y, int rl): Car(ms, hp, y){
               rear_lights=rl;             
  }
};

int main(){
  vector<Car*> database;
  //Create instances
  database.push_back(new Car(1,2,3));
  database.push_back(new Bmw(4,5,6,7.8));
  database.push_back(new Mercedez(9,10,11,12));

  for(int i=0; i<database.size(); i++){//Call the function and delete car
     database[i]->GetInfo();
     delete database[i];
  }
  std::cin.get();
  return 0;
}


Note that all std functions with the exception of std::wstringstream are correct. I never used stringstream so I just made things up as I moved along.
Last edited on
I've tried to run that piece of code, but it's full of errors.

Anyway, assuming it works, what will the output be? Won't it output 3 times this:
s<<L"Car with "<<max_speed<<" and "<<horsepower<<" made in "<<year;

Because you didn't use polymorphism?

Also, will your GetInfo() function work for the Mercedez and Bmw class? Because, just like I tried, you're trying to access rear_lights and number, while they are not part of car, only of the childclasses.

I'm confused with this lol.
That's the beauty of polymorphism. C++ will call function of the child car you have in memory. It will call their version of GetInfo().

I wrote that code on the spot, never run it. I'll compile it for ya and post the edited one so you will see for yourself.
But I thought you would need the keyword "virtual" to let it use their childclasses :S

Ok, I'll just wait the rechecked code so I can have a look and mess with it myself :)
yh you do:P If you don't write virtual function from parent will be called. I apologise for the haslty written code, hope I didn't confuse you too much.
Hi,

Your code works now for some reason I don't understand.

But let's say I want to edit the variable (let's assume we made it public instead of private). So the new code will be this:
1
2
3
4
database.push_back(new Car(1,2,3));
  database.push_back(new Bmw(4,5,6,7.8));
  database.push_back(new Mercedez(9,10,11,12));
  database[2]->rear_lights = 14;


Error:
error C2039: 'rear_lights' : is not a member of 'Car'

Again this error. What I don't understand, is why this doesn't work. Via the method GetInfo(), we can access rear_lights, but why not on that way?

I think you'll understand my problem now =)
Last edited on
If you have a Car*, you can only access elements of Car, since all you can know for sure is that it has all the variables of a Car. However, getinfo() can get the other information because it is a virtual function which knows that the object is actually a Mercedez.
Hmmm, okay, I understand that.

So is there a solution for my problem?
to do so you must convert Car* to Mercedez*
like this:
1
2
Mercedez* m = (Mercedez*)database[2];
m->rear_lights = 14;

or
((Mercedez*)database[2])->rear_lights = 14

You should probably use something like dynamic_cast<Mercedez*> instead of (Mercedez*) but for some reason I never do so. see http://www.cplusplus.com/doc/tutorial/typecasting/
Hope this helps
That seems to work!

I don't have time to mess with it right now, it will be for this weekend. I'll let you know if it works now or if I find new problems =)

Thank you very much, all of you who responded here :)
Topic archived. No new replies allowed.
Pages: 12