📄 poly.cpp
字号:
// Poly.cpp
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
//---- 宣告类别 Shape --------
class Shape
{
private:
int i;
public:
Shape(): i(7){}
~Shape(){}
virtual void Draw() {cout<<"画个图\n";}
virtual void Erase() {cout<<"将图清除\n";}
};
//---- 宣告类别 Circle--------
class Circle : public Shape
{
private:
int r;
public:
Circle(): r(5) {}
Circle(int N): r(N) {}
~Circle() {}
void Draw() {cout<<"画一个圆形\n";}
void Erase() {cout<<"把圆形清除\n";}
};
//---- 宣告类别 Square --------
class Square : public Shape
{
private:
int a;
public:
Square(): a(2) {}
Square(int N): a(N) {}
~Square() {}
void Draw() {cout<<"画一个正方形\n";}
void Erase() {cout<<"把正方形清除\n";}
};
//---- 宣告类别 Triangle --------
class Triangle : public Shape
{
private:
int a, b, c;
public:
Triangle(): a(1), b(1), c(1) {}
Triangle(int L, int M, int N): a(L), b(M), c(N) {}
~Triangle() {}
void Draw() {cout<<"画一个三角形\n";}
void Erase() {cout<<"把三角形清除\n";}
};
//---- 宣告类别 Cylinder--------
class Cylinder : public Circle
{
private:
int r, h;
public:
Cylinder(): r(5), h(1) {}
Cylinder(int M, int N): r(M), h(N) {}
~Cylinder() {}
void Draw() {cout<<"画一个圆柱形\n";}
void Erase() {cout<<"把圆柱形清除\n";}
};
void Make(Shape &S1) {S1.Draw();}
void Remove(Shape *pS) {pS->Erase();}
// ----主程式---------------------------
main()
{
Circle C1;
Cylinder CyL;
Triangle T2;
Square Sq3;
cout << "T2 有 "
<< sizeof(T2)/sizeof(int)
<< " 个 int." << endl;
cout << "CyL 有 "
<< sizeof(CyL)/sizeof(int)
<< " 个 int." << endl;
cout << "“Make(C1))” : ";
Make(C1);
cout << "“Make(CyL))”: ";
Make(CyL);
cout << "“Make(T2))” : ";
Make(T2);
cout << "“Make(Sq3))”: ";
Make(Sq3);
cout << "执行 “pS=&C1” 之后: " << endl;
Shape *pS=&C1;
cout << "“Remove(pS)”: ";
Remove(pS);
cout << "执行 “pS=&CyL” 之后: " << endl;
pS=&CyL;
cout << "“Remove(pS)”: ";
Remove(pS);
cout << "执行 “pS=&T2” 之后: " << endl;
pS=&T2;
cout << "“Remove(pS)”: ";
Remove(pS);
cout << "执行 “pS=&Sq3” 之后: " << endl;
pS=&Sq3;
cout << "“Remove(pS)”: ";
Remove(pS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -