Thread: Polymorphism newbie question

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Polymorphism newbie question

    Hi,

    I am trying to create an object which contains data members from the base class and the inherited class.

    vehicle.h:
    Code:
    #include <iostream>
    using namespace std;
    
    class Vehicle
    {
    public:
    	void EnterNewVehicle()
    	{
    		cout << "Inside Vehicle class" << endl;
    	}
    
    private:
    	int m_MPG;
    };
    
    class Car : public Vehicle
    {
    public:
    
    private:
    	int m_NoOfDoors;
    };

    main.cpp:
    Code:
    #include <iostream>
    #include "vehicle.h"
    
    using namespace std;
    
    int main()
    {
    	int choice = 0;
    	cout << "Enter 1 to create a car" << endl;
    	cin >> choice;
    
    	switch(choice)
    	{
    	case 1:
    		//Create an object of type Car
    		Vehicle node;
    		//I need to create a Car object with data members from both Vehicle and Car
    		node.EnterNewVehicle();
    
    	case 2:
    
    		break;
    	}
    
    	system ("PAUSE");
    	return 0;
    }
    So I'm trying to create an object called 'node' of type Car which will have both the data members MPG and NoOfDoors.

    I can ask the user for the MPG value, but don't know how to then transition into asking and entering the value for NoOfDoors.

    If someone could be so kind as to tell me how this is done I'd be most grateful, but PLEASE try and explain it to me in VERY basic terms because I won't 'get it' otherwise.

    Many thanks!


  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    Inherited classes contain the members of their base classes (the classes they inherit from). Because Car inherits from Vehicle (as you have defined them), when you create a Car, it will have the data members of a Vehicle as well. But because you've made Vehicle::m_MPG private, you can't access it directly from a Car. Changing it to protected will make it directly accessible from a Car. I'd recommend creating a constructor for Vehicle that takes a value for m_MPG, and a constructor for Car that takes 2 values, one for m_MPG and another for m_NoOfDoors for Car. In the initializer list of the Car constructor, call the Vehicle constructor with the mpg value. In case 1 of your switch statement, ask for those values from the user, then call that Car constructor. If this is not clear to you, do some reading in your favorite C++ book. If you don't have one I recommend Bruce Eckel's MindView, Inc: Thinking in C++ 2nd Edition by Bruce Eckel.

    As for asking from values from users, you should be able to use the same method you used to get the value for choice in main().

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    DirkMaas you helped me tremendously with this great explanation.

    I'll try this now, many many thanks!


  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Your question has to do more with inheriting implementation than polymorphism, which is more like this:

    Code:
    class Vehicle {
    public:
        virtual void move() = 0;
    };
    
    class Car : public Vehicle {
    public:
        virtual void move() 
        {
             // implement move for Car
        }
    };
    
    void moveVehicle(Vehicle * v)
    {
        v->move();  // Doesn't know what kind of Vehicle this is
    }
    
    int main()
    {
        Vehicle * car = new Car;
    
        moveVehicle(car);
    
        return 0;
    }
    Last edited by medievalelks; 04-02-2009 at 08:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  4. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  5. question on polymorphism
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2003, 07:30 PM