Homework of Chapter Polymorphism and Virtual Function

Homework:
Define a virtual base class Shape, then derive five class: Circle, Square, Rectangle, Trapezoid and Triangle. Using virtual funciton to work out and display the area of every graph. And get the sum of these graphs.

Tomorrow, oh no, it has, I will learn the last but one Chapter:I/0 stream. Also, hope it is not too difficult.
#include <iostream>
#include <cmath>

using namespace std;
const float Pi=3.14;

class Shape
{
    public:
        Shape(){}
        virtual float getAreaValue() const
        {
            return 0.0;
        }
        virtual void printArea() const = 0;
};

class Circle : public Shape
{
    public:
        Circle()
        {
            radius=0.0;
        }
        Circle(float r)
        {
            radius=r;
        }
        virtual void printArea() const;
        virtual float getAreaValue() const;
    private:
        float radius;
};

void Circle::printArea() const
{
    cout << "Circle Area:";
    cout << Pi*radius*radius << endl;
}

float Circle::getAreaValue() const
{
    return Pi*radius*radius;
}

class Square : public Shape
{
    public:
        Square()
        {
            side=0.0;
        }
        Square(float s)
        {
            side=s;
        }
        virtual void printArea() const;
        virtual float getAreaValue() const;
    private:
        float side;
};

void Square::printArea() const
{
    cout << "Square Area:";
    cout << side*side << endl;
}

float Square::getAreaValue() const
{
    return side*side;
}

class Rectangle : public Shape
{
    public:
        Rectangle()
        {
            length=0.0;
            width=0.0;
        }
        Rectangle(float l,float w)
        {
            length=l;
            width=w;
        }
        virtual void printArea() const;
        virtual float getAreaValue() const;
    private:
        float length;
        float width;
};

void Rectangle::printArea() const
{
    cout << "Rectangle Area:";
    cout << length*width << endl;
}

float Rectangle::getAreaValue() const
{
    return length*width;
}

class Trapezoid : public Shape
{
    public:
        Trapezoid()
        {
            topSide=0.0;
            bottomSide=0.0;
            height=0.0;
        }
        Trapezoid(float t,float b,float h)
        {
            topSide=t;
            bottomSide=b;
            height=h;
        }
        virtual void printArea() const;
        virtual float getAreaValue() const;
    private:
        float topSide;
        float bottomSide;
        float height;
};

void Trapezoid::printArea() const
{
    cout << "Trapezoid Area:";
    cout << ((topSide+bottomSide)*height)/2.0 << endl;
}

float Trapezoid::getAreaValue() const
{
    return ((topSide+bottomSide)*height)/2.0;
}

class Triangle : public Shape
{
    public:
        Triangle()
        {
            sideA=0.0;
            sideB=0.0;
            sideC=0.0;
        }
        Triangle(float a,float b,float c)
        {
            sideA=a;
            sideB=b;
            sideC=c;
        }
        virtual void printArea() const;
        virtual float getAreaValue() const;
    private:
        float sideA;
        float sideB;
        float sideC;
};

void Triangle::printArea() const
{
    if(((sideA+sideB)>sideC)&&((sideA+sideC)>sideB)&&((sideC+sideB)>sideA))
    {
        float temp=(sideA+sideB+sideC)/2.0;
        float s=0.0;
        s=sqrt(temp*(temp-sideA)*(temp-sideB)*(temp-sideC));
        cout << "Triangle Area:";
        cout << s << endl;
    }
    else
        cout << "These 3 sides cannot creat a triangle." << endl;
}

float Triangle::getAreaValue() const
{
    if(((sideA+sideB)>sideC)&&((sideA+sideC)>sideB)&&((sideC+sideB)>sideA))
    {
        float temp=(sideA+sideB+sideC)/2.0;
        float s=0.0;
        s=sqrt(temp*(temp-sideA)*(temp-sideB)*(temp-sideC));
        return s;
    }
    else
        return 0.0;
}

int main(void)
{
    Shape *p[5]={NULL};

    Circle circle(2.0);
    p[0]=&circle;
    Square square(3.0);
    p[1]=&square;
    Rectangle rectangle(3.0,4.0);
    p[2]=&rectangle;
    Trapezoid trapezoid(3.0,4.0,2.0);
    p[3]=&trapezoid;
    Triangle triangle(3.0,4.0,5.0);
    p[4]=&triangle;

    for(int i=0;i<5;i++)
        p[i]->printArea();

    float areaSum=0.0;
    for(int i=0;i<5;i++)
        areaSum+=p[i]->getAreaValue();
    cout << "\nSum of these graphs:" << areaSum << endl;

    return 0;
}

评论

此博客中的热门博文

开通此博客的缘由

链表的应用