64,639
社区成员
发帖
与我相关
我的任务
分享
#include <string>
class Item{
public:
Item(const std::string& book=" "):isbn(book),sold(0){}//问题出在string& book=" "这里,在调用时形式上是和Item()一样的,因为你指定了默认值
//改成Item(const std::string& book):isbn(book),sold(0){}就可以了
Item(){}//这才是默认构造函数
private:
std::string isbn;
int sold;
};
#include <iostream>
using namespace std;
class A{
public:
A(int n);
int i;
};
A::A(int n){
i = n;
}
class B: public A{
public:
int m;
};
int main()
{
B b;//无法编译,提示未找A的默认构造函数
cout << b.m << endl;
return 0;
}
#include <iostream>
using namespace std;
class A{
public:
A();//可以不去实现,编译不会出错,但是生成程序时连接会出错
A(int n);
int i;
};
A::A(){
i = 1;
}
A::A(int n){
i = n;
}
class B: public A{
public:
int m;
};
int main()
{
B b;
cout << b.m << endl;
return 0;
}
item();//默认构造函数
item(int i);
item(int i, int n);
item(char c);
//可以同时存在