2 回答

TA貢獻1807條經驗 獲得超9個贊
當你在類中自定義了帶參數的構造函數,就構成了重載,系統不會調用默認的構造函數,所以要自己定義默認構造函數。除此之外,最后的拷貝構造函數中strlen(c)的使用有誤。代碼修改如下:
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
using namespace std;
class circle
{
double radius;
public:
circle(){}//增加的默認構造函數
circle(double r)
{radius=r;}
double getarea()
{
double s;
s=radius*radius*3.1415926;
return s;
}
};
class table
{
double height;
public:
table(){}//增加的默認構造函數
table(double h)
{height=h;}
double getheight()
{return height;}
};
class roundtable:public circle,public table
{
char *color;
public:
roundtable(double a,double b,char *c=""):circle(a),table(b)
{
color=new char[strlen(c)];
strcpy(color,c);
}
char *getcolor()
{return color;}
~roundtable(){delete []color;}
roundtable(const roundtable&a)
{color=new char[strlen(a.color)];//此處進行了修改
strcpy(color,a.color);};
};
int main()
{
roundtable rt(0.8,1.2,"黑色");
cout<<"圓桌屬性數據:"<<endl;
cout<<"高度:"<<rt.getheight()<<"米"<<endl;
cout<<"面積:"<<rt.getarea()<<"平方米"<<endl;
cout<<"顏色:"<<rt.getcolor()<<endl;
getchar();
return 0;
}
- 2 回答
- 0 關注
- 118 瀏覽
添加回答
舉報