例子5.1p160
//访问公有基类的成员//书上不完整,不浪费时间了
#include<string>
#include<iostream>
using namespace std;
class Student
{
public:
void getValue()
{cin >> num >> name >> sex;};
void display()
{
cout << "num:" << num << endl;
cout << "name" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
char sex;
};
class Student1 :public Student
{
public:
void getValue1()
{cin>>age>>addr:}
void display1()
{
cout << "age:" << age << endl;
cout << "address:" << addr << endl;
}
private:
int age;
string addr;
};
例子5.2p160
例子5.3p167
//在派生类中引用保护成员//与栗子5.1结果相同
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
protected:
int num;
string name;
char sex;
};
class Student1 :protected Student
{
public:
void getValue1();
void display1();
private:
int age;
string addr;
};
void Student1::getValue1()
{
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 << "address:" << addr << endl;
}
int main()
{
Student1 stud1;
stud1.getValue1();
stud1.display1();
return 0;
}
例子5.4p169
例子5.5p171
//定义简单的派生类的构造函数//注意基类和派生类的构造函数的区别
#include <iostream>
#include<string>
using namespace std;
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(int n, string nam, char s, int a, string ad) :Student(n, nam, s)
{
age = a;
addr = ad;
}
void show()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
cout << "age:" << age << endl;
cout << "address:" << addr << endl << endl;
}
~Student1(){}
private:
int age;
string addr;
};
int main()
{
Student1 stud1(10010, "Wang-li", 'f', 19, "115road");
Student1 stud2(10011, "Zhang-fan", 'm', 21, "213road");
stud1.show();
stud2.show();
}
例子5.6p175
//包括子对象的派生类的构造函数
#include<iostream>
#include<string>
using namespace std;
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(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 << "address" << addr << endl << endl;
}
void showMonitor()
{
cout << endl << "Class nonitor is: " << endl;
monitor.display();
}
private:
Student monitor;//这个对象是由基类创建的对象,不是派生类
int age;
string addr;
};
int main()
{
Student1 stud1(10010, "Wang_li", 10001, "Li_jun", 19, "115 Road");
stud1.show();
stud1.showMonitor();
return 0;
}
例子5.7p177
//多级派生情况下的派生类的构造函数//Student1里面的char应该改了
#include<iostream>
#include<string>
using namespace std;
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(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(int n, string nam, int a, int s) :Student1(n, nam, a)
{
score = s;
}
void showAll()
{
show();
cout << "score:" << score << endl;
}
private:
int score;
};
int main()
{
Student2 stu(10010, "Li", 17, 89);
stu.showAll();
return 0;
}
例子5.8p181
//使用多重继承的派生类的构造函数(意思是说有两个父类)//代码有问题
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string nam, int a, string t)
{
name = nam;
age = a;
title = t;
}
void display()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "title" << title << endl;
}
protected:
string name;
int age;
string title;
};
class Student
{
public:
Student(string nam, char s, float sco)
{
strcpy(name1, nam);
sex = s;
score = s;
}
void display1()
{
cout << "name:" << name1 << endl;
cout << "sex:" << sex << endl;
cout << "score" << score << endl;
}
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
};
例子5.9p190
从5.8里面对两个父类增加了个基类
例子5.10p193
从5.9里面进行了更改
网友评论