包含子對象的派生類的構造函數

#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(int num,char ch)
{
n=num;
c=ch;
}
void display();
private:   //protected
int n;
char c;
};
void student::display()
{
cout<<"num:"<<n<<endl;
cout<<"ch:"<<c<<endl;
}
class student1:public student
{
public:
student1(int num,char ch,int num1,char ch1,int no,string str):student(num,ch),mm(num1,ch1)
{
a=no;
b=str;
}
void display1();
void mmshow();
private:
student mm;
int a;
string b;
};
void student1::display1()
{
cout<<"這個學生的數據以下:\n";
display();
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
}
void student1::mmshow()
{
cout<<"mm數據以下:\n";
mm.display();
}
int main()
{
student1 nn(55,'m',66,'n',7,"mn");
nn.display1();
nn.mmshow();
return 0;
}
相關文章
相關標籤/搜索