zl程序教程

您现在的位置是:首页 >  IT要闻

当前栏目

继承用法大全——c++面向对象编程(必看)

2023-02-18 16:26:30 时间

好了,今天我们来看c++面向对象编程之继承,都坐好了,秋名山码神发车了,基操勿6 一:继承的基本框架及优点: 儿子继承父亲。。 继承的好处:可以减少重复的代码

class A : public B; A 类称为子类 或 派生类 B 类称为父类 或 基类

派生类中的成员,包含两大部分: 一类是从基类继承过来的,一类是自己增加的成员。 从基类继承过过来的表现其共性,而新增的成员体现了其个性。

继承的语法:class 子类 : 继承方式 父类 下面是继承的一段c++代码

class Base1
{
public: 
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

//公共继承
class Son1 :public Base1
{
public:
	void func()
	{
		m_A; //可访问 public权限
		m_B; //可访问 protected权限
		//m_C; //不可访问
	}
};

void myClass()
{
	Son1 s1;
	s1.m_A; //其他类只能访问到公共权限
}

//保护继承
class Base2
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};
class Son2:protected Base2
{
public:
	void func()
	{
		m_A; //可访问 protected权限
		m_B; //可访问 protected权限
		//m_C; //不可访问
	}
};
void myClass2()
{
	Son2 s;
	//s.m_A; //不可访问
}

//私有继承
class Base3
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};
class Son3:private Base3
{
public:
	void func()
	{
		m_A; //可访问 private权限
		m_B; //可访问 private权限
		//m_C; //不可访问
	}
};
class GrandSon3 :public Son3
{
public:
	void func()
	{
		//Son3是私有继承,所以继承Son3的属性在GrandSon3中都无法访问到
		//m_A;
		//m_B;
		//m_C;
	}
};

总的来说就是,公共的父类继承随子类,私有的不会变,保护的随机应变,子类私有全私有。 二:同名时的处理

  1. 变量同名
  2. 函数同名
#include<iostream>
using namespace std;
class Base
{
public:
	Base()
	{
		m_A = 100;
	}
	int m_A;
	void func()
	{
		cout << "Bace func的调用" << endl;
	}
};
class Son :public Base
{
public:
	Son()
	{
		m_A = 200;
	}
	int m_A;
	void func()
	{
		cout << "Son func的调用" << endl;
	}
};
void text01()
{
	Son s;
	cout << s.m_A << endl;
	cout << s.Base::m_A << endl;//加作用域
}
void text02()
{
	Son s;
	s.func();
	s.Base::func();
}
int main()
{
	text01();
	text02();
	system("pause");
	return 0;
}

三.静态成员同名时的处理

#include<iostream>
using namespace std;
class Base
{
public:
	static void func()
	{
		cout << "Base static func" << endl;
	}
	static int m_A;
	static void func(int m_A)//函数重载
	{
		cout << "Base static m_A" << endl;
	}

};
int Base::m_A = 200;
class Son :public Base
{
public:
	static void func()
	{
		cout << "Son static func" << endl;
	}
	static int m_A;
};
int Son::m_A = 100;
void text01()
{
	//对象访问
	Son s;
	cout << "Base" << s.Base::m_A << endl;
	cout << "Son" << s.m_A << endl;
	//类名直接
	cout << "Base" << Son::Base::m_A << endl;
	cout << "Son" << Son::m_A << endl;
	
}
void text02()
{
	Son s;
	s.func();
	s.Base::func();
	Son::func();
	Son::Base::func();
}
int main()
{
	text01();
	text02();
	system("pause");
	return 0;
}

可以看出静态成员同名时有俩种处理方式,但是记住: 无论是什么同名处理,用::作用域一定可以处理 四:下面我们来看一下继承中父类与子类的顺序 提到顺序,不难想到函数的,构造与析构

#include<iostream>
using namespace std;
class Base
{
public:
	Base()
	{
		cout << "父类构造函数" << endl;
	}
	~Base()
	{
		cout << "父类析构函数" << endl;
	}
};
class Son :public Base
{
public:
	Son()
	{
		cout << "子类构造函数" << endl;
	}
	~Son()
	{
		cout << "子类析构函数" << endl;
	}
};
void test01()
{
	//继承中先调用父类构造,再是子类构造
	//先是父类析构,再是子类析构
	Son s;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

这里有点父类有点像栈的先进后出 五.多继承(但在实际开发中不建议使用)

#include<iostream>
using namespace std;
class Base1
{
public:
	Base1()
	{
		m_A = 100;
	}
	int m_A;
};
class Base2
{
public:
	Base2()
	{
		m_B = 200;
	}
	int m_B;
};
class Son :public Base1, public Base2
{
public:
	Son()
	{
		m_C = 300;
		m_D = 400;
	}
	int m_C, m_D;
};
void text01()
{
	Son s;
	cout << "son is size:" << sizeof(Son) << " " << sizeof(s) << endl;
	//16
	cout << s.Base1::m_A;
}
int main()
{
	text01();
	system("pause");
	return 0;
}

从代码中我们可以看出多继承的语法是 class Son :public Base1, public Base2 用“ ,”格开,其中子类Son,自身有俩个int为8,但是它又继承了俩个父类的int,所以输出为16