⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.cpp

📁 该小程序用面向对象的设计方法实现了对长方体表面积和体积的计算。
💻 CPP
字号:
#include <iostream.h>
#include <iomanip.h>

class Cuboid//创建一个长方体类
{
protected:
	int length;//长
	int breadth;//宽
	int high;//高

public:
	Cuboid()//默认的构造函数
	{
		length=breadth=high=3;
	}
	Cuboid(int a,int b,int c)//有参数的的构造函数
	{
		length=a;
		breadth=b;
		high=c;
	}
	virtual void display()//虚显示参数的函数
	{
		cout<<"长方体参数:"<<endl<<"长:"<<length<<"\t"<<"宽:"<<breadth<<"\t"<<"高:"<<high<<endl;
	}

	void cubage()//计算体积的参数
	{
		cout<<"体积:\t"<<length*breadth*high<<endl;
	}

	void surface_area()//计算表面积的参数
	{
		cout<<"表面积:\t"<<2*(length*breadth+length*high+breadth*high)<<endl;
	}
};

//继承长方体类,创建一个正方体类
class Cube : public Cuboid
{
public:
	Cube()//默认的构造函数
	{
		length=breadth=high=5;
	}
	Cube(int b)//有参数的的构造函数
	{
		length=breadth=high=b;
	}
	void display()//重写虚显示参数的函数
	{
		cout<<"正方体参数:"<<endl<<"边长:"<<length<<"\t"<<endl;//也可用宽breadth或高high
	}
};


void main()
{
	Cuboid c1;
	c1.display();
	c1.surface_area();
	c1.cubage();
	cout<<endl;

	Cuboid c2(3,4,5);
	c2.display();
	c2.surface_area();
	c2.cubage();
	cout<<endl;
	
	Cube c3;
	c3.display();
	c3.surface_area();
	c3.cubage();
	cout<<endl;

	Cube c4(3);
	c4.display();
	c4.surface_area();
	c4.cubage();
	cout<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -