例11.3在派生类中引用保护成员
#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类无公用成员
protected://基类保护成员
int num;
string name;
char sex;
};
class Student1 :protected Student//用protected方式声明派生类Student1
{
public:
void get_value1();
void display1();
private:
int age;
string addr;
};
void Student1::get_value1()//定义派生类公用成员函数
{
cin >> num >> name >> sex;//输入保护基类数据成员
cin >> age >> addr;//输入派生类数据成员
}
void Student1::display1()//定义派生类公用成员函数
{
cout << "num:" << num << endl;//引用基类的保护成员
cout << "name:" <<name << endl;//引用基类的保护成员
cout << "sex:" << sex << endl;//引用基类的保护成员
cout << "age:" << age << endl;//引用派生类的私有成员
cout << "sddress:" <<addr << endl;//引用派生类的私有成员
}
int main()
{
Student1 stud1;//stud1是派生类Student1类对象
stud1.get_value1();//派生类公用成员函数
stud1.display1();
return 0;
}
程序执行效果如图:
11.5定义简单派生类的构造函数;
#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类公用成员
Student(int n,string nam,char s)//基类构造函数
{
num = n;
name = nam;
sex = s;
}
~Student(){}//基类析构函数
protected://基类保护成员
int num;
string name;
char sex;
};
class Student1 :public Student//用public方式声明派生类Student1
{
public:
Student1(int n, string nam, char s, int a, string ad) :Student(n, name, s)
//定义派生类构造函数
{
age = a;
addr = ad;
}
void show()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
cout << "age:" << age << endl;
cout << "sddress:" << addr << endl << endl;
}
~Student1(){}//派生类析构函数
private:
int age;
string addr;
};
int main()
{
Student1 stud1(10010, "Wang-li", 'f',19,"115 Beijing Road,Shanghai");
Student1 stud2(10011, "Zhang-fang", 'm', 21, "213 Shanghai Road,Beijing");
stud1.show();
stud2.show();
return 0;
}
程序执行结果如图:
11.6包含子对象的派生类的构造函数
#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类公用成员
Student(int n,string nam)//基类构造函数
{
num = n;
name = nam;
}
void display()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
protected://基类保护成员
int num;
string name;
};
class Student1 :public Student//用public方式声明派生类Student1
{
public:
Student1(int n, string nam,int n1,string nam1, int a, string ad)
:Student(n, nam), monitor(n1,nam1)
//定义派生类构造函数
{
age = a;
addr = ad;
}
void show()
{
cout << "This student is:" << endl;
display();
cout << "age:" << age << endl;
cout << "address:" << addr << endl << endl;
}
void show_monitor()
{
cout << endl<< "Class monitor is:" << endl;
monitor.display();
}
private:
Student monitor;
int age;
string addr;
};
int main()
{
Student1 stud1(10010, "Wang-li",10001 ,"Li-sun",19,"115 Beijing Road,Shanghai");
stud1.show();
stud1.show_monitor();
return 0;
}
程序执行结果如图:
例11.7多级派生情况下的构造函数
#include <iostream>
using namespace std;
#include<string>
class Student//声明基类
{
public://基类公用成员
Student(int n,string nam)//基类构造函数
{
num = n;
name = nam;
}
void display()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
protected://基类保护成员
int num;
string name;
};
class Student1 :public Student//用public方式声明派生类Student1
{
public:
Student1(int n, string nam, int a)
:Student(n, nam)
//定义直接派生类构造函数
{
age = a;
}
void show()
{
display();
cout << "age:" << age << endl;
}
private:
int age;
};
class Student2 :public Student1//用public方式声明派生类Student2
{
public:
Student2(int n,string nam, int a,int s)
:Student1(n, nam,a)
//定义间接派生类构造函数
{
score=s;
}
void show_all()
{
show();
cout << "score:" << score << endl;
}
private:
int score;
};
int main()
{
Student2 stud1(10010, "Wang-li",17, 89);
stud1.show_all();
return 0;
}
程序执行效果如图: