#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;}
新聞熱點(diǎn)
疑難解答
圖片精選