标题:C++构造函数
取消只看楼主
吕孟伟
Rank: 8Rank: 8
等 级:贵宾
威 望:27
帖 子:200
专家分:870
注 册:2018-10-4
结帖率:100%
已结贴  问题点数:20 回复次数:0 
C++构造函数
程序代码:
#include<iostream>
using namespace std;

class Box{
     private:
         double length;
         double breadth;
         double height;
     public:
       Box(double len, double bre, double hei);//这是构造函数。
       Box(){};//请问,这是什么函数?
               //若没有这句,错误有两个,在下方已标出。
               //若没有{},也会出现错误。
       double GetVolume(void);
       Box operator+ (const Box&);
      
};
Box::Box(double len, double bre, double hei){
    length = len;
    breadth = bre;
    height = hei;
}
double Box::GetVolume(void){
    return length * breadth * height;
}
Box Box::operator+ (const Box& obj){
          Box box;//错误一:[Error] no matching function for call to 'Box::Box()'
          box.length = this->length + obj.length;
          box.breadth = this->breadth + obj.breadth;
          box.height = this->height + obj.height;
          return box;
}

int main(void)
{
        Box box1(2.0, 4.0, 6.0);
        Box box2(1.0, 2.0, 3.0);
        Box box3;//错误二:[Error] no matching function for call to 'Box::Box()'
        double Volume = 0.0;
        Volume = box1.GetVolume();
        cout << "BOX1'Volume is " << Volume << endl;
        Volume = box2.GetVolume();
        cout << "BOX2'Volume is " << Volume << endl;
        box3 = box1 + box2;
        Volume = box3.GetVolume();
        cout << "BOX3'Volume is " << Volume << endl;
    return 0;

 } 

搜索更多相关主题的帖子: length double obj 构造函数 Box 
2020-03-21 20:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-500491-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.104295 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved