-3
我对如何在继承中使用构图感到困惑。基于我的理解,我写了一个简单的例子。请帮助我。
1)如果上述代码正确与否? 2)我的多重继承实现是否正确? 3)我在Employees类中进行继承和组合。那是对的吗 ?如何在C++中使用组合使用多继承?
class address
{
private:
public:
struct MyStruct
{
int houseno;
string colony;
string city;
};
MyStruct m;
//shows the address
void get_address()
{
cout << m.houseno;
cout<<m.colony;
cout << m.city;
}
};
class telephoneNo
{
private:
string c;
public:
// takes the telephoneNo of the employee from user and pass to local variable
void set_telephone(string d)
{
c = d;
}
void get_telephone()
{
cout << c;
}
};
//multiple level inheritance
class employee :public address, public telephoneNo
{
private:
string name;
public:
address obj; //composition
telephoneNo obj2; // composition
void employee_name(string a)
{
name = a;
}
void show_emplyeeDetail()
{
cout << "Employee's Name is: ";
cout << name<<endl;
cout << "Employee's Address is: ";
obj.get_address();
cout<<endl;
cout << "Employee's Telephnoe no is: ";
obj2.get_telephone();
cout<< endl;
}
};
void main()
{
emp.obj;
cout << "Enter Name of employee " << endl;
cin >> nameInput;
cout << "-----------Enter address of employee----------- "<<endl;
cout << "Enter house: ";
cin >> emp.obj.m.houseno; //passing parameters to the struct
cout << "Enter colony : ";
cin >> emp.obj.m.colony;
cout << "Enter city: ";
cin >> emp.obj.m.city;
cout << "Enter telephone of employee: ";
cin >> telephoneInput;
emp.employee_name(nameInput);
emp.obj2.set_telephone(telephoneInput);
cout << "-------------Employee's Details are as follows------------ " << endl;
emp.show_emplyeeDetail();
}
你有问题吗? – 2017-07-06 11:00:33
您需要阅读[良好的C++书](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) –
'class address {private:public:'A whole新的冗长级别。替换这个'struct address {' –