Thread: still somewhat confused with inheritance~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Arrow still somewhat confused with inheritance~

    Hi, guys.
    I'm somewhat confused with inheritance yet. Please check my code first, it works just fine.
    PHP Code:
    #include <iostream>
    #include <string>

    class Vehicle
    {
      public:
        
    Vehicle(stringintint);
        
    Vehicle(){cout << "undefined vehicle" << endl;};
        
    void profile();
      private:
        
    string name;
        
    int wheel;
        
    int speed;
    };
    Vehicle :: Vehicle(string sint wint sp)
    {
      
    name=s;
      
    wheel=w;
      
    speed=sp;
      
    cout << "the name of the vehicle present is: " << name << endl;
      
    cout << "it has " << << " wheels." << endl;
      
    cout << "its speed is " << sp << " km/h." << endl;
    }
    void Vehicle :: profile()
    {
      
    cout << "welcome here, I'll introduce the vehicle later." << endl;
    }

    class 
    Car : public Vehicle
    {
      public:
        
    Car(stringintintint);
        
    Car(){ cout << "undefined car" << endl; };
      private:
        
    int price;
    };
    Car :: Car(string sint wint spint p) : Vehicle(swsp)
    {
      
    price=p;
      
    cout << "its price is " << price << " dollars." << endl;
    }

    void main()
    {
      
    Car myCar1;
      
    cout << endl;
      
    Car myCar2("bicycle"2150);

    Now, 2 questions are waiting your suggestions:
    1 when I create a object with the Car class, the old constructor with base class Vehicle are called first. we could see it from the output. Could I get ride of those which belong to base class and display only the contents of derived class ?
    2 the first constructor of derived class has 4 arguments, and 3 of them belong to the base class Vehicle, so I write something like this: : Vehicle(s, w, sp) but what about when we need of part of those arguments ? for example, we just want pass s and sp from the old class. I tried : Vehicle(s, sp) but got an error.
    Last edited by black; 06-30-2002 at 10:39 PM.
    Never end on learning~

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i think you asking questions that are explain just a few pages ahead of you in your book (assuming your learning from a book). look up overriding.

    as for the suggestion 1, i'm pretty sure the base constructor is _always_ called first. so it follows that you need to modify the base class, but that doesn't make sense as far as software reusability--maybe you can override a constructor also?

    looks like we're learning side by side, good luck let me know what you find out

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Talking

    Originally posted by blight2c
    looks like we're learning side by side, good luck let me know what you find out
    Both are dark side, aha !
    Never end on learning~

  4. #4
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    >1 when I create a object with the Car class, the old constructor with base class Vehicle are called first. we could see it from the output. Could I get ride of those which belong to base class and display only the contents of derived class ?

    Usually you don't print anything in the Constructor - it is just for initializing.

    >2 the first constructor of derived class has 4 arguments, and 3 of them belong to the base class Vehicle, so I write something like this: : Vehicle(s, w, sp) but what about when we need of part of those arguments ? for example, we just want pass s and sp from the old class. I tried : Vehicle(s, sp) but got an error.

    Why do you want to create a Vehcle without knowing the number of wheels?-) You should make another constructor for handling only two arguments:
    Vehicle :: Vehicle(string s, int sp)
    And you probably want to set some default value for the amount of wheels there.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by raimo
    >1 when I create a object with the Car class, the old constructor with base class Vehicle are called first. we could see it from the output. Could I get ride of those which belong to base class and display only the contents of derived class ?

    Usually you don't print anything in the Constructor - it is just for initializing.

    >2 the first constructor of derived class has 4 arguments, and 3 of them belong to the base class Vehicle, so I write something like this: : Vehicle(s, w, sp) but what about when we need of part of those arguments ? for example, we just want pass s and sp from the old class. I tried : Vehicle(s, sp) but got an error.

    Why do you want to create a Vehcle without knowing the number of wheels?-) You should make another constructor for handling only two arguments:
    Vehicle :: Vehicle(string s, int sp)
    And you probably want to set some default value for the amount of wheels there.
    1 how could I do if I want to initialize the variables with other values ???

    2 we may not use some arguments are eternal in Car class, because cars have 4 wheels as usual. it will be more convenient if we could call only the left arguments.


    if I'am somewhat wrong please let me know.
    Never end on learning~

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You're trying to call a non existent constructor with your two argument Car declaration. You'd need another constructor that takes two arguments. To be safe, both classes should have default, empty constructors, although your compiler might create them anyway.
    Later on, you may want to make Vehicle an abstract base class, since you probably won't ever need to declare a Vehicle object.
    What book are you using to learn? I used Deitel's book. It explains this stuff w/lots of examples pretty well.
    Truth is a malleable commodity - Dick Cheney

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Arrow

    Originally posted by salvelinus
    You're trying to call a non existent constructor with your two argument Car declaration. You'd need another constructor that takes two arguments. To be safe, both classes should have default, empty constructors, although your compiler might create them anyway.
    Later on, you may want to make Vehicle an abstract base class, since you probably won't ever need to declare a Vehicle object.
    What book are you using to learn? I used Deitel's book. It explains this stuff w/lots of examples pretty well.
    my book just said I need a initial list to pass arguments of base class, it did say that is the right constructor.

    the name of my book is "programming in C++", it is a basically one, but to my surprise it said almost nothing about pointer and reference. So sad~
    Never end on learning~

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Well, then your book is wrong. You don't have a Vehicle constructor that takes two arguments, so you can't pass just two arguments to your three argument constructor.
    You could initialize for default values in your Vehicle constructor, in a manner similar to how you're doing it from Car. That's why I suggested looking up member initialization. It has to be done in a particular order, in your case in the Vehicle class.
    Truth is a malleable commodity - Dick Cheney

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by salvelinus
    Well, then your book is wrong. You don't have a Vehicle constructor that takes two arguments, so you can't pass just two arguments to your three argument constructor.
    You could initialize for default values in your Vehicle constructor, in a manner similar to how you're doing it from Car. That's why I suggested looking up member initialization. It has to be done in a particular order, in your case in the Vehicle class.
    Mmm....I got it. it seems we couldnt believe in everything in books, hehe~

    thanx, guys~
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM