Question on Inheritance (Calling the base class)

This is a discussion on Question on Inheritance (Calling the base class) within the C++ Programming forums, part of the General Programming Boards category; How do I call public methods of the base class (the class it was inherited from, aka super class)? I ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    Question on Inheritance (Calling the base class)

    How do I call public methods of the base class (the class it was inherited from, aka super class)?

    I have been reading around but it has been quite unclear.

    Say, for instance, I wanna call a method from my super class.

    Car (extends Vehicle)
    Code:
    ...
    void Car::Display()
    {
       string color = Vehicle::getColor();
       cout << color;
    }
    ...
    Is this the correct way to do this?

    Because in Java, I just type super.getColor();
    In C#, I type Base.getColor();
    Last edited by markcls; 03-26-2007 at 01:16 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes.

    Seriously, read the tutorials, FAQ, and start testing code on your own.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Heheh, sorry

    I've been reading the tutorials and FAQs, and have started testing my own code.

    But I just wanted to reconfirm. Because I saw a site saying you could call it using Base::, but I had so many problems with that. Was just wondering if this is the only way

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    #include <iostream>
    
    class Parent
    {
    public:
    	int getID()
    	{
    		return 100;
    	}
    };
    
    class Child : public Parent
    {
    public:
    	int getID()
    	{
    		std::cout << "Super class ID: " << Parent::getID() << std::endl;
    		return 200;
    	}
    };
    
    int main()
    {
    	int iID;
    	Child *c = new Child;
    	
    	iID = c->getID();
    	std::cout << "The ID of the child is: " << iID << std::endl;
    
    	delete c;
    	
    	return 0;
    }
    Output:

    Code:
    Super class ID: 100
    The ID of the child is: 200
    Note: The best time to write code is when you're exhausted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 06:11 PM
  3. Replies: 25
    Last Post: 10-29-2007, 05:08 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM

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