头文件
#pragma once
#include "Shape.h"
class Rectange :
public Shape
{
public:
Rectange(float a,float b);
~Rectange();
public:
virtual double area()const;
private:
float length;
float width;
};
#pragma once
class Shape
{
public:
Shape();
~Shape();
public:
virtual double area()const = 0;
};
#pragma once
#include "Shape.h"
class Square :
public Shape
{
public:
Square(float a);
~Square();
public:
virtual double area()const;
private:
float length;
};
#pragma once
#include "Shape.h"
class Circle :
public Shape
{
public:
Circle(float r);
~Circle();
public:
virtual double area()const;
private:
float radius;
};
#pragma once
#include "Shape.h"
class Trapezoid :
public Shape
{
public:
Trapezoid(float a,float b,float c);
~Trapezoid();
public:
virtual double area()const;
private:
float length;
float width;
float height;
};
#pragma once
#include "Shape.h"
class Triangle :
public Shape
{
public:
Triangle(float a, float b);
~Triangle();
public:
virtual double area()const;
private:
float length;
float height;
};
cpp
#include "Circle.h"
#include "iostream"
using namespace std;
Circle::Circle(float r)
{
radius = r;
}
Circle::~Circle()
{
}
double Circle::area()const
{
cout << "Circle:" << endl;
return 3.14*radius*radius;
}
#include "Rectange.h"
#include "iostream"
using namespace std;
Rectange::Rectange(float a,float b)
{
length = a;
width = b;
}
Rectange::~Rectange()
{
}
double Rectange::area()const
{
cout << "Rectange:" << endl;
return length*width;
}
#include "Shape.h"
Shape::Shape()
{
}
Shape::~Shape()
{
}
#include "Square.h"
#include "iostream"
using namespace std;
Square::Square(float a)
{
length = a;
}
Square::~Square()
{
}
double Square::area()const
{
cout << "Square:" << endl;
return length*length;
}
#include "Trapezoid.h"
#include "iostream"
using namespace std;
Trapezoid::Trapezoid(float a,float b,float c)
{
length = a;
width = b;
height = c;
}
Trapezoid::~Trapezoid()
{
}
double Trapezoid::area()const
{
cout << "Trapezoid:" << endl;
return 0.5*height*(length+width);
}
#include "Triangle.h"
#include "iostream"
using namespace std;
Triangle::Triangle(float a,float b)
{
length = a;
height = b;
}
Triangle::~Triangle()
{
}
double Triangle::area()const
{
cout << "Triangle:" << endl;
return 0.5*length*height;
}
main
#include"Circle.h"
#include "Rectange.h"
#include "Shape.h"
#include "Square.h"
#include "Trapezoid.h"
#include "Triangle.h"
#include "iostream"
using namespace std;
void printAera(const Shape *p)
{
cout << p->area() << endl;
}
int main()
{
Circle c1(10);
Rectange r1(5,10);
Square s1(10);
Trapezoid tra1(5,5,10);
Triangle tri2(5,10);
Shape * p[5] = { &c1, &r1, &s1, &tra1, &tri2};
printAera(p[0]);
printAera(p[1]);
printAera(p[2]);
printAera(p[3]);
printAera(p[4]);
double Totalarea=0;
for (int i = 0; i < 5; i++)
{
Totalarea += p[i]->area();
}
cout << "Totalarea:" << endl;
cout << Totalarea << endl;
system("pause");
return 0;
}