谭浩强版C++实例 第五章 继承与派生(10个)


//访问公有基类的成员

#include 

#include 

using namespace std;

class Student

{

private:

int num;

string name;

char sex;

public:

void get_value()

{

cin>>num>>name>>sex;

}

void display()

{

cout<<"num:"<

cout<<"name:"<

cout<<"sex:"<

}

};

class Student1:public Student

{

private:

int age;

string addr;

public:

Student1()

{

age=0;

addr="beijing";

}

void display1()

{

cout<<"age:"<

cout<<"address:"<

}

};

int main()

{

Student1 stud;

stud.get_value();

stud.display();

stud.display1();

return 0;

}

//5.2访问私有基类的成员

#include 

#include 

using namespace std;

class Student

{

private:

int num;

string name;

char sex;

public:

void get_value()

{

cin>>num>>name>>sex;

}

void display()

{

cout<<"num:"<

cout<<"name:"<

cout<<"sex:"<

}

};

class Student1:private Student

{

private:

int age;

string addr;

public:

Student1()

{

age=0;

addr="beijing";

}

void display1()

{

cout<<"age:"<

cout<<"address:"<

}

};

int main()

{

Student1 stud;

//stud.get_value();//私有继承;这样引用是错误的

//stud.display();//私有继承;这样引用是错误的

stud.display1();

return 0;

}

//5.3在派生类中引用保护成员

#include 

#include 

using namespace std;

class Student 

{

protected:

int num;

string name;

char sex;

public:

void display();

};

void Student::display()

{

cout<<"num:"<

cout<<"name:"<

cout<<"sex:"<

}

class Student1:protected Student

{

private:

int age;

string addr;

public:

void display1();

};

void Student1::display1()

{

cout<<"num:"<

cout<<"name:"<

cout<<"sex:"<

cout<<"age:"<

cout<<"address:"<

}

int main()

{

Student1 stud1;

stud1.display1();

//stud1.num=10023;//错误;外界不能访问保护成员

return 0;

}

//5.4多级派生的访问属性

class A

{

public:

int i;

protected:

void f2();

int j;

private:

int k;

};

class B:public A

{

public:

void f3();

protected:

void f4();

private:

int m;

};

 

class C:protected B

{

public:

void f5();

private:

int n;

};

//5.5简单的派生类的构造函数

#include 

#include 

using namespace std;

class Student

{

protected:

int num;

string name;

char sex;

public:

Student(int n,string nam,char s)

{

num=n;

name=nam;

sex=s;

}

~Student(){}

};

class Student1:public Student

{

private:

int age;

string addr;

public:

Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s)//派生类构造函数

{

age=a;

addr=ad;

}

void show()

{

cout<<"num:"<

cout<<"name:"<

cout<<"sex:"<

cout<<"age:"<

cout<<"address:"<

}

~Student1(){}

};

int main()

{

Student1 stud1(10010,"wang_li",'f',19,"115 Beijing Road,Shanghai");

Student1 stud2(10011,"zhang_feng",'m',20,"213 Shanghai Road ,Beijing");

stud1.show();

stud2.show();

return 0;

}

//5.6包含子对象的派生类的构造函数

#include 

#include 

using namespace std;

class Student

{

protected:

int num;

string name;

public:

Student(int n,string nam)

{

num=n;

name=nam;

}

void display()

{

cout<<"num:"<

cout<<"name:"<

}

};

 

class Student1:public Student

{

private:

Student monitor;//子对象

int age;

string address;

public:

Student1(int n,string nam,int n1,string nam1,int a,string add):Student(n,nam),monitor(n1,nam1)

{

age=a;

address=add;

}

void show()

{

cout<<"This student is:"<

display();

cout<<"age:"<

cout<<"address:"<

}

void show_monitor()

{

cout<

cout<<"Class monitor is:"<

monitor.display();

}

};

int main()

{

Student1 stud1(10010,"wang li",10011,"li sun",

19,"changchungongyedaxue,Changchun");

stud1.show();

stud1.show_monitor();

return 0;

}

//5.7多级派生情况下派生类的构造函数

#include 

#include 

using namespace std;

class Student

{

protected:

int num;

string name;

public:

Student(int n,string nam)

{

num=n;

name=nam;

}

void display()

{

cout<<"num:"<

cout<<"name:"<

}

};

class Student1:public Student

{

public:

Student1(int n,string nam,int a):Student(n,nam)

{

age=a;

}

void show()

{

    display();

cout<<"age:"<

}

private:

int age;

};

class Student2:public Student1

{

public:

Student2(int n,string nam,int a,int s):Student1(n,nam,a)

{

score=s;

}

void show_all()

{

show();//课本上没有void,要是加上void就不输出前面的数据了

cout<<"score:"<

}

private:

int score;

};

int main()

{

Student2 stud(10010,"wangli",17,89);

stud.show_all();

return 0;

}

//5.8声明教师(Teacher)类和学生(Student)类,用多重继承的方式声明一个研究生(Graduate)派生类。教师类中包含数据成员

