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

C++day5(静态成员、类的继承、多继承)

一、Xmind整理:

二、上课笔记整理:

1.静态数据成员+静态成员函数(银行账户实例)

#include <iostream>using namespace std;class BankAccount
{
private:double balance;   //余额static double interest_rate;   //利率
public://无参构造函数BankAccount (){}//有参构造函数BankAccount(double balance):balance(balance){}//静态成员函数,获取当前利率static double getInterestRate(){return interest_rate;}//静态成员函数,设置当前利率、static double setInterestRate(double rate){interest_rate = rate;}//静态成员函数,获取连本带利的全额static double getLastMoney(BankAccount &account){return account.balance*(1+interest_rate);}
};double BankAccount::interest_rate = 0.05;int main()
{cout << "当前利率:" << BankAccount::getInterestRate() << endl;BankAccount::setInterestRate(0.03);cout << "当前利率:" << BankAccount::getInterestRate() << endl;BankAccount account1(2000.0);BankAccount account2(3000.0);cout << "第一个人连本带利的余额为:" << BankAccount::getLastMoney(account1) << endl;return 0;
}

2.类的继承

#include <iostream>using namespace std;//封装 人  类  父类/基类
class Person
{
private:string name;
protected:int age;
public:int h;
public://无参构造函数Person(){cout << "父类的无参构造函数" << endl;}//有参构造Person(string name, int age, int h):name(name),age(age),h(h){cout << "父类的有参构造函数" << endl;}//拷贝构造函数Person(const Person & other):name(other.name),age(other.age),h(other.h){cout << "父类的拷贝构造函数"  << endl;}//拷贝赋值函数Person & operator=(const Person &p){name = p.name;age = p.age;h = p.h;cout << "父类的拷贝赋值函数" << endl;return  *this;}void show(){cout << "父类的show" << endl;}
};//封装 学生 类   共有继承人 类
class Stu:public Person   //子类 、派生类
{
private:int id;int math;public://无参构造函数Stu(){cout << "子类的无参构造函数" << endl;}//有参构造函数Stu(string name, int age, int h, int id, int math):Person(name,age,h),id(id),math(math){cout << "子类的有参构造函数" << endl;}//拷贝构造函数Stu(const Stu & s):id(s.id),math(s.math),Person(s){cout << "子类的拷贝构造函数" << endl;}//拷贝赋值函数Stu & operator=(const Stu & s){Person::operator=(s);id = s.id;math = s.math;cout << "子类的拷贝赋值函数" << endl;return *this;}void show(){cout << "子类的show" << endl;cout << h << endl; //通过共有继承,类外、子类可以访问父类共有成员cout << age << endl; //通过共有继承,子类可以访问父类保护成员,类外不可以访问//cout << name << endl;//通过共有继承,子类不可访问父类私有成员,类外不可以访问}
};int main()
{Stu s("zhangsan",12,190,1001,78);Stu s2=s;Stu s3;s3 = s2;//    s.show();
//    s.Person::show();return 0;
}

3.多继承

#include <iostream>using namespace std;class Sofa
{
private:string sitting;
public://无参构造函数Sofa(){cout << "沙发的无参构造" << endl;}//有参构造函数Sofa(string s):sitting(s){cout << "沙发的有参构造" << endl;}void display(){cout << sitting << endl;}
};class Bed
{
private:string sleep;
public://无参构造函数Bed(){cout << "床的无参构造" << endl;}//有参构造函数Bed(string(s)):sleep(s){cout << "床的有参构造" << endl;}void display(){cout << sleep << endl;}
};class Sofa_Bed:public Bed,public Sofa
{
private:int w;
public://无参构造函数Sofa_Bed(){cout << "沙发床的无参构造" << endl;}//有参构造函数Sofa_Bed(string sit, string s, int w):Bed(s),Sofa(sit),w(w){cout << "沙发床的有参构造" << endl;}
};
int main()
{Sofa_Bed(s);Sofa_Bed s1("可坐","可躺",123);s1.Sofa::display();s1.Bed::display();return 0;
}
http://www.lryc.cn/news/139731.html

相关文章:

  • 2023MySQL+MyBatis知识点整理
  • 【随笔】如何使用阿里云的OSS保存基础的服务器环境
  • 汽车电子笔记之:AUTOSA架构下的多核OS操作系统
  • 解决华为云ping不通的问题
  • 数据结构入门 — 链表详解_双向链表
  • 时序预测 | MATLAB实现PSO-KELM粒子群算法优化核极限学习机时间序列预测(含KELM、ELM等对比)
  • SSL/TLS协议的概念、工作原理、作用以及注意事项
  • [Stable Diffusion教程] 第一课 原理解析+配置需求+应用安装+基本步骤
  • uniapp结合Canvas+renderjs根据经纬度绘制轨迹(二)
  • VR全景加盟会遇到哪些问题?全景平台会提供什么?
  • 如何进行微服务的集成测试
  • spark grpc 在master运行报错 exitcode13 User did not initialize spark context
  • nginx 反向代理的原理
  • 【SpringBoot】第二篇:RocketMq使用
  • 飞天使-vim简单使用技巧
  • 分布式搜索引擎----elasticsearch
  • AnnotationConfigApplicationContext类和ClasspathXmlApplicationContext类的区别?
  • 使用VSCode SSH实现公网远程连接本地服务器开发的详细教程
  • Codeforces Round 894 (Div. 3)
  • ACL2023 Prompt 相关文章速通 Part 1
  • “R语言+遥感“水环境综合评价方法
  • 数据结构之哈希
  • 可视化绘图技巧100篇基础篇(七)-散点图(一)
  • 关于什么是框架
  • iOS开发Swift-集合类型
  • 【keepalived双机热备与 lvs(DR)】
  • C++笔记之静态成员函数可以在类外部访问私有构造函数吗?
  • 最新SQLMap进阶技术
  • 【BurpSuite常用功能介绍】
  • Leetcode 108. 将有序数组转换为二叉搜索树