孙鑫MFC2
1.繼承
#include "stdafx.h" #include "iostream" using namespace std;class Animal { public:void eat(){cout<<"animal eat"<<endl;}void sleep(){cout<<"animal sleep"<<endl;}void breath(){cout<<"animal breath"<<endl;}};class Fish : public Animal //集成Animal類中的public部分 {};int _tmain(int argc, _TCHAR* argv[]) {Animal an;an.eat();Fish fh;fh.sleep();return 0; }2.子類對父類的的訪問權(quán)限 #include "stdafx.h" #include "iostream" using namespace std;class Animal { public:void eat(){cout<<"animal eat"<<endl;} protected:void sleep(){cout<<"animal sleep"<<endl;} private:void breath(){cout<<"animal breath"<<endl;}};class Fish : public Animal //集成Animal類中的public部分 { public:void test()//子類能訪問protected & public,不能訪問private{sleep();} };int _tmain(int argc, _TCHAR* argv[]) {Animal an;an.eat();Fish fh;fh.test();//main中只能訪問public//通過這種方式就可以訪問到animal中的protectedreturn 0; }
3.子類對象定義要先構(gòu)造父類
? 子類先析構(gòu)
4.當(dāng)父類沒有合適的構(gòu)造函數(shù)可供子類調(diào)用的解決辦法
子類向父類傳遞參數(shù)的方法
構(gòu)造函數(shù)不寫,系統(tǒng)會自動添加。當(dāng)構(gòu)造函數(shù)我們寫了,系統(tǒng)不會自動提供構(gòu)造函數(shù)
5.子類如果有常量,初始化方法之一 class Fish : public Animal //集成Animal類中的public部分 { public:// Fish()//當(dāng)Animal(int height,int weight),沒有不帶參數(shù)的構(gòu)造函數(shù),錯誤。//解決辦法Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在這里{cout<<"fish construct"<<endl;}~Fish(){cout<<"fish deconstruct"<<endl;} protected:const int a; };
6.函數(shù)的覆蓋(父類子類之間) class Animal { public:int a;Animal(int height,int weight){ // cout<<"animal construct"<<endl;}~Animal(){ // cout<<"animal deconstruct"<<endl;}void eat(){cout<<"animal eat"<<endl;}void sleep(){cout<<"animal sleep"<<endl;}void breath(){cout<<"animal breath"<<endl;}};class Fish : public Animal //集成Animal類中的public部分 { public:// Fish()//當(dāng)Animal(int height,int weight),沒有不帶參數(shù)的構(gòu)造函數(shù),錯誤。//解決辦法Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在這里{ // cout<<"fish construct"<<endl;}~Fish(){ // cout<<"fish deconstruct"<<endl;}void breath(){cout<<"fish bubble"<<endl;} protected:const int a; };int _tmain(int argc, _TCHAR* argv[]) { // Animal an; // an.eat();Fish fh;fh.breath();return 0; }
6.作用域標(biāo)識符(子類中希望調(diào)用父類的同名的函數(shù)) class Fish : public Animal //集成Animal類中的public部分 { public:// Fish()//當(dāng)Animal(int height,int weight),沒有不帶參數(shù)的構(gòu)造函數(shù),錯誤。//解決辦法Fish() : Animal(400,300),a(1)//常量a的初始化,可以放在這里{ // cout<<"fish construct"<<endl;}~Fish(){ // cout<<"fish deconstruct"<<endl;}void breath(){Animal::breath();// :: 作用域標(biāo)識符cout<<"fish bubble"<<endl;} protected:const int a; };
7.子類和父類的首地址是一樣的,所以顯示的是父類的breath
多態(tài)性 ?根據(jù)地址決定調(diào)用的函數(shù)
把
virtual void breath(){cout<<"animal breath"<<endl;}父類是虛函數(shù),編譯器會根據(jù)對象類型,選擇調(diào)用的函數(shù)。(遲綁定)
父類不是虛函數(shù),根據(jù)地址,選擇調(diào)用父類的函數(shù)
注釋掉子類中的函數(shù),就會調(diào)用父類的。
總結(jié):多態(tài)性就是,父類的虛函數(shù)做子類的備胎。
如果,父類中的是純虛函數(shù),就不能用于實例化對象。要求子類中不能沒有要調(diào)用的函數(shù)。
virtual void breath() = 0;//純虛函數(shù)/*{cout<<"animal breath"<<endl;}*/8.引用
int a = 6;int &b = a;//引用b是a的別名,沒有內(nèi)存空間。改變a或b都會修改其2者共同對應(yīng)的值。
9.防止重復(fù)編譯頭文件,在頭文件中
#ifndef FISH_H //使用這種語句防止重復(fù)編譯頭文件 #define FISH_H#include "Animal.h"class Fish : public Animal //集成Animal類中的public部分 { public:Fish();void breath(); };#endif注意,在 ?.cpp中,#include "stdafx.h"//一定要出現(xiàn)在最前端 #include "stdafx.h"//一定要出現(xiàn)在最前端#include "Fish.h"#include "iostream" using namespace std;Fish:: Fish():Animal(300,400) {}void Fish:: breath() {cout<<"fish bubble"<<endl; }
10.this 指針
指向?qū)ο蟊旧?#xff0c;代表對象的地址。
也就是說,在對象要調(diào)用成員函數(shù)時,this是隱含的,用于指示地址。
實現(xiàn)多個對象可以共同調(diào)用類中的共同的成員,使用this中指向的地址來分辨不同的對象。
對象調(diào)用 pt.output(10,10),成員函數(shù)獲得兩個形參,也獲得了地址。即 this = &pt.
11.類的繼承和訪問特性
12.程序編譯鏈接原理與過程
總結(jié)
- 上一篇: 【前端性能优化】浏览器渲染原理与性能优化
- 下一篇: DM建模实例2