1
In Java we have the possibility to create abstract classes, but without the possibility of instantiating 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 it is done?
So far I have tried to do what was said above (or almost), create an abstract class and inherit its its attributes and methods for another class that will be instantiable. Follows 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 I compile the program with GNU g++:
error: cannot 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
The idea of having abstract methods in the base class is so that they have no implementation in that base class and only in derived classes. Not to mention that it’s very unusual for a getter or Setter to be abstract.
– Isac
@Isac Living and learning! did not know this business of getter and Setter be unusual as abstracts. Thank you for your comment.
– user83187
@Isac "The idea of having abstract methods in the base class is so that they have no implementation in that base class and only in the derived classes." - So in this case I am incorrect in implementing my abstract base class?
– user83187
It would be incorrect, the idea is not to implement in the base class. If you are going to implement in the base class then do not put them abstract, removing the
=0
in each of them.– Isac
@Isac Blz! So I’m going to implement in Daughter. h? More how do I do this?
– user83187
Not in your case it would make sense to leave the implementation in the
Mother.h
but without being abstract, because it would hardly want to give another different implementation in the derived class. But it’s a little hard to understand what you intend to do in such an exoteric example.– Isac
@Isac Cara...but the business here is not if it makes sense and yes I know how I will make the inheritance of an abstract class, it is not at all that the name of the variables has "test" everywhere.
– user83187