日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

孙鑫MFC2

發(fā)布時間:2024/1/1 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 孙鑫MFC2 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)


#include "stdafx.h" #include "iostream" using namespace std;class Animal { public:Animal(){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(){cout<<"fish construct"<<endl;}~Fish(){cout<<"fish deconstruct"<<endl;}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; }

4.當(dāng)父類沒有合適的構(gòu)造函數(shù)可供子類調(diào)用的解決辦法

子類向父類傳遞參數(shù)的方法

構(gòu)造函數(shù)不寫,系統(tǒng)會自動添加。當(dāng)構(gòu)造函數(shù)我們寫了,系統(tǒng)不會自動提供構(gòu)造函數(shù)


class Animal { public: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){cout<<"fish construct"<<endl;}~Fish(){cout<<"fish deconstruct"<<endl;}void test()//子類能訪問protected & public,不能訪問private{sleep();} };
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


#include "stdafx.h" #include "iostream" using namespace std;class Animal { public:int a;Animal(int height,int weight){}~Animal(){}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的初始化,可以放在這里{}~Fish(){}virtual void breath(){cout<<"fish bubble"<<endl;} protected:const int a; };void fn(Animal *pAn) {pAn->breath(); }int _tmain(int argc, _TCHAR* argv[]) {Fish fh;Animal *pAn;pAn = &fh;fn(pAn);return 0; }

多態(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é)

以上是生活随笔為你收集整理的孙鑫MFC2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。