C++Day6——继承

C++Day6——继承

继承的基本语法

#include <iostream>
using namespace std;

//利用继承模拟网页
//减少重复代码,提高代码复用性
class BasePage
{
public:
	void header()
	{
		cout << "公共的头部" << endl;
	}

	void footer()
	{
		cout << "公共的底部" << endl;
	}

	void leftList()
	{
		cout << "公共的左侧列表" << endl;
	}
};

//class 子类 : 继承方式 父类
//News子类  派送类
//BasePage父类  基类
class News : public BasePage
{
public:
	void content()
	{
		cout << "新闻播报..." << endl;
	}
};

class Sport : public BasePage
{
public:
	void content()
	{
		cout << "世界杯..." << endl;
	}
};

void test01()
{
	News news;
	cout << "新闻页面内容如下:" << endl;
	news.header();
	news.footer();
	news.leftList();
	news.content();
}

int main()
{
	test01();
	return EXIT_SUCCESS;
}

继承方式

  • public公有继承
  • private私有继承
  • protected保护继承
#include <iostream>
using namespace std;

公共继承///
class Base1
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class Son1 : public Base1
{
public:
	void func()
	{
		m_A = 100;//父类中公共权限,子类中变为公共权限
		m_B = 100;//父类中保护权限,子类中变为保护权限
		//m_C=100;//父类中私有权限,子类无法访问
	}
};

void test01()
{
	Son1 s1;
	s1.m_A = 100;//在Son1中m_A是公共权限,类外可以访问
	//s1.m_B=100;//在Son1中m_B是保护权限,类外不可以访问
}

保护继承///
class Base2
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class Son2 : protected Base2
{
public:
	void func()
	{
		m_A = 100;//父类中公共权限,子类中变为保护权限
		m_B = 100;//父类中保护权限,子类中变为保护权限
		//m_C=100;//父类中私有权限,子类无法访问
	}
};

void test02()
{
	Son2 s2;
	//s2.m_A = 100;//在Son2中m_A是保护权限,类外不可以访问
	//s2.m_B=100;//在Son2中m_B是保护权限,类外不可以访问
}

私有继承///
class Base3
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class Son3 : private Base3
{
public:
	void func()
	{
		m_A = 100;//父类中公共权限,子类中变为私有权限
		m_B = 100;//父类中保护权限,子类中变为私有权限
		//m_C=100;//父类中私有权限,子类无法访问
	}
};

void test03()
{
	Son3 s3;
	//s3.m_A = 100;//在Son3中m_A是私有权限,类外不可以访问
	//s3.m_B=100;//在Son3中m_B是私有权限,类外不可以访问
}

class GrandSon3 : public Son3
{
public:
	void func()
	{
		//m_A = 100;在Son3中已经为私有权限 访问不到
	}
};

继承中的对象模型

#include <iostream>
using namespace std;

class Base
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;//父类中私有属性,子类访问不到,是由编译器隐藏了,但是又继承
	//可以利用开发人员工具查看对象模型
};

class Son : public Base
{
public:
	int m_D;
};

void test01()
{
	cout << "sizeof Son=" << sizeof(Son) << endl;//16
}

int main()
{
	test01();
	return EXIT_SUCCESS;
}

继承中的构造和析构顺序

#include <iostream>
using namespace std;

class Base1
{
public:
	Base1()
	{
		cout << "Base1的构造函数调用" << endl;
	}

	~Base1()
	{
		cout << "Base1的析构函数调用" << endl;
	}

};

class Other
{
public:
	Other()
	{
		cout << "Other的构造函数调用" << endl;
	}
	~Other()
	{
		cout << "Other的析构函数调用" << endl;
	}
};

class Son1 : public Base1
{
public:
	Son1()
	{
		cout << "Son1的构造函数调用" << endl;
	}

	~Son1()
	{
		cout << "Son1的析构函数调用" << endl;
	}
	Other other;
};

void test01()
{
	Son1 s;//先调用父类构造,在调用其他成员构造,在调用自身构造,析构的顺序相反
}

class Base2
{
public:
	Base2(int a)
	{
		cout << "Base2的构造函数调用" << endl;
	}

};

class Son2 : public Base2
{
public:
	Son2(int a):Base2(a)//利用初始化列表语法 显示调用父类中的其他构造函数
	{
		cout << "Son2的构造函数调用" << endl;
	}
};

