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

首頁 > 編程 > C++ > 正文

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

2019-11-11 03:45:56
字體:
供稿:網(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_;  //外部可以被訪問PRotected:	int protected_i_; //內(nèi)部可以訪問,外部無法訪問,可繼承,子類可訪問private:	int private_i_; //只有類內(nèi)部可以訪問	int num_;};class A :public Base    //維持不變{public:	A() :Base(0)   //默認(rèn)的調(diào)用基類的默認(rèn)構(gòu)造函數(shù),		//如果沒有默認(rèn)構(gòu)造,在初始化列表中初始化	{		std::cout << "A::A()" << std::endl;	}	~A()	{		std::cout << "A::~A()" << std::endl;	}	int GetNum() const   //重寫后返回子類的num_ 覆蓋了父類的方法,父類的方法就不存在了	{		return num_;	}private:	int num_ ;};class B :protected Base  //修改權(quán)限,把public改為protect,外部無法訪問{};class C :private Base   //把public改為private,外部無法訪問{};int main(){	A demo;//先調(diào)用父類的構(gòu)造函數(shù),再調(diào)用自己的構(gòu)造。析構(gòu)先析構(gòu)子類,再析構(gòu)父類	demo.GetNum();//若子類沒重寫返回父類的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)的時候會導(dǎo)致B中的成員泄露					//將A類定義成虛析構(gòu)函數(shù)才能對B類成員進(jìn)行正確的釋放	// 調(diào)用順序 父類構(gòu)造 -> 父類成員的構(gòu)造(類中有其他類的對象)  ->子類構(gòu)造        // 析構(gòu)順序 子類析構(gòu) -> 父類成員析構(gòu)   -> 父類析構(gòu)	return 0;}

MyString 重寫 +  虛函數(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ù)后就會調(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 // 使用虛繼承只繼承了一個a_{	//無虛繼承時有a_*2  b_ c_ };int main(){	D d;	//d.a_; 不使用虛繼承有二義性	//d.C::a_;	d.a_;//虛繼承	return 0;}

純虛函數(shù)

//有純虛函數(shù)的類就是抽象類,抽象類不能創(chuàng)建對象,是其他類的基類 ,一般用來當(dāng)接口使用class Animal{public:	virtual void Cry()=0;//純虛函數(shù),無須實(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ā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 合山市| 专栏| 永兴县| 黄大仙区| 广平县| 抚州市| 平乡县| 景德镇市| 准格尔旗| 安康市| 宁德市| 林西县| 如皋市| 昭平县| 巴楚县| 沾益县| 巴彦淖尔市| 沁阳市| 玉溪市| 宜都市| 左权县| 仲巴县| 桂东县| 牙克石市| 峨眉山市| 社会| 金堂县| 南投市| 信丰县| 太原市| 都昌县| 大英县| 涡阳县| 祁阳县| 靖江市| 罗城| 会理县| 桓仁| 建平县| 临汾市| 嘉义市|