C++深度解析教程笔记7

C++深度解析教程笔记7

  • 第13课 - 进阶面向对象(上)
    • 类和对象
    • 小结
  • 第14课 - 进阶面向对象(下)
    • 类之间的基本关系
      • 继承
      • 组合
    • 类的表示法
    • 实验-类的继承
  • 第15课 - 类与封装的概念
    • 实验-定义访问级别
      • cmd
    • 实验
    • 小结
  • 第16课 - 类的真正形态
    • 实验-struct&class
    • 实验-operator类的实现
    • 小结

本文学习自狄泰软件学院 唐佐林老师的 C++深度解析教程,图片全部来源于课程PPT,仅用于个人学习记录

第13课 - 进阶面向对象(上)

类和对象

类和对象是面向对象中的两个基本概念
类:指的是一类事物,是一个抽象的概念
对象:指的是属于某个类的具体实体
类是一种模型,这种模型可以创建出不同的对象实体
对象实体是类模型的一个具体实例

在这里插入图片描述

小结

每个类可以有多个对象
每个对象必然属于某个类

第14课 - 进阶面向对象(下)

类之间的基本关系

继承

从已存在类细分出来的类和原类之间具有继承关系(is a)
继承的类(子类)拥有原类(父类)的所有属性和行为
如下图:子类是惠普电脑和苹果电脑;父类是电脑

惠普电脑
电脑
苹果电脑

组合

一些类的存在必须依赖于其他类的关系叫组合
组合的类在某一个局部上由其他的类组成

如下图:

电脑
硬盘
CPU
内存
主板

类的表示法

在这里插入图片描述
利用继承简化重复的属性和行为

在这里插入图片描述
利用结构体、变量和函数描述

在这里插入图片描述

实验-类的继承

#include <stdio.h>struct Biology {bool living;
};struct Animal : Biology {bool movable;void findFood() { }
};struct Plant : Biology {bool growable;
};struct Beast : Animal {void sleep() { }
};struct Human : Animal {void sleep() { }void work() { }
};int main()
{return 0;
}

在这里插入图片描述

第15课 - 类与封装的概念

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

实验-定义访问级别

#include <stdio.h>#include <stdio.h>struct Biology 
{bool living;
};struct Animal : Biology 
{bool movable;void findFood(){ }
};struct Plant : Biology 
{bool growable;
};struct Beast : Animal 
{void sleep() { }
};struct Human : Animal 
{void sleep() { printf("I'm sleeping...\n");}void work() { printf("I'm working...\n");}
};struct Girl : Human
{
private:int age;int weight;
public:void print(){age = 22;weight = 48;printf("I'm a girl, I'm %d years old.\n", age);printf("My weight is %d kg.\n", weight);}
};struct Boy : Human
{
private:int height;int salary;
public:int age;int weight;void print(){height = 175;salary = 9000;printf("I'm a boy, my height is %d cm.\n", height);printf("My salary is %d RMB.\n", salary);}    
};int main()
{Girl g;Boy b;// g.age=10;// error: 'int Girl::age' is private within this contextg.print();b.age = 19;b.weight = 120;//b.height = 180;// error: 'int Boy::height' is private within this contextb.print();return 0;
}

cmd

E:\test>g++ 15-1.cppE:\test>a
I'm a girl, I'm 22 years old.
My weight is 48 kg.
I'm a boy, my height is 175 cm.
My salary is 9000 RMB.

在这里插入图片描述

实验

#include <stdio.h>int i = 1;struct Test
{
private://私有的,只允许类内部访问int i;public:int j;int getI(){i = 3;return i;}
};int main()
{int i = 2;Test test;test.j = 4;printf("i = %d\n", i);              // i = 2;printf("::i = %d\n", ::i);          // ::i = 1;//printf("test.i = %d\n", test.i);    // Error  error: 'int Test::i' is private within this contextprintf("test.j = %d\n", test.j);    // test.j = 4printf("test.getI() = %d\n", test.getI());  // test.getI() = 3return 0;
}
/*
i = 2
::i = 1
test.j = 4
test.getI() = 3*/

小结

在这里插入图片描述

第16课 - 类的真正形态

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

实验-struct&class

#include <stdio.h>struct A
{// defualt to publicint i;// defualt to publicint getI(){return i;}
};class B
{// defualt to privateint i;// defualt to privateint getI(){return i;}
};int main()
{A a;B b;a.i = 4;printf("a.getI() = %d\n", a.getI());// b.i = 4;//error: 'int B::i' is private within this context//printf("b.getI() = %d\n", b.getI());// error: 'int B::getI()' is private within this contextreturn 0;
}
/*
E:\test>g++ 16-1.cppE:\test>a
a.getI() = 4
*/

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

实验-operator类的实现

//operator.h
#ifndef _OPERATOR_H_
#define _OPERATOR_H_
class Operator
{
private:char mc;double mp0;double mp1;
public:bool setOperator(char op);void setParameter(double p0,double p1);bool result(double& r);};#endif//operator.c
#include "operator.h"
bool Operator::setOperator(char op){bool ret = false;if( (op == '+') || (op == '-') || (op == '*') || (op == '/') ){ret = true;mc = op;}else{mc = '\0';}return ret;//    bool res=false;
//    operator_cla::mc=mmc;
//    res=true;
//    return res;
}
void Operator::setParameter(double p0,double p1)
{Operator::mp0=p0;Operator::mp1=p1;
}
bool Operator::result(double& r)
{bool res=false;switch(mc){case '+':r=Operator::mp0+Operator::mp1;res=true;break;case '-':r=Operator::mp0-Operator::mp1;res=true;break;case '*':r=Operator::mp0*Operator::mp1;res=true;break;case '/':if(Operator::mp1>-0.000000001&&Operator::mp1<0.000000001){//break;}else{r=Operator::mp0/Operator::mp1;res=true;}break;}return res;}//main.c
#include <stdio.h>
#include "operator.h"int main()
{double r=0;Operator op;op.setOperator('+');op.setParameter(3.5,6);if(op.result(r)){printf("3.5+6=%f\n",r);}op.setOperator('-');op.setParameter(3.5,6);if(op.result(r)){printf("3.5-6=%f\n",r);}op.setOperator('/');op.setParameter(3.5,0);if(op.result(r)){printf("3.5/0=%f\n",r);}else{printf("3.5/0 calculate error\n");}return 0;
}
/*
3.5+6=9.500000
3.5-6=-2.500000
3.5/0 calculate error
*/

小结

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/682376.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基士得耶CP 6303c速印机不制版故障检修

故障&#xff1a;基士得耶CP 6303c经常提示版纸卡纸&#xff0c;重设版纸 版纸还没下滚筒&#xff0c;卡在版纸仓里面,手动滚动裁纸刀可以解决&#xff0c;但第二天又是这个毛病&#xff1b; 版纸定位传感器的灵敏度调节一下,然后给机器全面做个清洁大保养&#xff0c;尤其是传…

wordpress子比主题美化-为图文列表封面添加动态缩略图特效 多种效果演示

wordpress子比主题-为图文列表文章封面添加动态缩略图特效 给自己子比主题加一个列表文章封面添加动态缩略图 直接复制以下代码&#xff0c;添加到主题自定义CSS代码中即可&#xff0c;下图为效果演示 wordpress子比主题-为图文列表文章封面添加动态缩略图特效 给自己子比主题…

linux中进程相关概念(一)

什么是程序&#xff0c;什么是进程&#xff0c;有什么区别&#xff1f; 程序是静态的概念&#xff0c;当我们使用gcc xxx.c -o pro进行编译时&#xff0c;产生的pro文件&#xff0c;就是一个程序。 进程是程序的一次运行活动&#xff0c;通俗点就是说程序跑起来了就是进程。 …

【springboot基础】如何搭建一个web项目?

正在学习springboot&#xff0c;还是小白&#xff0c;今天分享一下如何搭建一个简单的springboot的web项目&#xff0c;只要写一个类就能实现最基础的前后端交互&#xff0c;实现web版helloworld &#xff0c;哈哈&#xff0c;虽然十分简陋&#xff0c;但也希望对你理解web运作…

网络新手如何上手水牛社软件?我的建议与看法

水牛社是一款专为电脑用户设计的软件&#xff0c;拥有明确的著作权&#xff0c;其核心功能在于发布和整合各类网络活动任务资源、教程等&#xff0c;内容多元且不设固定分类。其靠谱程度取决于你对软件的了解程度和个人需求的适配性。 软件内部包含五个主要栏目&#xff0c;大…

(八)JSP教程——application对象

application对象是一个比较重要的对象&#xff0c;服务器在启动后就会产生这个application对象&#xff0c;所有连接到服务器的客户端application对象都是相同的&#xff0c;所有的客户端共享这个内置的application对象&#xff0c;直到服务器关闭为止。 可以使用application对…

AlphaFold3: Google DeepMind的的新突破

AlphaFold 3的论文今天在Nature期刊发表啦!这可是AI在生物领域最厉害的突破的最新版本。AlphaFold-3的新招就是用扩散模型去"画出"分子的结构。它一开始先从一团模模糊糊的原子云下手,然后慢慢透过去噪把分子变得越来越清楚。 Alphafold3 我们活在一个从Llama和Sora那…

STM32理论 —— μCOS-Ⅲ(新)

文章目录 1. 任务调度器1.1 抢占式调度 μCos-Ⅲ全称是Micro C OS Ⅲ&#xff0c;由Micriμm 公司发布的一个基于C 语言编写的第三代小型实时操作系统(RTOS)&#xff1b; RTOS 与裸机相比最大的优势在于多任务管理与实时性&#xff0c;它提供了多任务管理和任务间通信的功能&a…

如何切换PHP版本

如果服务器上安装了多个php&#xff0c;可能会导致默认的php版本错误&#xff0c;无法启动swoole等服务&#xff0c; 查看命令行的php版本方法&#xff1a;https://q.crmeb.com/thread/9921 解决方法如下&#xff0c;选一个即可&#xff1a; 一、切换命令行php版本&#xff…

Zabbix5.0——安装与部署

目录 一、zabbix-server(192.168.206.134) 监控方 1. 环境准备 2.安装zabbix 2.1 准备zabbix-repo 2.2清理缓存 2.3安装zabbix主包&#xff08;服务器和代理&#xff09; 2.4安装zabbix前端包 3. 数据库安装 3.1 授权zabbix账号 3.2导入数据库&#xff08;初始化zabbix&#x…

震惊,现在面试都加科技与狠货了

震惊&#xff0c;现在面试都加科技与狠货了 生成式AI盛行的现在&#xff0c;程序员找工作变容易了吗我和老痒喝着大酒&#xff0c;吃着他的高升宴&#xff0c;听他说他面试的各种细节&#xff0c;老狗我只恨自己动作慢了一步&#xff0c;不然现在在那侃侃而谈的就是我了。 面试…

Unity 修复Sentinel key not found (h0007)错误

这个问题是第二次遇到了&#xff0c;上次稀里糊涂的解决了&#xff0c;也没当回事&#xff0c;这次又跑出来了&#xff0c;网上找的教程大部分都是出自一个人。 1.删除这个路径下的文件 C:\ProgramData\SafeNet Sentinel&#xff0c;注意ProgramData好像是隐藏文件 2.在Windows…