由于继承体系极其简单,这边就不解释了,有错误或者不好的地方希望在评论区指出
头文件
#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);
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;
}