当前位置: 首页 > news >正文

c++234继承

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

#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.lryc.cn/news/444320.html

相关文章:

  • Axios 封装网络请求
  • LeetCode 面试经典150题 190.颠倒二进制位
  • vulhub搭建漏洞环境docker-compose up -d命令执行报错以及解决方法汇总
  • C++ 简介
  • shardingjdbc分库分表原理
  • C++泛型编程:模版
  • 一道涉及 Go 中的并发安全和数据竞态(Race Condition)控制的难题
  • 如何降低H5商城系统的开发成本
  • 为什么越来越多的网工运维转行网络安全?_idc运维转网络安全工程师_系统运维转行网安
  • 【TabBar嵌套Navigation案例-产品推荐页面-UICollectionView-结合xib使用 Objective-C语言】
  • java.nio.ByteBuffer的 capacity, limit, position, mark
  • 握手传输 状态机序列检测(记忆科技笔试题)_2024年9月2日
  • 电商跨境电商商城系统/网上商城接口/电商数据接口详情
  • openFrameworks_如何使用ofxXmlSettings和ofxGui来创建识别界面
  • 180多个GIS地理空间定义术语中英文对照配图
  • Vue(14)——组合式API①
  • 【图像检索】基于颜色模型的图像内容检索,matlab实现
  • 看过来——量子计算中一个神奇符号的解释
  • 传输层 IV(TCP协议——流量控制、拥塞控制)【★★★★】
  • Java设计模式全面解析
  • spring全家桶使用教程
  • REST-系统架构师(六十九)
  • SAP B1 营销单据 - 复制从复制到总结
  • css设置overflow:hiden行内元素会发生偏移的现象
  • 使用多个 GitHub 账号的 SSH 配置与常见问题排查
  • sql语法学习
  • 滚雪球学SpringCloud[5.3讲]: 配置管理中的高可用与容错
  • 电商安全新挑战:筑起数字防御长城,守护业务与数据安全
  • Python 单元测试:深入理解与实战应用20240919
  • 二、MySQL环境搭建