C++ Quick Start--12--Abstract class and interface simulation

A Preliminary Study of Abstract Classes

What is an abstract class? A class containing a pure virtual function is an abstract class. What is a pure virtual function? That is, virtual void fun() = 0; such a function is a pure virtual function.

class Figer{
public:
    virtual int getArea() = 0;
};

class Circle:public Firger{
public:
    Circle(int a,int b){
        this->a = a;
        this->b = b;
    }
    virtual int getArea(){
        return 3.14 * this->a * this->b;
    }
private:
    int a,b;
};

class Square:public Figer{
public:
    Square(int a,int b){
        this->a = a;
        this->b = b;
    }
    virtual int getArea(){
        return a*b;
    }
private:
    int a,b;
};

void printArea(Figer & figer){
    cout<<getArea()<<endl;
}

int main(){
    Circle c(1,2);
    Square s(1,2);
    printArea(c);
    printArea(s);
    return 0;
}

With an abstract class, we can program interface-oriented, we can first define a set of abstract class templates, and then implement it. The implementation of each module does not need to be related to how others implement it, because using the polymorphism of C++, we only need to point to the subclass object through the template class.
For example , in the printArea() method above, the Square class and the Circle class can be two types. Written by different people, they don't need to know how the other party implemented it, they just need to pass the parameters to the printArea.

Mock java interface using abstract class

In C++ you cannot define an interface directly, but you can simulate an interface through an abstract class.

class Interface1 {
public:
    virtual int add(int a, int b) = 0;
    virtual void print() = 0;
};

class Interface2 {
public:
    virtual int mult(int a,int b) = 0;
    virtual void print() = 0;
};

class Parent {
protected:
    int a;
    Parent(int a) {
        this->a = a;
    }
    virtual int getA() {
        return this->a;
    }
};

class Child :public Parent,public Interface1,public Interface2{
public:
    Child(int a):Parent(a) {
        this->a = 2;
    }
    virtual int add(int a,int b) {
        return a + b;
    }
    virtual int mult(int a,int b) { 
        return a * b;
    }
    virtual void print() {
        cout << this->a << endl;
    }   
    virtual int getA() {
        return this->a;
    }
};  

int main() {
    Interface1 * c1 = new Child(1);
    Interface2 * c2 = new Child(2);
    cout << c1->add(1, 2) << endl;
    cout << c2->mult(1, 2) << endl;
    c1->print();
    delete (Child *)c1;
    delete (Child *)c2;
    system("pause");
}

You will find that Interface1 and Interface2 here are like an interface in java. It does not have its own implementation, but defines a set of templates, which are handed over to subclasses to implement. The main difference between java interfaces and abstract classes is that interfaces can be implemented. Multiple, and abstract classes can only be inherited once, and multiple inheritance in C++, you don't have to worry about this abstract class can only be inherited once.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569858&siteId=291194637