继承,虚函数,链表
class People{
public:
people(){
}
people(string id, string name = "New People"):m_strID(id),m_strName(name){
}
virtual void info(){
cout<< "ID" << m_strID << "name:" << m_strName << endl;
}
private:
const string m_strID;
string m_strName;
};
class Node{
public:
Node(People *peopel){
m_people = p;
m_pNext = NULL;
}
Node *next(){
return m_pNext;
}
void setNext(Node *node){
m_pNext = node;
}
void display(){
if(NULL ! = m_peopel){
m_people->info();
}
}
private:
People *m_people;
Node *m_pNext
};
class Link{
public:
Link(){
m_pFirstNode = NULL;
m_iLen = 0;
}
void insert(Node *node){
node->m_pNext = m_pFirstNode;
m_pFirstNode = node;
m_ilen++;
}
void display(){
Node *node = n_pFirstNode;
while (NULL != node){
node->display();
node = node->next();
}
}
Node *m_pFirstNode;
int m_iLen;
};
//定义学生派生类,继承people类
class STU : public people{
public:
STU(string id,string name,float score):people(id,name),m_fScore(score){
}
STU(){
}
void info(){
peopel::info();
cout<<"学分"<<m_score<<endl;
}
private:
float m_score;
};
//定义老师派生类,继承people类
class TEC : public people{
public:
TEC(string id,string name,string course,float salary)
:people(id,name),m_fScore(course),m_fSalary(salary){
}
TEC(){
}
void info(){
peopel::info();
cout<<"课程"<<m_strCourse<<endl;
cout<<"工资"<<m_fSalary<<endl;
}
private:
string m_strCourse;
float m_fSalary;
};
int main(int argc, const char *argv[]){
Link link;
Node *node = NULL;
STU *stu = new STU("101021","小明",90);
node = new Node(stu);
link.insert(node);
link.insert(new Node(new STU("101022", "小明2", 92)));
link.insert(new Node(new STU("101023", "小明3", 93)));
link.insert(new Node(new STU("101024", "小明4", 94)));
link.display();
link.insert(new Node(new TEC("11", "aa", "math", 99999)));
link.insert(new Node(new TEC("12", "aa2", "math", 99999)));
link.insert(new Node(new TEC("13", "aa3", "math", 99999)));
link.insert(new Node(new TEC("14", "aa4", "math", 99999)));
link.display();
}
析构函数:
如果一个函数的构造函数使 A(){}
那么其析构函数是是 ~A(){}
为了在释放基类的时候可以自动释放对象的派生类
一般将基类的析构函数定义为虚函数,即虚析构函数,
那么派生类的析构函数默认是虚函数
防止传递参数到形参是类的对象的函数中时发生隐式类型转换
class A{
public:
expilicit A(){}
};
void fun(A a){}
int main(void){
fun(8);
A a2 = 8;//A a2(8);
fun(a2);
}
因为有 ‘expilicit’关键字的存在,所以传递参数到fun中时必须传递A类的对象
如果没有此关键字会导致传参过程中发生隐式类型转换,即注释后面的内容