c ++ abstract data types

#include <iostream>

class Shape
{
public:
    Shape() {}
    virtual ~Shape() {}
    virtual long getArea() { return -1; } // error
    virtual long getPerim() { return -1; }
    virtual void draw() {}
};

class Circle : public Shape
{
public:
    Circle(int newRadius):radius(newRadius) {}
    ~Circle() {}
    long getArea() { return 3 * radius * radius; }
    long getPerim() { return 9 * radius; }
    void draw();
private:
    int radius;
    int circumference;
};

void Circle::draw()
{
    std::cout << "Circle drawing routine here!\n";
}

class Rectangle : public Shape
{
public:
    Rectangle(int newLen, int newWidth):
        length(newLen), width(newWidth) {}
    virtual ~Rectangle() {}
    virtual long getArea() { return length * width; }
    virtual long getPerim() { return 2 * length + 2 * width; }
    virtual int getLength() { return length; }
    virtual int getWidth() { return width; }
    virtual void draw();
private:
    int width;
    int length;
};

void Rectangle::draw()
{
    for (int i = 0; i < length; i++)
    {
        for (int j = 0; j < width; j++)
            std::cout << "x ";

        std::cout << "\n";
    }
}

class Square : public Rectangle
{
public:
    Square(int len);
    Square(int len, int width);
    ~Square() {}
    long getPerim() { return 4 * getLength(); }
};

Square::Square(int newLen):
    Rectangle(newLen, newLen)
{}

Square::Square(int newLen, int newWidth):
    Rectangle(newLen, newWidth)
{
    if (getLength() != getWidth())
        std::cout << "Error, not a square ... a rectangle?\n";
}

int main()
{
    int choice;
    bool fQuit = false;
    Shape * sp;

    while (1)
    {
        std::cout << "(1) Circle (2) Rectangle (3) Square (0) Quit: ";
        std::cin >> choice;

        switch (choice)
        {
        case 1:
            sp = new Circle(5);
            break;
        case 2:
            sp = new Rectangle(4, 6);
            break;
        case 3:
            sp = new Square(5);
            break;
        default:
            fQuit = true;
            break;
        }
        if (fQuit)
            break;

        sp->draw();
        std::cout << "\n";
    }
    return 0;
}

shape is an abstract data type (ADT) , only the interface for its derived classes, if you try to instantiate a Shape object that will be very troublesome, it may be impossible.

It shows a concept of abstract data types (e.g., shape), rather than specific objects (such as a circle). In c ++ in, ADT can only base class for other classes, but in fact can not be created, therefore, can not create a Shape instance if the Shape is declared as ADT.

18.2 pure virtual function

C ++ to support the creation of abstract data types by providing a pure virtual function. Pure virtual functions must be overridden in the derived class virtual function. By virtual function is initialized to 0 to be declared as pure virtual.

Anything that contains one or more pure virtual functions are ADT can not be instantiated, attempts to do so will result in a compilation error, the pure virtual functions in the class pointed to its customers the following two points.

1. Do not create this object class, it should be derived.

2. Be sure to rewrite this class inherits the pure virtual function.

In the ADT derived class, inherited pure virtual function remains pure virtual, to be instantiated, it must be for each derived class pure virtual function, otherwise the derived class will also be the ADT.

ADT can not be instantiated as follows:

#include <iostream>
using namespace std;

class Shape
{
public:
    Shape() {}
    virtual ~Shape() {}
    virtual long getArea() { return -1; } // error
    //virtual long getArea()=0;


    virtual long getPerim() { return -1; }
    //virtual long getPerim()=0;



    virtual void draw() {}
    //virtual void draw()=0;


};

class Circle : public Shape
{
public:
    Circle(int newRadius):radius(newRadius) {cout<<"circle"<<endl;}
    ~Circle() {}
    long getArea() { return 3 * radius * radius; }
    long getPerim() { return 9 * radius; }
    void draw();
private:
    int radius;
    int circumference;
};

void Circle::draw()
{
    std::cout << "Circle drawing routine here!\n";
}

class Rectangle : public Shape
{
public:
    Rectangle(int newLen, int newWidth):
        length(newLen), width(newWidth) {}
    virtual ~Rectangle() {}
    virtual long getArea() { return length * width; }
    virtual long getPerim() { return 2 * length + 2 * width; }
    virtual int getLength() { return length; }
    virtual int getWidth() { return width; }
    virtual void draw();
private:
    int width;
    int length;
};

void Rectangle::draw()
{
    for (int i = 0; i < length; i++)
    {
        for (int j = 0; j < width; j++)
            std::cout << "x ";

        std::cout << "\n";
    }
}

class Square : public Rectangle
{
public:
    Square(int len);
    Square(int len, int width);
    ~Square() {}
    long getPerim() { return 4 * getLength(); }
};

Square::Square(int newLen):
    Rectangle(newLen, newLen)
{}

Square::Square(int newLen, int newWidth):
    Rectangle(newLen, newWidth)
{
    if (getLength() != getWidth())
        std::cout << "Error, not a square ... a rectangle?\n";
}

int main()
{
    int choice;
    bool fQuit = false;
    Shape * sp;

   // Shape *ptr=new Shape;    //ci chu shi bu neng chuang jian shape dui xiang de 

    while (1)
    {
        std::cout << "(1) Circle (2) Rectangle (3) Square (0) Quit: ";
        std::cin >> choice;

        switch (choice)
        {
        case 1:
            sp = new Circle(5);
            break;
        case 2:
            sp = new Rectangle(4, 6);
            break;
        case 3:
            sp = new Square(5);
            break;
        default:
            fQuit = true;
            break;
        }
        if (fQuit)
            break;

        sp->draw();
        std::cout << "\n";
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/suyunzzz/article/details/89974994