//name(姓名)、age(年龄)、title(职称)。学生类中包括数据成员name1(姓名)、sex(性别)、score(成绩)。在定义

//派生类时给出初始化的数据;然后输出这些数据

//此程序没有调通,可能是编译器的问题

#include 

#include 

using namespace std;

class Teacher

{

public:

Teacher(string nam,int a,string t)

{

name=nam;

age=a;

title=t;

}

void display()

{

cout<<"name:"<

cout<<"age:"<

cout<<"title:"<

}

protected:

string name;

int age;

string title;

};

class Student

{

public:

Student(char nam[],char s,float sco)

{

strcpy(name1,nam);//此处有问题

sex=s;

score=sco;

}

void display1()

{

cout<<"name:"<

cout<<"sex:"<

cout<<"score:"<

}

protected:

string name1;

char sex;

float score;

};

 

class Graduate:public Teacher,public Student

{

public:

Graduate(string nam,int a,char s,string t,float sco,float w):Teacher(nam,a,t),

Student(nam,s,sco),wage(w){}

void show()

{

cout<<"name:"<

cout<<"age:"<

cout<<"sex:"<

cout<<"score:"<

cout<<"title:"<

cout<<"wage:"<

}

private:

float wage;

};

int main()

{

Graduate grad("wangli",23,'f',"assistant",89.5,1234.5);

grad.show();

return 0;

}

//5.8.1声明教师(Teacher)类和学生(Student)类,用多重继承的方式声明一个研究生(Graduate)派生类。教师类中包含数据成员

//name(姓名)、age(年龄)、title(职称)。学生类中包括数据成员name1(姓名)、sex(性别)、score(成绩)。在定义

//派生类时给出初始化的数据;然后输出这些数据

//此程序没有调通,可能是编译器的问题,修改以后了,可以调通了

#include 

#include 

using namespace std;

class Teacher

{

public:

Teacher(string nam,int a,string t)

{

name=nam;

age=a;

title=t;

}

void display()

{

cout<<"name:"<

cout<<"age:"<

cout<<"title:"<

}

protected:

string name;

int age;

string title;

};

class Student

{

public:

Student(string nam,char s,float sco)

//源程序:Student (char nam[],char s,float sco)

{

name1=nam;//源程序是strcpy(name1,nam)

sex=s;

score=sco;

}

void display1()

{

cout<<"name:"<

cout<<"sex:"<

cout<<"score:"<

}

protected:

string name1;

char sex;

float score;

};

 

class Graduate:public Teacher,public Student

{

public:

Graduate(string nam,int a,char s,string t,float sco,float w):Teacher(nam,a,t),

Student(nam,s,sco),wage(w){}

void show()

{

cout<<"name:"<

cout<<"age:"<

cout<<"sex:"<

cout<<"score:"<

cout<<"title:"<

cout<<"wage:"<

}

private:

float wage;

};

int main()

{

Graduate grad("wangli",23,'f',"assistant",89.5,1234.5);

grad.show();

return 0;

}

//5.9Teacher类和Student类之上增加一个共同的基类Person

//作为人员的一些基础数据放在Person中;在Teacher类和Student

//在增加一些必要的数据

#include 

#include 

using namespace std;

class Person

{

protected:

string name;

char sex;

int age;

public:

Person(string nam,char s,int a)

{

name=nam;

sex=s;

age=a;

}

};

class Teacher:virtual public Person

{

public:

Teacher(string nam,char s,int a,string t):Person(nam,s,a){title=t;}

protected:

string title;

};

class Student:virtual public Person

{

public:

Student(string nam,char s,int a,float sco):Person(nam,s,a),score(sco){}

protected:

float score;

};

class Graduate:public Teacher,public Student

{

public:

Graduate(string nam,char s,int a,string t,float sco,float w):Person(nam,s,a),

Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){}

void show()

{

cout<<"name:"<

cout<<"age:"<

cout<<"sex:"<

cout<<"score:"<

cout<<"title:"<

cout<<"wages:"<

}

private:

float wage;

};

int main()

{

Graduate grad("wang-li",'f',24,"assistant",89.3,1234.5);

grad.show();

return 0;

}

 

//5.10定义一个基类Student(学生),再定义Student类的公用派生类Graduate

//(研究生),用指向基类对象的指针输出数据

#include 

#include 

using namespace std;

class Student

{

private:

int num;

string name;

float score;

public:

Student(int ,string ,float);

void display();

};

Student::Student(int n,string nam,float s)

{

num=n;

name=nam;

score=s;

}

void Student::display()

{

cout<

cout<<"num:"<

cout<<"name:"<

cout<<"score:"<

}

class Graduate:public Student

{

public:

Graduate(int ,string,float,float);

void display();

private:

float pay;

};

Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p){}

void Graduate::display()

{

Student::display();

cout<<"pay="<

}

int main()

{

Student stud(1001,"Li",87.5);

Graduate grad(2001,"wang",98.5,5599.8);

Student *pt=&stud;

pt->display();

pt=&grad;

pt->display();

return 0;

}

你可能感兴趣的:(C/C++)