c++实训课

程序一:

#include <iostream>
#include <stdlib.h>
#define COST 20
using namespace std;

class tiji //体积
{
private:
double length, width, height; //长宽高
public:
tiji() {}; //默认的构造函数
tiji(double l, double w, double h) //构造函数的形参初始化私有变量
{
length = l;
width = w;
height = h;
}
double changf_tiji() //求长方体的体积函数
{
double v = length * width * height;
return v;
}

double biao_area()
{
double s;
s = 2 * (length * width + length * height * width * height);
return s;
}

double repaie()
{
double ss;
ss = 2 * (length * width + length * height * width * height) - length * width; //除地板以外的面积
return ss;
}

void input(); //输入数据的函数
};

void tiji::input()
{
cin >> length >> width >> height;
}

void menu()
{
cout << "*******************************" << endl;
cout << " 1. 求立方体的体积" << endl;
cout << " 2. 求立方体的表面积" << endl;
cout << " 3. 求立方体的装修造价" << endl;
cout << " 0. 退出" << endl;
cout << "*******************************" << endl;
}

void _exit()
{
cout << "*******************************" << endl;
cout << " 欢迎使用本程序" << endl;
cout << endl;
cout << " 谢谢" << endl;
cout << "*******************************" << endl;
}
//主函数
int main()
{
tiji B; //调用默认构造函数
int c;
menu();
while (1)
{
cout << "请输入长、宽、高的值:";
B.input();
cout << "请选择:";
cin >> c;
switch (c)
{
case 1:
cout << " 1.立方体的体积:" << B.changf_tiji() << endl;
break;
case 2:
cout << " 2.立方体的表面积:" << B.biao_area() << endl;
break;
case 3:
cout << " 3.立方体的装修造价:" << B.repaie() << endl;
break;
case 0:
system("cls");
_exit();
exit(1);
break;
default:
cout << "无此功能!";
}
}
}

程序二:

#include <iostream>
using namespace std;
const double PI = 3.1415926;

//定义抽象类
class shape //图形类
{
public:
virtual double area() = 0; //纯虚函数
};

//定义圆的子类
class circle :public shape
{
private:
double radius;
public:
circle(double r)
{
radius = r;
}
double area()
{
double s;
s = PI * radius * radius; //求圆面积
return s;
}
};

//定义梯形的子类
class trapezoid:public shape
{
private:
double top, bottom, height;
public:
trapezoid(double t, double b, double h) //构造函数的参数初始化私有变量
{
top = t;
bottom = b;
height = h;
}
double area()
{
double s1;
s1 = (top + bottom) * height / 2;
return s1;
}
};

//定义三角形的子类
class triangle :public shape
{
private:
double bottom, height;
public:
triangle(double b, double h)
{
bottom = b;
height = h;
}
double area()
{
double s2;
s2 = bottom * height / 2;
return s2;
}
};

//主函数
int main()
{
shape* p[3]; //shape是抽象类,不能实例化
p[0] = new circle(3); //new是分配内存空间
p[1] = new trapezoid(4, 6, 5);
p[2] = new triangle(5, 8);
for (int i = 0; i < 3; i++)
{
cout << "p[" << i << "]=" << p[i]->area() << endl;
}
double sum = 0.0;
for (int i = 0; i < 3; i++)
sum += p[i]->area();

cout << "三个图形的面积之和是:" << sum << endl;
return 0;
}

 

程序三:

#include <iostream>
#include <stdlib.h>
using namespace std;
const double PI = 3.1415926;

//基类的定义
class Point
{
private:
int x, y; //横坐标和纵坐标
public:
Point(int xx, int yy) //构造函数
{
x = xx;
y = yy;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
friend ostream &operator<<(ostream&, const Point&); //友元函数
};

ostream& operator<<(ostream& show, const Point& p)
{
show << "(" << p.x << "," << p.y << ")" << endl;
return show;
}

//定义圆的子类
class Circle:public Point
{
protected:
int r;
public:
Circle(int xx, int yy, int rr) :Point(xx, yy) //这里充分说明了圆是点的子类
{
r = rr;
}
int Getr()
{
return r;
}
double circumferen() const //常函数
{
return 2 * PI * r;
}
double area() const
{
return PI * r * r;
}
friend ostream& operator<<(ostream&, const Circle&); //声音友元函数
};

ostream& operator<<(ostream& show, const Circle& c)
{
show << ",半径" << "c.r" << ",周长:" << c.circumferen() << endl;

return show;
}

class Cylinder :public Circle
{
private:
int h;
public:
Cylinder(int xx = 0, int yy = 0, int rr = 0, int hh = 0) :Circle(xx, yy, rr)
{
h = hh;
}
int Geth()
{
return h;
}
double Cy_area() const
{
return 2 * Circle::area() + 2 * PI * r * h;
}

double volume() const
{
return Circle::area() * h;
}
friend ostream& operator<<(ostream&, const Cylinder&);
};

ostream& operator<<(ostream& show, const Cylinder& cy)
{
show << "半径" << cy.r << "表面积:" << cy.Cy_area() << ",体积:" << cy.volume() << endl;
return show;
}

int main()
{
Point objP(3,5);
cout << "横坐标:" << objP.Getx() << " ";
cout << "纵坐标:" << objP.Gety() <<endl;

Circle objC(3,5,9);
cout << "圆的半径:" << objC.Getr() << endl;
cout << "圆的周长:" << objC.circumferen() << endl;
cout << "圆的面积:" << objC.area() << endl;

Cylinder objCy(3,5,9,12);
cout << "圆柱的表面积:" << objCy.Cy_area() << endl;
cout << "圆柱的体积:" << objCy.volume() << endl;
return 1;
}

posted @   bobo哥  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示