抽象类案例

头文件

#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;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值