编写程序,写一个Shape类,该类有求表面积(get_surface)和体积(get_volume)两个虚函数;由该类派生出圆柱体Cylinder,球体(Sphere),正方体(Cube),计算圆柱体,球体,正方体的表面积和体积。
类图如下:
头文如下:
#pragma once
class Shape //Shape类
{
public:
virtual float get_surface(); //返回表面积的值
virtual float get_volume();//返回体积的值
virtual void set_input();//给几何图形的基本属性赋值
virtual void display();//输出几何图形的属性
Shape();//构造函数
~Shape();//析构函数
};
class Cylinder :public Shape//圆柱体类
{
private:
float r;//半径
float h;//高
public:
Cylinder();//构造函数
void set_input();//给几何图形的基本属性赋值
float get_surface();//返回表面积的值
float get_volume();//返回体积的值
void display();//输出几何图形的属性
~Cylinder();//析构函数
};
//球体类
class Sphere :public Shape
{
private:
float r;//半径
public:
Sphere();//构造函数
void set_input();//给几何图形的基本属性赋值
float get_surface();//返回表面积的值
float get_volume();//返回体积的值
void display();//输出几何图形的属性
~Sphere();//析构函数
};
//正方体类
class Cube :public Shape
{
private:
float d;//边长
public:
Cube();//构造函数
void set_input();//给几何图形的基本属性赋值
float get_surface();//返回表面积的值
float get_volume();//返回体积的值
void display();//输出几何图形的属性
~Cube();//析构函数
};
class Select //选择的类
{
public:
Select();//构造函数
void select_type(Shape* shape);//调用哪个类
~Select();//析构函数
};
main如下:
#include<iostream>
using namespace std;
#include "m.h"
//Shape类
float Shape::get_surface()
{
return 0.0;
}
float Shape::get_volume()
{
return 0.0;
}
void Shape::set_input()
{
cout << "NULL" << endl;
}
void Shape::display()
{
cout << "NULL" << endl;
}
Shape::Shape()
{
cout << "Shape 构造函数" << endl;
}
Shape:: ~Shape()
{
cout << "Shape 析构函数" << endl;
}
//圆柱体
Cylinder::Cylinder()
{
r = 0.0;
h = 0.0;
}
void Cylinder::set_input()
{
cout << "---------输入---------------" << endl;
cout << "圆柱体的底面半径:";
cin >> r;
cout << "圆柱体的高:";
cin >> h;
}
float Cylinder::get_surface()
{
return 2 * 3.14 * r * h + 2 * 3.14 * r * r;
}
float Cylinder::get_volume()
{
return 3.14 * r * r * h;
}
void Cylinder::display()
{
cout <<"---------输出---------------"<< endl;
cout << "圆柱体的底面半径:" << r << endl;
cout << "圆柱体的高:" << h << endl;
cout << "圆柱体的表面积:" << get_surface() << endl;
cout << "圆柱体的体积:" << get_volume() << endl;
cout << "---------------------------" << endl<< endl;
}
Cylinder::~Cylinder()
{
cout << "Cylinder 析构函数" << endl;
}
//球体
Sphere::Sphere()
{
r = 0.0;
}
void Sphere::set_input()
{
cout << "---------输入---------------" << endl;
cout << "球体的半径:";
cin >> r;
}
float Sphere::get_surface()
{
return 4 * 3.14 * r * r;
}
float Sphere::get_volume()
{
return (4.0 / 3) * 3.14 * r * r * r;
}
void Sphere::display()
{
cout << "---------输出---------------" << endl;
cout << "球体的半径:" << r << endl;
cout << "球体的表面积:" << get_surface() << endl;
cout << "球体的体积:" << get_volume() << endl;
cout << "----------------------------" << endl << endl;
}
Sphere::~Sphere()
{
cout << "Sphere 析构函数" << endl;
}
//正方体
Cube::Cube()
{
d = 0.0;
}
void Cube::set_input()
{
cout << "---------输入---------------" << endl;
cout << "正方体的边长:";
cin >> d;
}
float Cube::get_surface()
{
return 6 * d * d;
}
float Cube::get_volume()
{
return d * d * d;
}
void Cube::display()
{
cout << "---------输出---------------" << endl;
cout << "正方体的边长:" << d << endl;
cout << "正方体的表面积:" << get_surface() << endl;
cout << "正方体的体积:" << get_volume() << endl;
cout << "----------------------------" << endl << endl;
}
Cube::~Cube()
{
cout << "Cube 析构函数" << endl;
}
void Select:: select_type(Shape* shape)
{
shape->set_input();
shape->display();
}
Select::Select()
{
cout << "Select类构造函数" << endl;
}
Select::~Select()
{
cout << "Select类虚构函数" << endl;
}
int main()
{
Shape* sh;
Select se;
int n;
cout << "请问你想计算哪种几何图形" << endl;
cout << "1--圆柱体" << endl;
cout << "2--球体" << endl;
cout << "3--正方体" << endl;
cout << "你的选择是:";
cin >> n;
switch (n)
{
case 1:
{
Cylinder c;
sh = &c;
se.select_type(sh);
break;
}
case 2:
{
Sphere s;
sh = &s;
se.select_type(sh);
break;
}
case 3:
{
Cube cu;
sh = &cu;
se.select_type(sh);
break;
}
default:
break;
}
return 0;
}
代码仅供参考