<pre class="html" name="code">5 #include<iostream> #include<cmath> using namespace std; class Shape { public: virtual void PrintArea() { } }; //圆 class Circle: public Shape { public: Circle(float r): m_Radius(r) { } virtual void PrintArea() { cout << "Circle:" << 3.1416 * m_Radius * m_Radius << endl; } private: float m_Radius; }; //正方形 class Square: public Shape { public: Square(float a): m_A(a) { } virtual void PrintArea() { cout << "Square:" << m_A * m_A << endl; } private: float m_A; }; //矩形 class Rectangle: public Shape { public: Rectangle(float l, float w): m_Length(l), m_Width(w) { } virtual void PrintArea() { cout << "Rectangle:" << m_Length * m_Width << endl; } private: float m_Length; //长 float m_Width; //宽 }; //梯形 class Trapezoid: public Shape { public: Trapezoid(float a, float b, float h): m_A(a), m_B(b), m_Height(h) { } virtual void PrintArea() { cout << "Trapezoid:" << m_A * m_B * m_Height / 2.0f << endl; } private: float m_A; //上底 float m_B; //下底 float m_Height; //高 }; //三角形 class Triangle: public Shape { public: Triangle(float a, float b, float c): m_A(a), m_B(b), m_C(c) { } virtual void PrintArea() { float p = (m_A + m_B + m_C) / 2.0f; float result = sqrt(p * (p - m_A) * (p - m_B) * (p - m_C)); cout << "Triangle:" << result << endl; } private: float m_A; //边1 float m_B; //边2 float m_C; //边3 }; int main() { Shape *s[5]; s[0] = new Circle(5); s[0] -> PrintArea(); s[1] = new Square(5); s[1] -> PrintArea(); s[2] = new Rectangle(2, 3); s[2] -> PrintArea(); s[3] = new Trapezoid(2, 3, 4); s[3] -> PrintArea(); s[4] = new Triangle(3, 4, 5); s[4] -> PrintArea(); return 0; }
3. #include<iostream> using namespace std; class Point { public: //构造函数 Point(float x = 0, float y = 0): m_X(x), m_Y(y) { } //析构函数 ~Point() { cout << "Executing Point Destructor" << endl; } //virtual ~Point() { cout << "Executing Point Destructor" << endl; } float GetX() { return this -> m_X; } //获取横坐标 float GetY() { return this -> m_Y; } //获取纵坐标 private: int m_X; //横坐标 int m_Y; //纵坐标 }; class Circle: public Point { public: //构造函数 Circle(float x = 0, float y = 0, float r = 1): Point(x, y), m_Radius(r) { } //析构函数 ~Circle() { cout << "Executing Circle Destructor" << endl; } float GetRadius() { return this -> m_Radius; } private: int m_Radius; //半径 }; int main() { Point *p = new Circle(); delete p; cout << "---------" << endl; Circle * gradl = new Circle(); delete gradl; cout << "---------" << endl; return 0; }
修改
#include<iostream> #include<cmath> using namespace std; class Shape { public: virtual void PrintArea() { } }; //圆 class Circle: public Shape { public: Circle(float r): m_Radius(r) { } void PrintArea() { cout << 3.1416 * m_Radius * m_Radius << endl; } private: float m_Radius; }; //矩形 class Rectangle: public Shape { public: Rectangle(float l, float w): m_Length(l), m_Width(w) { } void PrintArea() { cout << m_Length * m_Width << endl; } private: float m_Length; //长 float m_Width; //宽 }; //三角形 class Triangle: public Shape { public: Triangle(float a, float b, float c): m_A(a), m_B(b), m_C(c) { } void PrintArea() { float p = (m_A + m_B + m_C) / 2.0f; float result = sqrt(p * (p - m_A) * (p - m_B) * (p - m_C)); cout << result << endl; } private: float m_A; //边1 float m_B; //边2 float m_C; //边3 }; int main() { (new Circle(5)) -> PrintArea(); (new Rectangle(2, 3)) -> PrintArea(); (new Triangle(3, 4, 5)) -> PrintArea(); return 0; }
4.#include<iostream> using namespace std; //点类 class Point { public: Point(float x = 0, float y = 0); //构造函数 void SetPoint(float x = 0, float y = 0); //为点重新赋值 float GetX(); //获取横坐标 float GetY(); //获取纵坐标 //运算符重载 friend ostream& operator << (ostream &, const Point &); private: float m_X; float m_Y; }; //构造函数 Point :: Point(float a, float b) { this -> m_X = a; this -> m_Y = b; } //为点重新赋值 void Point :: SetPoint(float a, float b) { this -> m_X = a; this -> m_Y = b; } //横坐标 float Point :: GetX() { return this -> m_X; } //纵坐标 float Point :: GetY() { return this -> m_Y; } //重载运算符 ostream& operator << (ostream &output, Point &p) { output << "[" << p.GetX() << "," << p.GetY() << "]"; return output; } //================================================== //圆类 class Circle: public Point { public: Circle(float x, float y, float r): Point(x, y), m_Radius(r) { } //构造函数 float GetRadius() { return this -> m_Radius; } //获取半径 float GetPerimeter() { return 2 * 3.1416f * m_Radius; } //获取周长 float GetArea() { return 3.1416f * m_Radius * m_Radius; } //获取面积 //运算符重载 friend ostream& operator << (ostream &, const Circle &); private: float m_Radius; //半径 }; //重载运算符 ostream& operator << (ostream &output, Circle &c) { output << "[" << c.GetX() << "," << c.GetY() << "] r=" << c.GetRadius(); return output; } //================================================== //圆柱体类 class Cylinder: public Circle { public: Cylinder(float x, float y, float r, float h): Circle(x, y, r), m_Height(h) { } float GetHeight() { return this -> m_Height; } float GetVolume() { return Circle :: GetArea() * m_Height; } //运算符重载 friend ostream& operator << (ostream &, const Cylinder &); private: float m_Height; //高 }; //重载运算符 ostream& operator << (ostream &output, Cylinder &cy) { output << "[" << cy.GetX() << "," << cy.GetY() << "]" << " r=" << cy.GetRadius() << " h=" << cy.GetHeight(); return output; } int main() { Point p(3.5f, 6.4f); cout << p << endl; Circle c(3.5f, 6.4f, 3); cout << c << endl; Cylinder cy(3.5f, 6.4f, 3, 5); cout << cy << endl; return 0; }