How do I inherit an abstract class in C ++?

1

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

    
asked by anonymous 25.11.2017 / 22:27

1 answer

1

I disagree with the comment that says abstract getters / setters are uncommon. In fact the C ++ mechanism for getters / setters is unusual, but where they are used it is common to use it if it makes sense for the class to be really abstract, which often does not, there would not even be an unusual case , would be wrong.

I totally agree that an example class with abstract names and no clear definition of what the object is can not model anything right. What can be right for one case can be very wrong for another. If you do something that does not make sense then you are wrong and you should not.

Both Isac and I and several other users here are committed to teaching right, we can make mistakes, but we try to get right all the time. It would be irresponsible of us to accept something wrong by knowing what it is. So you have some solutions depending on what the object is.

Abstract class

The class is not abstract or at least part of it is not abstract (yes, it is possible to have an abstract part and another not, of course the class will be considered abstract and not installable with only an abstract method, but the rest needs to be abstract:

class Mother {
    string test01;
    int test03;
    public:
        virtual string getTest01();
        virtual void setTest01(string teste01);
        virtual int getTest03();
        virtual void setTest03(int teste03);
};

string Mother::getTest01() { return test01; }
void Mother::setTest01(string test01) { this.test01 = test01; }
int Mother::getTest03() { return test03; }
void Mother::setTest03(int test03) { this.test03 = test03; }

Daughter.h

class Daughter: virtual public Mother {}; //só faz sentido se fizer mais alguma coisa

Interface

If you want the methods to be abstract it does not make sense to have attributes, so we can make this class an interface:

class Mother {
    public:
        virtual string getTest01() = 0;
        virtual void setTest01(string teste01) = 0;
        virtual int getTest03() = 0;
        virtual void setTest03(int teste03) = 0;
};

Daughter.h

class Daughter: virtual public Mother {}; //não muito, mas aqui até faz sentido

string Daughter::getTest01() { return test01; }
void Daughter::setTest01(string test01) { this.test01 = test01; }
int Daughter::getTest03() { return test03; }
void Daughter::setTest03(int test03) { this.test03 = test03; }

I put it in GitHub for future reference.

Simplified because if it is an abstract example you do not have to put more than the minimum necessary. I could have taken Teste03 , I did not do it to show that I use int (almost always using unsigned is an error and when it is not used unsigned int (programming for you to understand understanding is easy, for everyone to understand is more difficult.) I also used it to show that it is customary to group the methods for what they are and not for what they do.

And I've even used string because that typedef only serves to cause readability problems and eat some error unintentionally.

Of course I've taken parts of the code that are not necessary for understanding the problem since it is too abstract an example.

I did not do an abstract class example (instead of an interface) because the example is too abstract, but it would be possible to have attributes in cases where it makes sense (not this).

I made other cosmetic improvements, but important.

    
26.11.2017 / 14:08