3 回答

TA贡献1810条经验 获得超4个赞
之所以要提供没有参数的构造函数或给参数赋予初始值,是因为定义或动态分配对象数组时需要调用一个没有参数的构造函数,由于CShape类内部给出了一个带参数的构造函数,因而编译器不会自动给类生成一个默认构造函数,所以创建对象数组时会出错。给参数赋予初值的意思是,如果某个参数没有提供值将会使用默认值,否则使用实参传递过来的值。

TA贡献1824条经验 获得超8个赞
no appropriate default constructor available
是没有默认的构造函数,不是构造参数
CShape(){}
可以连带,继承的属性不能设置成保护
public:
int location;
#include <stdio.h>
#include <iostream>
using namespace std;
class CShape
{
public:
CShape() {}
CShape(int pre_location) { location=pre_location; }
~CShape() {}
virtual void display() =0;
char randomchar();
public:
int location;
};
class CTriangle: public CShape
{
public:
CTriangle(int pre_height, int pre_location): CShape(pre_location) { height=pre_height; }
~CTriangle() {}
virtual void display()=0;
private:
int height;
};
class CReserveTriangle: public CTriangle
{
public:
CReserveTriangle(int pre_height, int pre_location): CTriangle(pre_height, pre_location) {}
~CReserveTriangle() {}
virtual void display();
protected:
int height;
};
void CReserveTriangle::display()
{
}
void main( )
{
CReserveTriangle crt(1,2);
cout<< crt.location<<endl;
}

TA贡献1757条经验 获得超7个赞
class CShape
{
public:
CShape(int pre_location) { location=pre_location; }
CShape(){}/////////////////////////////add this line
~CShape() {}
virtual void display() =0;
char randomchar();
protected:
int location;
};
添加回答
举报