C++Primer第十五章15.5学习笔记(练习15.21组成图形基元的继承体系)

由于继承体系极其简单,这边就不解释了,有错误或者不好的地方希望在评论区指出

头文件

#pragma once
#ifndef GRAPHIC_H
#define GRAPHIC_H
#include<string>
#include<iostream>
#include<math.h>
using std::string;
using std::cout;
using std::endl;
#define PI acos(-1);//3.1415926
//图形基类
class Graphic {
public:
	Graphic()=default;
	Graphic(const string& n) :name(n) {}
	virtual void get_attribute()const {
		cout << "class:"<<name << endl;
	}
	~Graphic() = default;
private:
	string name;
};
//二维图形抽象基类
class TwoDeminsion :public Graphic{
public:
	TwoDeminsion() {};
	TwoDeminsion(const string& name):Graphic(name){}
	virtual ~TwoDeminsion() {};
	virtual double get_area()const = 0;//获取面积
};
//三维图形抽象基类
class ThreeDeminsion :public TwoDeminsion {
public:
public:
	ThreeDeminsion() = default;
	ThreeDeminsion(const string& n) :TwoDeminsion(n) {}
	virtual double get_volume()const = 0;//获取体积
	virtual ~ThreeDeminsion() {}
};
//二维圆类
class Circle :public TwoDeminsion {
public:
	Circle() = default;
	Circle(double r) :TwoDeminsion("Circle"), radius(r) {};
	~Circle() = default;
	void get_attribute()const override {
		this->Graphic::get_attribute();
		cout << "area:" << get_area() << endl;;
	}
	double get_area()const override {
		return radius * radius* PI;
	}
protected:
	double radius=0;
};
//二维矩形类
class Rectangle :public TwoDeminsion {
public:
	Rectangle() = default;
	Rectangle(double l=0, double w=0, const string& name = "Rectangle") : TwoDeminsion(name),length(l), width(w) {}
	virtual ~Rectangle() {};
	void get_attribute()const override {
		this->Graphic::get_attribute();
		cout << "area:" << get_area() << endl;;
	}
	double get_area()const override {
		return length * width;
	}
protected:
	double length = 0, width = 0;

};
//正方形类
class Square :public Rectangle {
public:	
	Square() = default;
	Square(double s) :Rectangle( s, s, "Square") {}
	virtual ~Square() {};
};

//三维与圆有关的抽象类,例如圆锥,圆柱,球
class ThreeDeminsionOfCircle : public ThreeDeminsion{
public:
	ThreeDeminsionOfCircle() = default;
	ThreeDeminsionOfCircle(double r, const string& n) :ThreeDeminsion(n),radius(r) {}
	virtual ~ThreeDeminsionOfCircle() {}
protected:
	double radius=0;
};

//球体
class Sphere:public ThreeDeminsionOfCircle {
public:
	Sphere() = default;
	Sphere(double r):ThreeDeminsionOfCircle( r, "Sphere") {}
	virtual ~Sphere(){}
	void get_attribute()const override{
		this->Graphic::get_attribute();
		cout << "area:" << get_area() << endl;;
		cout << "volume:" << get_volume() << endl;
	}
	double get_volume()const override {
		return get_area()/3*radius;
	}
	double get_area()const override {
		return 4 * radius * radius*PI;
	}
};
//圆柱体
class Cylinder :public ThreeDeminsionOfCircle {
public:
	Cylinder() = default;
	Cylinder(double r, double h):ThreeDeminsionOfCircle( r,"Cylinder"), height(h) {};
	virtual ~Cylinder() {}
	double get_area()const override {
		return height * radius * radius * PI;
	}
	double get_volume()const override {
		return get_area() * height;
	}
	void get_attribute()const override {
		this->Graphic::get_attribute();
		cout << "area:" << get_area() << endl;
		cout << "volume:" << get_volume() << endl;
	}
protected:
	double height=0;
};
//六面体类
class Hexahedron:public ThreeDeminsion {
public:
	Hexahedron() = default;
	Hexahedron(double a = 0, double b = 0, double c = 0, const string& name = "六面体") :ThreeDeminsion(name), x(a), y(b), z(c) {}
	virtual ~Hexahedron() {}
	double get_area()const override {
		return (x * y + x * z + y * z) * 2;
	}
	double get_volume()const override {
		return x * y * z;
	}
	void get_attribute()const {
		this->Graphic::get_attribute();
		cout << "area:" << get_area() << endl;
		cout << "volume:" << get_volume() << endl;
	}
protected :
	double x = 0, y = 0, z = 0;
};
//正方体类
class Cube :public Hexahedron {
public:
	Cube() = default;
	Cube(double d):Hexahedron(d,d,d,"正六面体"){}
};

#endif // !

测试代码

#include"graphic.h"
using namespace std;
int main(int arc, char** argv) {
	Circle c(3);//圆类
	c.get_attribute();
	cout << endl;
	Rectangle r(2, 3);//矩形类
	r.get_attribute();
	cout << endl;
	Square s(4);//正方形类
	s.get_attribute();
	cout << endl;
	Sphere sphere(4);//球类
	sphere.get_attribute();
	cout << endl;
	Cylinder cylinder(4, 2);//圆柱体类
	cylinder.get_attribute(); 
	cout << endl;
	Hexahedron hexahedron(1,2,3);//六面体类
	hexahedron.get_attribute();
	cout << endl;
	Cube cube(2);//立方体类
	cube.get_attribute();
	return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值