c++234继承

ops/2024/9/24 6:08:02/

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#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;//okb = 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
}

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

调用原则:

#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;
}
//#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)

http://www.ppmy.cn/ops/115147.html

相关文章

黑盒测试 | 挖掘.NET程序中的反序列化漏洞

通过不安全反序列化漏洞远程执行代码 img 今天&#xff0c;我将回顾 OWASP 的十大漏洞之一&#xff1a;不安全反序列化&#xff0c;重点是 .NET 应用程序上反序列化漏洞的利用。 &#x1f4dd;$ _序列化_与_反序列化 序列化是将数据对象转换为字节流的过程&#xff0c;字节流…

vue3知识汇总

vue3.x 0. changelog https://juejin.cn/post/7030992475271495711#heading-0 1. vite//要构建一个 Vite Vue 项目&#xff0c;运行&#xff0c;使用 NPM:npm init vitejs/app 项目名//使用 Yarn:yarn create vitejs/app 项目名//你会觉得非常快速的创建了项目&#xff0c;然…

淘系电商平台店铺所有商品宝贝数据API接口,用json格式示例

以下是一个淘宝店铺所有商品接口数据的 JSON 格式示例&#xff1a; { "status": "success", "message": "商品数据获取成功", "data": { "shop_name": "时尚潮流店铺", "products": [ { &q…

C++ | Leetcode C++题解之第432题全O(1)的数据结构

题目&#xff1a; 题解&#xff1a; class AllOne {list<pair<unordered_set<string>, int>> lst;unordered_map<string, list<pair<unordered_set<string>, int>>::iterator> nodes;public:AllOne() {}void inc(string key) {if (…

【c++】动态内存管理

&#x1f31f;&#x1f31f;作者主页&#xff1a;ephemerals__ &#x1f31f;&#x1f31f;所属专栏&#xff1a;C 目录 前言 一、内存区域分布 二、c中的动态内存管理方式 1. new与delete对内置类型的操作 2. new与delete对自定义类型的操作 三、operator new函数和op…

基于51单片机的矿井安全检测系统

基于51单片机的矿井安全检测系统使用51单片机作为系统主控&#xff0c;LCD1602进行显示同时系统集成了ADC0808和烟雾传感器、甲烷传感器&#xff0c;二者结合测量环境烟雾值&#xff0c;同时使用DHT11温湿度传感器获取环境温湿度值&#xff0c;使用L298N驱动风扇&#xff0c;利…

Gin渲染

HTML渲染 【示例1】 首先定义一个存放模板文件的 templates文件夹&#xff0c;然后在其内部按照业务分别定义一个 posts 文件夹和一个 users 文件夹。 posts/index.tmpl {{define "posts/index.tmpl"}} <!DOCTYPE html> <html lang"en">&…

WEB攻防- Oracle基本注入

前置知识 1.dual表 此表是Oracle数据库中的一个自带表&#xff0c;为满足查询条件而产生。与MySQL不同的是&#xff0c;在MySQL中查询语句可以直接是&#xff1a;select 1,2&#xff0c;但是在Oracle中就必须跟一个表名&#xff0c;但是如查询日期是没有表的&#xff0c;就可以…