-2

I understand how this goes in the opposite direction. But for various reasons i want to use a base class object to call a derived class method

Let's say we have 2 classes, together representing a person's data (name and age) :

class Person 
{
protected: 
    char* name;   /// may be more than just one. also, heared that std::string is more efficient
public: 
    /// constructors, operator=, destructors, methods and stuff...   
}

class Info: public Person
{
protected: 
    int age;  /// may be more than one parameter.
public:
    /// constructors, operator=, destructors, methods and stuff... 

   int get_age() const;   /// method i want to call with a class Person object
    {
        return y;
    }
}

Since those 2 classes are about a person's data, and i have a Person object, i want to use this object to find out his age as well ( likely calling the method get_age() from it's derived class, Info)

Saw something with pure virtual methods, but i don't know how to properly call that virtual function in main.

How can i do it ? (i will apreciate if you can show me the main of the program too).

RedIcs
  • 11
  • 5
  • Your `main` has the right idea, though it too incomplete of an example to explain why are are getting an unexpected output. – Cory Kramer Apr 09 '21 at 19:33
  • 2
    if person doesn't have a get_age method, how are you able to call it? – Irelia Apr 09 '21 at 19:35
  • Also you didn't properly allocate myinfo, it should be `Info* myinfo = new Info;` – Irelia Apr 09 '21 at 19:35
  • 1
    I see a lot of pointers, but not one object being created in your code. Could you [edit] you question to include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Brian61354270 Apr 09 '21 at 19:36
  • 1
    The inheritance relationship isn't right. One can't reasonably say that an `Info` **is a** `Person`. This is better served with composition. – sweenish Apr 09 '21 at 19:41

1 Answers1

1

You can make sure the derived class has the function you want to call by declaring it as a virtual function in the base class. Typically a "pure virtual function" (one without an implementation) is used.

Like this:

class Person
{
protected:
    char* name;   /// may be more than just one. also, heared that std::string is more efficient
public:
    /// constructors, operator=, destructors, methods and stuff...

    // Pure Virtual Function
    virtual int get_age() const = 0;   /// force derived classes to implement

};

class Info: public Person
{
protected:
    int age;  /// may be more than one parameter.
public:
    /// constructors, operator=, destructors, methods and stuff...

   int get_age() const override   /// override here
    {
        return age;
    }
};
Galik
  • 47,303
  • 4
  • 80
  • 117
  • answer not good, got error: "cannot alocate an object of abstract type Person – RedIcs Apr 09 '21 at 20:38
  • @RedIcs Unfortunately I can´t teach you all about how inheritance works in `C++` in a simple answer. I recommend reading up on the subject., particularly about *virtual functions* and *polymorphism*. – Galik Apr 09 '21 at 21:11
  • @Redlcs The answer is good and correct. However, the rules of the language are clearly stated in the standards. If you are trying to write code without knowing some subjects, it is natural that you will encounter such an error. Trying this will fix your problem for now. But it won't add anything to you. You should learn the polymorphism and virtual dispatch mechanism well. Person *p = new Info; – bekici Apr 09 '21 at 21:20