//父类中默认构造、析构、拷贝构造、operator=是不会被子类继承下去

int main()
{
	test01();
	return EXIT_SUCCESS;
}

继承中的同名成员处理

#include <iostream>
using namespace std;

class Base
{
public:
	Base()
	{
		this->m_A = 10;
	}
	int m_A = 10;

	void func()
	{
		cout << "Base中的func调用" << endl;
	}
	void func(int a)
	{
		cout << "Base中的func(int a)调用" << endl;
	}
};

class Son : public Base
{
public:
	Son()
	{
		this->m_A = 20;
	}

	int m_A;
	void func()
	{
		cout << "Son中的func调用" << endl;
	}
};

void test01()
{
	Son s1;
	cout << "s1.m_A=" << s1.m_A << endl;//20
	//可以利用作用域访问父类中的同名成员
	cout << "Base中的m_A=" << s1.Base::m_A << endl;
	s1.func();
	s1.Base::func();
	//s1.func(10);当子类重新定义了父类中的同名成员函数
	//子类的成员函数会隐藏父类中所有重载版本的同名成员
	//可以利用作用域显示指定调用
}

int main()
{
	test01();
	return EXIT_SUCCESS;
}

继承中的同名静态成员处理

#include <iostream>
using namespace std;

class Base
{
public:
	static int m_A;
	static void func()
	{
		cout << "Base中func调用" << endl;
	}
	static void func(int a)
	{
		cout << "Base中func(int a)调用" << endl;
	}
};

int Base::m_A = 10;

class Son : public Base
{
public:
	static int m_A;
	static void func()
	{
		cout << "Son中func调用" << endl;
	}
};

int Son::m_A = 20;

void test01()
{
	//通过对象访问
	Son s;
	cout << "m_A=" << s.m_A << endl;
	cout << "Base中m_A=" << s.Base::m_A << endl;

	//通过类名访问
	cout << "m_A=" << Son::m_A << endl;
	cout << "Base中的m_A=" << Son::Base::m_A << endl;
}

void test02()
{
	//通过对象访问
	Son s1;
	s1.func();
	s1.Base::func();
	//s1.func(10);
	//子类的成员函数会隐藏父类中所有重载版本的同名成员
	//可以利用作用域显示指定调用

	//通过类名访问
	Son::func();
	Son::Base::func();
}

int main()
{
	test01();
	test02();
	return EXIT_SUCCESS;
}

多继承基本语法

#include <iostream>
using namespace std;

class Base1
{
public:
	Base1()
	{
		this->m_A = 10;
	}
	int m_A;
};

class Base2
{
public:
	Base2()
	{
		this->m_A = 20;
	}
	int m_A;
};

//多继承
class Son : public Base1, public Base2
{
public:
};

void test01()
{
	cout << "sizeof_Son=" << sizeof(Son) << endl;//16
	Son s;
	//当多继承中两个父类有同名成员,需加作用域区分
	cout << "Base1中的m_A=" << s.Base1::m_A << endl;
	cout << "Base2中的m_A=" << s.Base2::m_A << endl;

}

int main()
{
	test01();
	return EXIT_SUCCESS;
}

菱形继承(钻石型继承)的问题及解决

  • 两个派生类继承同一个基类又有某个类同时继承这两个派生类
#include <iostream>
using namespace std;

//动物类
class Animal
{
public:
	int m_Age;//年龄
};

//Animal称为虚基类

//羊类
class Sheep : virtual public Animal{};
//驼类
class Tuo : virtual public Animal{};

//羊驼
class SheepTuo : public Sheep, public Tuo
{

};

void test01()
{
	SheepTuo st;
	st.Sheep::m_Age = 10;
	st.Tuo::m_Age = 20;

	cout << "Sheep::m_Age=" << st.Sheep::m_Age << endl;
	cout << "Tuo::m_Age=" << st.Tuo::m_Age << endl;
	cout << "Tuo::m_Age=" << st.m_Age << endl;

	//当发生虚继承后,sheep和tuo类中继承了一个vbptr指针(虚基类指针)
	//指向的是一个虚基类表vbtable
	//虚基表中记录了偏移量,通过偏移量可以找到唯一的m_Age
}

int main()
{
	test01();
	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值