failed with inheritance~

This is a discussion on failed with inheritance~ within the C++ Programming forums, part of the General Programming Boards category; Hi, all~ after reading the chapter of inheritance, I couldnt help testing it. but I failed with my first test, ...

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

    Unhappy failed with inheritance~

    Hi, all~

    after reading the chapter of inheritance, I couldnt help testing it. but I failed with my first test, is there anything omitted ???

    my code in short is:
    PHP Code:
    class Vehicle
    {
      
    //my code about class Vehicle;
    };
    class 
    Car : public Vehicle
    Never end on learning~

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    class..Car..:..public..Vehicle {
    // with all pure virtuals implemented
    };
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by SilentStrike
    class..Car?..public..Vehicle {
    // with all pure virtuals implemented
    };
    shall I fill it with a new constructor ? I tried but failed a second time. the code is right below:

    PHP Code:
    class Vehicle
    {
      
    //my code about class Vehicle;
    };
    class 
    Car : public Vehicle
    {
      public:
        
    Car(stringintint)
        {
          
    cout << "I'm a car !";
        }
      private:
        
    int door;
    }; 
    Never end on learning~

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    no response ? my goodness !
    Never end on learning~

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Code works for me, after replacing Car(string, int, int) with Car(std::string s, int j, int k) and adding the appropriate headers. What is the problem?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Yep, the classes look fine to me too.
    Also, there might indeed be a problem with the constructor, maybe compiler specific. Try if it works when you make a constructor without any input.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Does the Vehicle class have a default (takes no arguments) constructor? If so, it looks like that code should work.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    it works at last but something confused me. Let's glimpse code first:

    PHP Code:
    #include <iostream>
    #include <string>

    class Vehicle
    {
      public:
        
    Vehicle(stringintint);
        
    Vehicle();     
      private:
        
    string name;
        
    int tire;
        
    int speed;
    };
    Vehicle :: Vehicle(string sint tint sp)
    {
      
    //...
    }
    Vehicle :: Vehicle()
    {
      
    //...
    }

    class 
    Car : public Vehicle{};

    void main()
    {
      
    Car myCar;

    it works fine, but the compiler would commit some mistakes when I build up a child class with the latter constructor which has 3 arguments just like this below:
    PHP Code:
    void main()
    {
      
    Car myCar("bicycle"215);

    Havent we inherited all the public member functions from the base class ?

    Last edited by black; 06-28-2002 at 03:40 AM.
    Never end on learning~

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Are you forgetting the using statement in Vehicle? Assuming you're using cout or something since you #include <iostream>
    Truth is a malleable commodity - Dick Cheney

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    This code works:
    PHP Code:
    #include <iostream>
    #include <string>

    using namespace std;

    class 
    Vehicle
    {
      public:
        
    Vehicle(stringintint);
        
    Vehicle();     
      private:
        
    string name;
        
    int tire;
        
    int speed;
    };
    Vehicle::Vehicle(string sint tint sp)
    {
        
    name s;
        
    tire t;
        
    speed sp;
        
    cout << name << ' ' << tire << ' ' << speed << endl;
    }
    Vehicle::Vehicle()
    {
        
    cout << "Default constructor\n";
    }

    class 
    Car : public Vehicle
    {};

    int main()
    {
      
    Car myCar;
      
    Vehicle yourCar("pickup"1755);
      return 
    0;

    but changing Vehicle yourCar... to Car yourCar... doesn't. That's because w/o a base class initializer, the default base class constructor is called, which doesn't take 3 arguments.
    This works for Car
    PHP Code:
    class Car : public Vehicle
    {
        public:    
            
    Car::Car(){};
            
    Car::Car(string nint tint s) : Vehicle(nts){};
    }; 
    That runs and compiles correctly. You have to add the default constructor to Car when you add the other one, at least in MSVC. W/o the 2nd constructor, you'll be able to use the default constructor in Vehicle.
    Truth is a malleable commodity - Dick Cheney

  11. #11
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Car::Car(string n, int t, int s) : Vehicle(n, t, s){};
    what about this line please ??? I have never seen it before.
    Never end on learning~

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    That's an initialization statement. In this case, it initializes the non default base class constructor. The Car constructor is passing the values in the Car declaration (in main) to the Vehicle constructor.
    Check out help files on member and base class initialization.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Find Injected DLLs In A Process?
    By pobri19 in forum Windows Programming
    Replies: 35
    Last Post: 02-06-2010, 08:53 AM
  3. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  4. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  5. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21