weixin_59789548 2021-06-29 01:27 采纳率: 100%
浏览 16
已结题

求解求解求解。。。。

1、编写一个求立方体体积的函数volume,并编写main函数测试该函数;要求使用函数重载或者函数的参数带有默认值技术使得该函数至少有三种调用形式
2、改造上一题中的长方体类。长方体对象名为rectangle,数据成员包括:length,width,height;成员函数包括但不限定于:构造函数、析构函数、Set函数和GetVolume函数。编写main函数进行测试。
      可使用构造函数重载、有默认参数、参数初始化列表、对象数组、对象指针、const关键字等。运用知识越多分值越高

  • 写回答

1条回答 默认 最新

  • qfl_sdu 2021-06-29 02:21
    关注

    代码如下,如有帮助,请采纳一下,谢谢。

    题目1:

    #include <iostream>
    using namespace std;
    
    class Lifangti
    {
    private:
    	double length;
    public:
    	Lifangti(double l){length = l;}
    	float volume(float ll)
    	{
    		return ll * ll * ll;
    	}
    	int volume(int ll = 1)
    	{
    		return ll*ll*ll;
    	}
    	
    };
    
    int main()
    {
    	Lifangti ls(2);
    	cout << "边长为3的立方体体积=" << ls.volume(3) << endl;
    	cout << "边长为2.0的立方体体积=" << ls.volume((float)1.0) << endl;
    	cout << "边长为默认值的立方体体积为" << ls.volume() << endl;
    }

    题目2:

    #include <iostream>
    using namespace std;
    
    class Rectangle
    {
    private:
    	int length;
    	int width;
    	int height;
    public:
    	Rectangle(){}
    	Rectangle(int l,int w=1,int h=1){length = l;width = w;height = h;}
    	Rectangle( Rectangle &t) 
    	{
    		Set(t.GetLeng(),t.GetWidth(),t.GetHeight());
    	}
    	~Rectangle(){}
    	void Set(int l,int w,int h){length = l;width = w;height = h;}
    	int GetLeng(){return length;}
    	int GetWidth(){return width;}
    	int GetHeight(){return height;}
    	int GetVolume(){return length * width * height;}
    
    	bool operator ==( Rectangle &r)const
    	{
    		if(this->length == r.GetLeng() && this->width == r.GetWidth() && this->height == r.GetHeight())
    			return true;
    		else
    			return false;
    	}
    };
    
    int main()
    {
    	Rectangle* a[2];
    	a[0] = new Rectangle(1,2,3);
    	a[1] = new Rectangle();
    	a[1]->Set(2,2,2);
    	cout << "a[0]:" << a[0]->GetLeng() << "*" << a[0]->GetWidth() << "*" << a[0]->GetHeight()<< endl;
    	cout <<"其体积为" << a[0]->GetVolume() << endl;
    
    	cout << "a[1]:" << a[1]->GetLeng() << "*" << a[1]->GetWidth() << "*" << a[1]->GetHeight()<< endl;
    	cout <<"其体积为" << a[1]->GetVolume() << endl;
    
    
    	delete a[0];
    	delete a[1];
    
    	return 0;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection