国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

C++學(xué)習(xí)筆記 lesson12 繼承

2019-11-11 03:12:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

繼承權(quán)限

#include<iostream>class Base{public:	//Base()	//{	//	std::cout << "Base::Base()" << std::endl;	//}	Base(int num) :num_(num)	{		std::cout << "Base::Base()" << std::endl;	}	 ~Base()	{		std::cout << "Base::~Base()" << std::endl;	}	virtual int  GetNum()const //虛函數(shù),尋求子類中最符合的方式	{		return num_;	}	int public_i_;  //外部可以被訪問(wèn)PRotected:	int protected_i_; //內(nèi)部可以訪問(wèn),外部無(wú)法訪問(wèn),可繼承,子類可訪問(wèn)private:	int private_i_; //只有類內(nèi)部可以訪問(wèn)	int num_;};class A :public Base    //維持不變{public:	A() :Base(0)   //默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),		//如果沒(méi)有默認(rèn)構(gòu)造,在初始化列表中初始化	{		std::cout << "A::A()" << std::endl;	}	~A()	{		std::cout << "A::~A()" << std::endl;	}	int GetNum() const   //重寫(xiě)后返回子類的num_ 覆蓋了父類的方法,父類的方法就不存在了	{		return num_;	}private:	int num_ ;};class B :protected Base  //修改權(quán)限,把public改為protect,外部無(wú)法訪問(wèn){};class C :private Base   //把public改為private,外部無(wú)法訪問(wèn){};int main(){	A demo;//先調(diào)用父類的構(gòu)造函數(shù),再調(diào)用自己的構(gòu)造。析構(gòu)先析構(gòu)子類,再析構(gòu)父類	demo.GetNum();//若子類沒(méi)重寫(xiě)返回父類的num_	A a;	Base *p_base = &a; //父類的指針指向子類	return 0;}

虛函數(shù)

#include<iostream>class A{public:	virtual ~A()	{}};class B:public A{public:	B():A()	{		demo_ = new int(0);	}	~B()	{		delete demo_;	}private:	int *demo_;//指針成員};int main(){	A *p_a = new B; //只能看到A類的方法和成員,析構(gòu)的時(shí)候會(huì)導(dǎo)致B中的成員泄露					//將A類定義成虛析構(gòu)函數(shù)才能對(duì)B類成員進(jìn)行正確的釋放	// 調(diào)用順序 父類構(gòu)造 -> 父類成員的構(gòu)造(類中有其他類的對(duì)象)  ->子類構(gòu)造        // 析構(gòu)順序 子類析構(gòu) -> 父類成員析構(gòu)   -> 父類析構(gòu)	return 0;}

MyString 重寫(xiě) +  虛函數(shù)實(shí)現(xiàn)

#include <cstring>#include<iostream>class String{public:	String(const char *str = "")	{		unsigned int len = strlen(str);		str_ = new char[len + sizeof(char)];		strcpy(str_, str);	}	String(const String &other)	{		unsigned int len = strlen(other.str_);		str_ = new char[len + sizeof(char)];		strcpy(str_, other.str_);	}	~String()	{		delete[]str_;	}	String &Operator+=(const String &other)	{		unsigned int len = strlen(str_) + strlen(other.str_);		char *temp = new char[len + sizeof(char)];		strcpy(temp, str_);		strcat(temp, other.str_);			delete[]str_;		str_ = temp;		return *this;	}	virtual String operator+(const String &other)	{		String demo=*this;		demo+= other;		return demo;	}	friend std::ostream&operator<<(std::ostream&os, const String&other)	{		os << other.str_;		return os;	}protected:	char *str_;};class MyString :public String{public:	MyString(const char *str="PoEdu"):String(str)	{	}	String operator + (const String &other)	{		MyString temp=*this;		temp += other; // 繼承了+=		temp += "----PoEdu";		return temp;	}	};int main(){	MyString str("I Love");	String *p_string = &str;	std::cout << str + ("Mark") << std::endl;	std::cout << *p_string+("Mark") << std::endl; //調(diào)用的是父類里面的+ ,把+做成虛函數(shù)后就會(huì)調(diào)用子類的+	return 0;}

虛繼承

class  A{public:	int a_;};class B :virtual public A{public:	int b_;};class C: virtual public A{public:	int c_;};class D : public B, public C // 使用虛繼承只繼承了一個(gè)a_{	//無(wú)虛繼承時(shí)有a_*2  b_ c_ };int main(){	D d;	//d.a_; 不使用虛繼承有二義性	//d.C::a_;	d.a_;//虛繼承	return 0;}

純虛函數(shù)

//有純虛函數(shù)的類就是抽象類,抽象類不能創(chuàng)建對(duì)象,是其他類的基類 ,一般用來(lái)當(dāng)接口使用class Animal{public:	virtual void Cry()=0;//純虛函數(shù),無(wú)須實(shí)現(xiàn)	};class Dog :public Animal  //必須實(shí)現(xiàn)Cry 才能被實(shí)例化{public:	void Cry()	{}};int main(){	Animal *animal = new Dog();	animal->Cry();	return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 马山县| 扶风县| 西畴县| 高清| 海阳市| 绥芬河市| 陵水| 贡山| 夏河县| 东辽县| 雷山县| 铜山县| 六安市| 新蔡县| 恭城| 汉中市| 甘肃省| 廉江市| 西安市| 炎陵县| 介休市| 逊克县| 融水| 淳安县| 新宾| 柘城县| 明水县| 辽源市| 井冈山市| 家居| 内黄县| 大理市| 吉木萨尔县| 铁岭市| 资溪县| 无棣县| 海淀区| 大邑县| 余江县| 安新县| 揭东县|