C++学习笔记 lesson12 继承

继承权限

#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 //虚函数,寻求子类中最符合的方式
	{
		return num_;
	}
	int public_i_;  //外部可以被访问
protected:
	int protected_i_; //内部可以访问,外部无法访问,可继承,子类可访问
private:
	int private_i_; //只有类内部可以访问
	int num_;
};

class A :public Base    //维持不变
{
public:
	A() :Base(0)   //默认的调用基类的默认构造函数,
		//如果没有默认构造,在初始化列表中初始化
	{
		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  //修改权限,把public改为protect,外部无法访问
{};
class C :private Base   //把public改为private,外部无法访问
{};

int main()
{
	A demo;//先调用父类的构造函数,再调用自己的构造。析构先析构子类,再析构父类
	demo.GetNum();//若子类没重写返回父类的num_

	A a;
	Base *p_base = &a; //父类的指针指向子类
	return 0;
}

虚函数

#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类的方法和成员,析构的时候会导致B中的成员泄露
					//将A类定义成虚析构函数才能对B类成员进行正确的释放
	// 调用顺序 父类构造 -> 父类成员的构造(类中有其他类的对象)  ->子类构造
        // 析构顺序 子类析构 -> 父类成员析构   -> 父类析构
	return 0;
}

MyString 重写 +  虚函数实现

#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; //调用的是父类里面的+ ,把+做成虚函数后就会调用子类的+
	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;
}

纯虚函数

//有纯虚函数的类就是抽象类,抽象类不能创建对象,是其他类的基类 ,一般用来当接口使用
class Animal
{
public:
	virtual void Cry()=0;//纯虚函数,无须实现
	
};
class Dog :public Animal  //必须实现Cry 才能被实例化
{
public:
	void Cry()
	{}
};

int main()
{
	Animal *animal = new Dog();
	animal->Cry();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值