c++234继承



cpp 复制代码
#include<iostream>
using namespace std;


//public 修饰的成员便俩个和方法都能使用
//protected:类的内部 在继承的子类中可使用
class Parents
{
public:
	int a;//名字
protected:
	int b;//密码
private:
	int c;//情人

public:
	void printT()
	{
		cout << "printT" << endl;
	}
};
class Children1 :public Parents
{
public:
	void useVar()
	{
		a = 0;
		b = 0;
		//c = 0;//private
	}
};
 
//私有继承
class Children2 :private Parents
{
public:
	void useVar()
	{
		a = 0;
		b = 0;
		//c = 0;//private
	}
};

class Children3 :protected Parents
{
public:
	void useVar()
	{
		a = 0;//ok
		b = 0;//ok
	//	c = 0;
	}
};


//void main01()
//{
//	Parents t1, t2;
//	t1.a = 10;
//	//t1.b = 30;//err
//	//t2.c = 30;//err
//
//
//	return;
//}
void main02()
{
	Children2 c2;
	//c1.a = 10;//err
	//c2.b = 30;//err
	//c3.b = 20;//err
}
void main()
{
	Children3 c3;
	//c3.a = 10;//err
	//c3.b = 30;//err
	//c3.c = 20;//err
}





调用原则:

cpp 复制代码
#include<iostream>
using namespace std;
class Parent
{
public:
	Parent(int a, int b)
	{
		this->a = a;
		this->b = b;
		cout << "父类构造" << endl;
	}
	~Parent()
	{
		
		cout << "父类析构" << endl;
	}
	void printP(int a, int b)
	{
		this->a = a;
		this->b = b;
		cout << "father" << endl;
	}
private:
	int a;
	int b;
 };
class  child :public Parent
{
public:
	child(int a, int b, int c) :Parent(a, b)
	{
		this->c = c;
	}
	~child()
	{
		cout << "discont" << endl;
	}
	void printC()
	{
		cout << "son" << endl;
	}
private:
	int c;
};

void play()
{
	child c1(1, 2, 3);
}


void main()
{
	//Parent p(1, 2);
	//child c1(1, 2, 3);
	play();
	return;
}
cpp 复制代码
//#include <iostream>
//#include <string>
//using namespace std;
//
//class Object
//{
//public:
//    Object(int a, int b) : a(a), b(b)
//    {
//        cout << "obj a: " << a << " b: " << b << endl;
//    }
//
//protected:
//    int a;
//    int b;
//};
//
//class Parent : public Object
//{
//public:
//    Parent(const string& p) : Object(1, 2), obj1(3, 4), obj2(5, 6), p(p)
//    {
//        cout << p << endl;
//        cout << "父类构造" << endl;
//    }
//
//    ~Parent()
//    {
//        cout << "父类析构" << endl;
//    }
//
//    void printP(int a, int b)
//    {
//        cout << "father" << endl;
//    }
//
//protected:
//    string p;
//    Object obj1;
//    Object obj2;
//};
//
//class child : public Parent
//{
//public:
//    child(const string& p) : Parent(p), obj1(3, 4), obj2(5, 6), myp(p)
//    {
//
//    }
//
//    ~child()
//    {
//        cout << "discont " << myp << endl;
//    }
//
//    void printC()
//    {
//        cout << "son " << myp << endl;
//    }
//
//private:
//    string myp;
//    Object obj1;
//    Object obj2;
//};
//
//int main()
//{
//    child c1("继承");
//    c1.printC();
//    return 0;
//}
复制代码
 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b1d26ed362de44ffaeb548db15445a19.png)

继承二义性
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/50aae96b141d473eb4c7437b1e80e780.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/374ee3c6f68a408eb91ddb4f859074c6.png)
相关推荐
fpcc3 天前
C++23中的模块应用说明之三深入分析和混合编程
c++·c++23
fpcc4 天前
C++23中的模块应用说明之二整体说明和导出控制
c++·c++23
特立独行的猫a12 天前
C++23 std::expected 详解:告别传统错误码和异常,构建现代健壮代码
开发语言·c++23·expected·错误码处理
ALex_zry16 天前
C++20和C++23 在内存管理、并发控制和类型安全相关优化方式的详细技术分析
安全·c++20·c++23
fpcc17 天前
C++23中的模块应用说明之一基础分析
c++23
Qt程序员17 天前
从 C++11 到 C++23:枚举的原理升级与实践
c++·c++23
fpcc21 天前
C++23中的自定义模块开发
c++·c++23
Ivy_belief1 个月前
C++新特性汇总:涵盖C++11到C++23
java·c++·c++11·c++23
Mr_WangAndy1 个月前
C++23新特性_简化隐士移动和auto{}
c++·c++23·c++40周年·简化隐士移动·auto新用法
fpcc1 个月前
C++23中的import使用CMake实践
c++·c++23