求助 虚继承 一点头绪都没有
设计一个许继承类Bsae,该类中包含姓名、年龄两个私有的数据成员以及相关的成员函数,由Base派生处领导类,Leader,包括职务、部门这两个私有数据成员以及相关的成员函数。再由Base派生出工程师类Engineer,包含职称和专业这两个私有的数据成员及相关的成员函数。再由Leader和Engineer这两个类派生出主任工程师Chairman,并测试所设计的类.在建立Leader类和Engineer类声明继续虚基类Base,其他就是普通的类定义啊 已经写完了还是谢谢哈 sidfate 发表于 2014-4-28 09:57 static/image/common/back.gif
在建立Leader类和Engineer类声明继续虚基类Base,其他就是普通的类定义啊
Base里的私有成员 Leader和Engineer继承了后私有成员不可见啊 哪种继承下派生类都不继承基类的私有成员
So?
你在为派生类初始化的时候先调用的是虚基类的构造函数。因此你可以用继承下来的基类的成员函数访问基类的私有成员。 sidfate 发表于 2014-4-28 21:39 static/image/common/back.gif
哪种继承下派生类都不继承基类的私有成员
So?
你在为派生类初始化的时候先调用的是虚基类的构造函数。因 ...
我写了但是私有成员那部分还是不可见用友元也不行呢你方便加我QQ吗方便点我的QQ:512712020 把你写的贴一份出来 sidfate 发表于 2014-4-28 22:05 static/image/common/back.gif
把你写的贴一份出来
#include <iostream>
#include <string>
using namespace std;
class Base
{
friend class Leader;
friend class Engineer;
string name;
int age;
public:
Base(string na,int ag):name(na),age(ag)
{}
void Printf1()
{
cout<<"Base:"<<endl;
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
}
};
class Leader:virtual public Base
{
string job;
string depart;
public:
Leader(string jo,string de,string na,int ag):Base(na,ag)
{
job=jo;
depart=de;
}
void Printf2()
{
cout<<"Leader:"<<endl;
cout<<"job:"<<job<<endl;
cout<<"depart:"<<depart<<endl;
}
};
class Engineer:virtual public Base
{
string profession;
string major;
public:
Engineer(string pro,string ma,string na,int ag):Base(na,ag)
{
profession=pro;
major=ma;
}
void Printf3()
{
cout<<"Engineer:"<<endl;
cout<<"profession:"<<profession<<endl;
cout<<"major:"<<major<<endl;
}
};
class Chairman:public Leader,public Engineer
{
};
int main()
{
Base b1("李",20);
b1.Printf1();
Engineer e1("computer","program","li",20);
e1.Printf3();
return 0;
}
把声明友元的部分去掉吧,没用。
void Printf3()
{
Printf1();
cout<<"Engineer:"<<endl;
cout<<"profession:"<<profession<<endl;
cout<<"major:"<<major<<endl;
} 过来学习学习!,,,,,, sidfate 发表于 2014-4-29 09:53 static/image/common/back.gif
把声明友元的部分去掉吧,没用。
void Printf3()
#include <iostream>
#include <string>
using namespace std;
class Base
{
string name;
int age;
public:
Base(string na,int ag):name(na),age(ag)
{}
void Printf()
{
cout<<"Base:"<<endl;
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
}
};
class Leader:virtual public Base
{
string job;
string depart;
public:
Leader(string jo,string de,string na,int ag):Base(na,ag)
{
job=jo;
depart=de;
}
void Printf()
{
Base::Printf();
cout<<"Leader:"<<endl;
cout<<"job:"<<job<<endl;
cout<<"depart:"<<depart<<endl;
}
};
class Engineer:virtual public Base
{
string profession;
string major;
public:
Engineer(string pro,string ma,string na,int ag):Base(na,ag)
{
profession=pro;
major=ma;
}
void Printf()
{
Base::Printf();
cout<<"Engineer:"<<endl;
cout<<"profession:"<<profession<<endl;
cout<<"major:"<<major<<endl;
}
};
class Chairman:public Leader,public Engineer
{
public:
Chairman(string pro,string ma,string jo,string de,string na,int ag):Base(na,ag),Leader(pro,ma,na,ag),Engineer(jo,de,na,ag)
{
}
void Printf()
{
Leader::Printf();
Engineer::Printf();
}
};
int main()
{
Engineer e1("职业","职务","李",20);
e1.Printf();
Leader l1("职称","部门","李",20);
l1.Printf();
return 0;
}
页:
[1]