Question

I'm trying to create a class containing a virtual function, which i would like to inherit in two child classes.

I know some people already asked this (here and there for example), but i couldn't understand the answers.

So i made a simplified example code of what i'm trying :

//Mother .h file 

#ifndef _MOTHER_H_
#define _MOTHER_H_
#include <iostream>

class Mother
{
 protected :
   std::string _name;
 public:
   Mother(std::string name);
   ~Mother();
   virtual std::string getName() = 0; 
};

#endif

//Mother .cpp file

#include "Mother.h"

Mother::Mother(std::string name)
{
  this->_name = name; 
}

Mother::~Mother()
{
}


//Child.h file

#ifndef _CHILD_H_
#define _CHILD_H_
#include "Mother.h"

class Child : public Mother
{
 private : 
   std::string _name;
 public:
   Child(std::string name);
   ~Child();
};

#endif


//Child .cpp file

#include "Mother.h"
#include "Child.h"

Child::Child(std::string name) : Mother(name)
{
  this->_name = name;
}

Child::~Child()
{
}

std::string Mother::getName()
{
  return this->_name;
}

Here is my main.cpp file :

//Main.cpp file
#include "Child.h"

int main()
{
   Child l("lol");

   std::cout << l.getName() << std::endl;

   Mother& f = l;

   std::cout << f.getName() << std::endl;

   return 0;
 }

Here's what the compilator says : (compiling with g++ *.cpp -W -Wall -Wextra -Werror)

main.cpp: In function ‘int main()’:
main.cpp:5:9: error: cannot declare variable ‘l’ to be of abstract type‘Child’
In file included from main.cpp:1:0:
Child.h:8:7: note:   because the following virtual functions are pure within ‘Child’:
In file included from Child.h:6:0,
             from main.cpp:1:
Mother.h:14:23: note:   virtual std::string Mother::getName()

What am i doing wrong ?

(sorry if i made some english mistakes, i am not a native speaker).

Was it helpful?

Solution

In Mother's declaration you have:

virtual std::string getName() = 0; 

This is not just a virtual, but a pure virtual. The distinction between a virtual and a pure virtual is that the pure variety must have an override implemented in a derived class, even if you have provided an implementation in the base class. For example:

class Foo
{
public:
  virtual void DoIt() = 0 {};  // pure virtual.  Must be overridden in the derived class even though there is an implementation here
};

class Bar : public Foo
{
public:
  void DoIt(); // override of base
};

void Bar::DoIt()
{
  // implementation of override
}

You can't instantiate a class with un-implemented pure virtual methods. If you try, you will get a compiler error:

int main()
{
  Foo f;  // ERROR
  Bar b;  // OK
}

And that is exactly what you tried to do. You declared getName() to be pure virtual in Mother, but you did not override it in Child. Then you tried to instantiate a Child

int main()
{
   Child l("lol");

Which resulted in the compiler error.

To fix it, provide an override of getName() in the Child class.

OTHER TIPS

You class child should override getName() method as it is pure virtual defined in class mother

Seems typo to me.. as std::string Mother::getName() is defined in child.cpp..

std::string Child::getName()
{
  return this->_name;
}

The pure virtual function in your base class Mother does not make sense from an OOP perspective, it is a common trait in all children so the same function could be used. There is no need to override it.

struct Person
{
  Person(std::string name) : _name(name) {}
  std::string _name;
  std::string getName() {return _name; }
};

struct Mother : Human
{
   Mother(std::string name) : Person(name) {}
};

struct Child : Human
{
   Child(std::string name) : Person(name) {}
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top