在c++继承中A为基类,B为共有继承A的派生类,C为共有继承B的派生类。那么A的构造函数将成为B的
在c++继承中A为基类,B为共有继承A的派生类,C为共有继承B的派生类。那么A的构造函数将成为B的公有成员,而B的共有成员将成为C的公有成员函数,那为什么C的构造函数不能...
在c++继承中A为基类,B为共有继承A的派生类,C为共有继承B的派生类。那么A的构造函数将成为B的公有成员,而B的共有成员将成为C的公有成员函数,那为什么C的构造函数不能直接调用A的构造函数。书上为什么这么说我觉得不对啊!
展开
3个回答
展开全部
//看14.10的代码
//上面的例子
#include <iostream>
using std::endl;
using std::cout;
class A{
public:
A(int a) : _a(a){
cout << "I'm A:" << endl;
}
private:
int _a;
};
class B : public A{
public:
B( int a, int b) : A(a), _b(b){
cout << "I'm B:" << endl;
}
private:
int _b;
};
class C : public B{
public:
C(int a, int b, int c) : /*A(a), /(ㄒoㄒ)/~~真不知道这里你要怎么加*/
B(a, b), _c(c){
cout << "I'm C:" << endl;
}
private:
int _c;
};
int
main(void){
C a(1,2,3);
}
//output
I'm A:
I'm B:
I'm C:
//--multInherige-----------------------------------
#include<iostream>
using namespace std;
class worker{
public:
worker(int base) : _base(base){
cout << "I'm worker" << endl;
}
private:
int _base;
};
class singer : virtual public worker{
public:
singer(int base, int sing) : worker(base), _sing(sing){
cout << "I'm singer" << endl;
}
private:
int _sing;
};
class waiter : virtual public worker{
public:
waiter(int base, int server) : worker(base), _server(server){
cout << "I'm server" << endl;
}
private:
int _server;
};
class singerAndwaiter: public waiter,public singer{
public:
singerAndwaiter(int base, int server, int sing, int what)
: worker(base), waiter(base, server), singer(base, sing), _what(what){
cout << "I'm mix" << endl;
}
private:
int _what;
};
int
main(void){
singerAndwaiter A(1,2,3,4);
return 0;
}
//上面的例子
#include <iostream>
using std::endl;
using std::cout;
class A{
public:
A(int a) : _a(a){
cout << "I'm A:" << endl;
}
private:
int _a;
};
class B : public A{
public:
B( int a, int b) : A(a), _b(b){
cout << "I'm B:" << endl;
}
private:
int _b;
};
class C : public B{
public:
C(int a, int b, int c) : /*A(a), /(ㄒoㄒ)/~~真不知道这里你要怎么加*/
B(a, b), _c(c){
cout << "I'm C:" << endl;
}
private:
int _c;
};
int
main(void){
C a(1,2,3);
}
//output
I'm A:
I'm B:
I'm C:
//--multInherige-----------------------------------
#include<iostream>
using namespace std;
class worker{
public:
worker(int base) : _base(base){
cout << "I'm worker" << endl;
}
private:
int _base;
};
class singer : virtual public worker{
public:
singer(int base, int sing) : worker(base), _sing(sing){
cout << "I'm singer" << endl;
}
private:
int _sing;
};
class waiter : virtual public worker{
public:
waiter(int base, int server) : worker(base), _server(server){
cout << "I'm server" << endl;
}
private:
int _server;
};
class singerAndwaiter: public waiter,public singer{
public:
singerAndwaiter(int base, int server, int sing, int what)
: worker(base), waiter(base, server), singer(base, sing), _what(what){
cout << "I'm mix" << endl;
}
private:
int _what;
};
int
main(void){
singerAndwaiter A(1,2,3,4);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
做个实验运行下,不行的话就说明不允许了, 约定使然。
追问
我要是能做实验还会提问吗,你这回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询