-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
赋值兼容性原则
子类对象可以当作父类对象使用
子类对象可以直接赋值给父类对象
子类对象可以直接初始化父类对象
父类指针可以直接指向子类对象
父类引用可以直接引用子类对象
子类是就是特殊的父类!!!
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent//定义父类
{
protected:
const char* name;
public:
Parent()//构造函数 赋值
{
name = "Parent";
}
void print()
{
cout<<"Name: "<<name<<endl;
}
};
class Child : public Parent //定义子类
{
protected:
int i;
public:
Child(int i)
{
this->name = "Child";//继承来的name 赋值
this->i = i;
}
};
int main(int argc, char *argv[])
{
Child child(1000); //定义子类对象child
Parent parent = child;//用子类对象初始化父类对象
Parent* pp = &child; //父类指针指向子类对象
Parent& rp = child; //父类引用子类对象
parent.print();
pp->print();
rp.print();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
第二:
继承对象模型
类在C++ 编译器的内部 可以理解为结构体
子类是由父类成员叠加子类新成员得到的
问题
如何初始化父类成员?
父类与子类的构造函数有什么关系?
继承与构造
在子类对象 构造的时候 需要调用 父类构造函数对其 继承得来的成员 进行初始化
继承与析构
在子类对象 析构的时候 需要调用 父类析构函数对其 继承得来的成员 进行清理
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent//父类函数
{
public:
Parent()//构造函数
{
cout<<"Parent()"<<endl;
}
~Parent()//析构函数
{
cout<<"~Parent()"<<endl;
}
};
class Child : public Parent//子类函数
{
public:
//在子类对象 构造的时候 需要调用 父类构造函数对其 继承得来的成员 进行初始化
Child()//在调用子类构造函数Child()时,会先调用父类的构造函数Parent()
{
cout<<"Child()"<<endl;
}
//在子类对象 析构的时候 需要调用 父类析构函数对其 继承得来的成员
~Child()
{
cout<<"~Child()"<<endl;
}
};
void run()
{
Child child;
}
int main(int argc, char *argv[])
{
run();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
继承与构造和析构
子类对象在创建时会首先调用父类的构造函数
父类构造函数执行结束后,执行子类的构造函数
当父类的构造函数有参数时,需要在子类的初始化列表中显示调用
析构函数调用的先后顺序与构造函数相反
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
public:
Parent(const char* s)//有参数
{
cout<<"Parent()"<<" "<<s<<endl;
}
~Parent()
{
cout<<"~Parent()"<<endl;
}
};
class Child : public Parent
{
public:
//当父类的构造函数有参数时,需要在子类的初始化列表中显示调用
Child() : Parent("Parameter from Child!")//传递字符串参数Parameter from Child!
{
cout<<"Child()"<<endl;
}
~Child()
{
cout<<"~Child()"<<endl;
}
};
void run()
{
Child child;
}
int main(int argc, char *argv[])
{
run();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
第三:
继承与组合的混搭
类中的成员变量可以是其它类的对象
问题
如果一个类继承自父类并且有其它的对象作为成员,那么构造函数如何调用?
口诀:先父母,后客人,再自己
#include <cstdlib>
#include <iostream>
using namespace std;
class Object//父类
{
public:
Object(const char* s)//打印字符串
{
cout<<"Object()"<<" "<<s<<endl;
}
};
class Parent : public Object//继承Object
{
public:
Parent(const char* s) : Object(s)//Parent在初始化列表里面显示调用,传递参数s
{
cout<<"Parent()"<<" "<<s<<endl;
}
};
class Child : public Parent
{
protected:
//初始化顺序
Object o1;
Object o2;
public:
//先父母,后客人,再自己
Child() : o2("o2"), o1("o1"), Parent("Parameter from Child!")
{
cout<<"Child()"<<endl;
}
};
void run()
{
Child child;
}
int main(int argc, char *argv[])
{
run();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
第四:
同名成员变量
思考
当子类中定义的成员变量与父类中的成员变量同名时会发生什么?
当子类成员变量与父类成员变量同名时
子类依然从父类继承同名成员
在子类中通过作用域分别符:: 进行同名成员区分
同名成员存储在内存中的不同位置
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
protected:
int i;
int f;
};
class Child : public Parent
{
protected:
int i;
void f()
{
//在子类中通过作用域分别符:: 进行同名成员区分
cout<<"Parent::i = "<<Parent::i<<endl;
cout<<"Child::i = "<<Child::i<<endl;
cout<<"Parent::f = "<<Parent::f<<endl;
}
public:
Child(int i, int j)
{
Parent::i = i;
Child::i = j;
Parent::f = i + j;
f();
}
};
void run()
{
Child child(1, 3);
}
int main(int argc, char *argv[])
{
run();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
小结
子类对象可以当作父类对象使用
子类对象在创建时需要调用父类构造函数进行初始化
子类对象在销毁时需要调用父类析构函数进行清理
先执行父类构造函数,再执行成员构造函数
在继承中的析构顺序与构造顺序对称相反
同名成员通过作用域分辨符进行区分