Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call derived class method from base class reference

class Material
{
public:
 void foo()
 {
  cout << "Class Material";
 }
};

class Unusual_Material : public Material
{
public:
 void foo()
 {
  cout << "Class Unusual_Material";
 }
};

int main()
{
 Material strange = Unusual_Material();
 strange.foo(); //outputs "Class Material" 

 return 0;
}

I would like for this to result in the "Class Unusual_Material" being displayed to the console. Is there a way I can achieve this? In my program I have a class Material from which other more specific materials are derived. The method Material::foo() represents a method in Material that is adequate for most materials, but occationally, another foo() needs to be defined for a material with unusual properties.

All objects in my program contain a Material field. In the event that they are assigned an unusual material, I would like the derived, unusual foo to be called.

This is probably either pretty easy, or impossible, but I can't figure it out either way.

Thanks

like image 734
user487100 Avatar asked Nov 17 '10 20:11

user487100


People also ask

Can we call derived class method from base class?

Even though the derived class can't call it in the base class, the base class can call it which effectively calls down to the (appropriate) derived class. And that's what the Template Method pattern is all about.

How do you call a base class method from a derived class in C#?

base (C# Reference)The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

What is the connection between the base class and the derived class?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

Can a base class object access the methods of derived class?

In general this is not possible.


2 Answers

What you want is polymorphism, and to enable it for a function you need to make it virtual:

class Material 
{ 
public: 
    virtual void foo() // Note virtual keyword!
    { 
        cout << "Class Material"; 
    } 
}; 

class Unusual_Material : public Material 
{ 
public: 
    void foo() // Will override foo() in the base class
    { 
        cout << "Class Unusual_Material"; 
    } 
}; 

Also, polymorphism only works for references and pointers:

int main()  
{  
    Unusual_Material unusualMaterial;
    Material& strange = unusualMaterial;
    strange.foo();  
    return 0; 
}

/* OR */

int main()  
{  
    Unusual_Material unusualMaterial;
    Material* strange = &unusualMaterial;
    strange->foo();  
    return 0; 
}

What you have in your code snippet will slice the Unusual_Material object:

int main() 
{ 
    // Unusual_Material object will be sliced!
    Material strange = Unusual_Material(); 
    strange.foo(); 
    return 0; 
} 
like image 187
In silico Avatar answered Sep 19 '22 09:09

In silico


Still better explanation would be..

class Base
{
public:
 void foo()     //make virtual void foo(),have derived method invoked
 {
  cout << "Class Base";
 }
};
class Derived: public Base
{
public:
 void foo()
 {
  cout << "Class Derived";
 }
};
int main()
{
 Base base1 = Derived ();
 Base1.foo(); //outputs "Class Base" 
           // Base object, calling base method

 Base *base2 = new Derived ();
 Base2->foo(); //outputs"Class Base",Again Base object calling base method

// But to have base object, calling Derived method, following are the ways
// Add virtual access modifier for base foo() method. Then do as below, to //have derived method being invoked.
//
// Base *base2 = new Derived ();
// Base2->foo();    //outputs "Class Derived" .

return 0;
}
like image 37
Avinash Goud N J Avatar answered Sep 20 '22 09:09

Avinash Goud N J