12-6

 #ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
class Shape
{
public:
    virtual double Area() const
    {
        return 0.0;
    }
    virtual void PrintShapeName() const = 0;
    virtual void Print() const = 0;
};
#endif
#ifndef POINT_H
#define POINT_H
#include "shape.h"
class Point : public Shape
{
    int x, y;
public:
    Point( int = 0, int = 0 );
    void SetPoint( int, int );
    int GetX()
    {
        return x;
    }
    int GetY()
    {
        return y;
    }
    virtual void PrintShapeName() const
    {
        cout << "Point: ";
    } virtual void Print() const;
};
#endif

#include <iostream>
using namespace std;
#include "point.h"
Point::Point( int a, int b )
{
    SetPoint( a, b );
}
void Point::SetPoint( int a, int b )
{
    x = a;
    y = b;
}
void Point::Print() const
{
    cout << '[' << x << ", " << y << ']';
}
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
using namespace std;
#include "point.h"
class Circle : public Point
{
    double radius;
public:
    Circle(int x = 0, int y = 0 double r = 0.0);
    void SetRadius( double );
    double GetRadius() const;
    virtual double Area() const;
    virtual void Print() const;

    virtual void PrintShapeName() const
    {
        cout << "Circle: ";
    }
};
#endif
#include "circle.h"
Circle::Circle(int a,int b,double r): Point(a,b)
{
    SetRadius( r );
}
void Circle::SetRadius( double r )
{
    radius = ( r >= 0 ? r : 0 );
}
double Circle::GetRadius() const
{
    return radius;
}
double Circle::Area() const
{
    return 3.14159 * radius * radius;
}
void Circle::Print() const circle
{

    cout << "Center = ";
    Point::Print();
    cout << "; Radius = " << radius << endl;

}
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
using namespace std;
#include "point.h"
class Rectangle : public Point
{
    double length, width;

public:
    Rectangle(int x=0, int y=0, double l=0.0, double w=0.0);
    void SetLength( double );
    void SetWidth( double );
    double GetLength() const;
    double GetWidth() const;
    virtual double Area() const;
    virtual void Print() const;

    virtual void PrintShapeName() const
    {
        cout << "Rectangle: ";
    }
};

#endif
#include "Rectangle.h"
Rectangle::Rectangle(int a,int b,double l,double w): Point(a,b)
{
    SetLength(l);
    SetWidth(w);
}

void Rectangle::SetLength( double l )
{
    length = ( l >= 0 ? l : 0 );
}
void Rectangle::SetWidth( double w )
{
    width = ( w >= 0 ? w : 0 );
}
double Rectangle::GetLength() const
{
    return length;
}
double Rectangle::GetWidth() const
{
    return width;
}
double Rectangle::Area() const
{
    return length * width;
}
void Rectangle::Print() const
{
    cout << "Left Top Vertex = ";
    Point::Print();
    cout << "; Length = " << length << ", Wigth = " << width << endl;
}

#include "circle.h"
#include "rectangle.h"
void virtualViaPointer( const Shape * );
void virtualViaReference( const Shape & );
int main()
{

    Point point(30,50);
    Circle circle(120,80,10.0);
    、
    Rectangle rectangle(10,10,8.0,5.0);
    point.PrintShapeName();
    point.Print();
    cout << endl;
    circle.PrintShapeName();
    circle.Print();
    rectangle.PrintShapeName();
    rectangle.Print();
    Shape *arrayOfShapes[ 3 ];
    arrayOfShapes[ 0 ] = &point;
    arrayOfShapes[ 1 ] = &circle;
    arrayOfShapes[ 2 ] = &rectangle;
    cout << "Virtual function calls made off " << "base-class pointers\n";
    for ( int i = 0; i < 3; i++ )
        virtualViaPointer( arrayOfShapes[ i ] );
    cout << "Virtual function calls made off " << "base-class references\n";
    for ( int j = 0; j < 3; j++ )
        virtualViaReference( *arrayOfShapes[ j ] );
    return 0;
}
void virtualViaPointer( const Shape *baseClassPtr )
{
    baseClassPtr->PrintShapeName();
    baseClassPtr->Print();
    cout << "Area = " << baseClassPtr->Area() << endl;
}
void virtualViaReference( const Shape &baseClassRef )
{
    baseClassRef.PrintShapeName();
    baseClassRef.Print();
    cout << "Area = " << baseClassRef.Area() << endl;
}

猜你喜欢

转载自blog.csdn.net/let_life_stop/article/details/80252133