In Java we have the possibility of creating abstract classes, but without the possibility to instantiate them, but we can create and instantiate a class that inherits the attributes and methods of an abstract parent. In C ++ this is done in a different way and I would like to know how to do it?
So far I have tried to do what was said above (or almost), create an abstract class and inherit its attributes and methods for another class that will be instantiable. Here is the code:
Mother.h
#ifndef MOTHER_H_INCLUDED
#define MOTHER_H_INCLUDED
#include <string>
typedef std::string str;
class Mother{
private:
str test01;
str test02;
unsigned test03;
public:
virtual void setTest01(str teste01_)=0;
virtual void setTest02(str teste02_)=0;
virtual void setTest03(unsigned teste03_)=0;
public:
virtual str getTest01()=0;
virtual str getTest02()=0;
virtual unsigned getTest03()=0;
};
#endif // MOTHER_H_INCLUDED
//Red Hat
Mother.cpp
#include "Mother.h"
void Mother::setTest01(str test01_){
test01=test01_;
}
void Mother::setTest02(str test02_){
test02=test02_;
}
void Mother::setTest03(unsigned test03_){
test03=test03_;
}
str Mother::getTest01(){
return test01;
}
str Mother::getTest02(){
return test02;
}
unsigned Mother::getTest03(){
return test03;
}
Daughter.h
#ifndef DAUGHTER_H_INCLUDED
#define DAUGHTER_H_INCLUDED
#include "Mother.h"
class Daughter: virtual public Mother{
};
#endif // DAUGHTER_H_INCLUDED
main.cpp
#include "Mother.h"
#include "Daughter.h"
int main(void){
Daughter tst;
return 0;
}
When compiling the program with GNU g ++:
error: can not declare variable 'tst' to be of abstract type 'Daughter' Daughter.h | 6 | note: because the following virtual functions are pure within 'Daughter': |
... among other errors and warnings