C++类和对象——继承详解

目录

1.基本语法

2.继承方式 

3.继承中的对象模型

4.构造和构析顺序 

5.同名成员处理

6.同名静态成员处理

7.多继承语法

8.菱形继承 

图片示例:

虚继承

代码示例: 


1.基本语法

 

#include<bits/stdc++.h>
using namespace std;

//公共页面类
class basepage
{
public:
	void header()
	{
		cout << "启动" << endl;
	}
	
	void left()
	{
		cout << "结束" << endl;
	}
};

class three_body : public basepage
{
public:
	void content()
	{
		cout << "three_body" << endl;
	}
};

class wandering_earth : public basepage
{
public:
	void content()
	{
		cout << "wandering_earth" << endl;
	}
};

int main()
{
	three_body t;
	t.header();
	t.content();
	t.left();
	
	wandering_earth w;
	w.header();
	w.content();
	w.left();
	
	return 0;
}

 

 

 

2.继承方式 

 

解释:

继承方式:公有继承保护继承私有继承
父类中为公共权限子类中为公共权限子类中为保护权限子类中为私有权限
父类中为保护权限子类中为保护权限子类中为保护权限子类中为私有权限
父类中为保护权限子类中访问不到子类中访问不到子类中访问不到

保护权限和私有权限类外访问不到 

3.继承中的对象模型

#include<bits/stdc++.h>
using namespace std;

//公共页面类
class base
{
public:
	int pu;
protected:
	int pro;
private:
	int pri;
};

class son1 : public base
{
public:

	int a;
};

int main()
{
	son1 a;
	//父类中所有非静态成员属性都会被子类继承下去
	//父类中私有成员属性是被隐藏了,因此访问不到,但是确实被继承下去了
	cout << sizeof(a) << endl;
	return 0;
}

 

 

 

4.构造和构析顺序 

父类先构造,子类再构造,析构,父类最后析构。

#include<bits/stdc++.h>
using namespace std;

class father
{
public:
	father()
	{
		cout << "父类的构造函数" << endl;
	}
	
	~father()
	{
		cout << "父类的析构函数" << endl;
	}
};

class son : public father
{
public:
	son()
	{
		cout << "子类的构造函数" << endl;
	}
	
	~son()
	{
		cout << "子类的析构函数" << endl;
	}
};

int main()
{
	son s;
	return 0;
}

 

5.同名成员处理

 

 

#include<bits/stdc++.h>
using namespace std;

class father
{
public:
	father()
	{
		a = 100;
	}
	
	void func()
	{
		cout << "父类函数调用" << endl;
	}
	int a;
};

class son : public father
{
public:
	son()
	{
		a = 200;
	}
	
	void func()
	{
		cout << "子类函数调用" << endl;
	}
	
	int a;
};

int main()
{
	son s;
	
	cout << s.a << endl;
	//通过子类对象访问父类同名对象要加作用域
	cout << s.father::a << endl;
	
	s.func();
	s.father::func();
	
	return 0;
}

 

6.同名静态成员处理

 

#include<bits/stdc++.h>
using namespace std;

class father
{
public:
	static void func()
	{
		cout << "父类函数调用" << endl;
	}

	static int a;
};

int father::a = 200;

class son : public father
{
public:
	static void func()
	{
		cout << "子类函数调用" << endl;
	}
	
	static int a;
};

int son::a = 100;

int main()
{
	son s;
	
	//通过对象进行访问
	cout << s.a << endl;
	cout << s.father::a << endl;
	
	cout << "-------------------" << endl;
	
	//通过类名进行访问
	cout << son::a << endl;
	cout << son::father::a << endl;
	
	cout << endl;
	
    //通过对象进行访问
	s.func();
	s.father::func();
	
	cout << "-------------------" << endl;
	
	//通过类名进行访问
	son::func();
	son::father::func();
	
	return 0;
}

 

7.多继承语法

 

 

#include<bits/stdc++.h>
using namespace std;

class base1
{
public:  

	int a = 1000;
	
};

class base2
{
public:  

	int a = 2000;
	
};

class son : public base1,public base2
{
public:

	int b;
	int c;
};

int main()
{
	son s;
	
	cout << sizeof(s) << endl;
	
	cout << s.base1::a << endl;
	cout << s.base2::a << endl;
	
	return 0;
}

 

8.菱形继承 

图片示例:

 

 下面以animal,羊,骆驼,羊驼这四种动物为例:

 

虚继承

在继承之前加上virtual 

 

使用虚继承后sheep和tuo里存的会变为一个虚基类指针,共同指向m_age,避免了二义性。

 

代码示例: 

#include<bits/stdc++.h>
using namespace std;

class animal
{
public:

	int m_age;
};

class sheep : virtual public animal{};

class camel : virtual public animal{};

class alpaca : public sheep,public camel{};

int main()
{
	alpaca a;
	a.m_age = 10;
	
	cout << a.sheep::m_age << endl;
	cout << a.camel::m_age << endl;
	cout << a.m_age << endl;
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏箱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值