C++学习日志
记录C++学习过程中的相关代码
@白圭
积沙成塔
展开
-
从C到C++的进阶--第一个C++程序
前言这篇文章主要讲述最基本的C++程序,pause的使用。一、pandas是什么?#include<iostream>using namespace std;int main(){ system("pause"); return 0;}总结代码运行成功,页面会停在"请按任意键继续页面"。...原创 2022-02-05 00:03:56 · 682 阅读 · 10 评论 -
C++学习日志1--引用
前言这篇文章主要讲述C++中"引用"的用法。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;int main(){ //引用基本语法 //数据类型 &别名=原名 int a = 10; //创建引用 int &b = a; cout << "a=" << a << endl; cout << "b=" <<原创 2022-02-05 00:04:12 · 272 阅读 · 0 评论 -
C++学习日志2--引用交换变量值
前言这篇文章主要讲述如何通过引用,使得参数的发生改变。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;//交换函数//1.值传递 无法交换void my_swap01(int a, int b){ int temp = a; a = b; b = temp;}//2.地址传递 可以交换void my_swap02(int *a, int *b){ int temp = *a;原创 2022-02-05 00:04:22 · 403 阅读 · 0 评论 -
C++学习日志3--引用作为函数的返回值
前言这篇文章主要讲述引用作为函数的返回值。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;//1.不要返回局部变量的引用 此操作非法int& test01(){ int a = 10;//局部变量存放在四区中的 栈区 return a;}//2.函数的调用可以作为左值int& test02(){ static int a = 10;//静态变量,存放在全局区,全局区上的数原创 2022-02-05 00:34:27 · 240 阅读 · 0 评论 -
C++学习日志4--const修饰引用形参 防止误操作
前言C++中可以用const来修饰引用的形参,防止函数运行时的误操作一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;//打印数据函数//常量引用//使用场景:用来修饰形参,防止误操作void showvalue(const int &val){ cout << val << endl;}int main(){ //int a = 10; //int原创 2022-02-07 00:04:51 · 351 阅读 · 0 评论 -
C++学习日志5--函数的默认参数
前言这篇文章主要讲述C++中函数的默认参数相关知识。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;//函数的默认参数//可以调用默认,也可以传入int func(int a,int b=20, int c=30) { return a + b + c;}int main(){ int a=func(10); int b = func(10, 30); cout << a &原创 2022-02-07 00:16:37 · 201 阅读 · 0 评论 -
C++学习日志6--函数的占位参数
前言这篇文章主要讲述C++中函数占位参数的使用一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;//函数的占位参数 (数据类型 a,数据类型 )//目前阶段的占位参数,还用不到,后面课程中会使用到//占位参数,也可以是默认参数int func(int a,int) { cout << "this is func" << endl;}int main(){ func(1原创 2022-02-07 00:25:39 · 300 阅读 · 0 评论 -
C++学习日志7--函数的重载
前言这篇文章主要讲述C++中函数重载的知识。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;//函数的重载//可以让函数名相同,提高复用性//函数重载的满足条件//1.必须在同一个作用域下//2.函数名称相同//3.函数的参数类型不同,或者个数不同,或者顺序不同int func() { cout << "func 的调用" << endl;}int func(in原创 2022-02-07 12:37:48 · 326 阅读 · 0 评论 -
C++学习日志8--属性和行为作为一个整体
前言这篇文章主要讲述C++中类的初识—属性和行为是一个类的基本要素。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>using namespace std;const double pi = 3.14;//封装、继承、多态//对象:具有属性和行为//设计一个圆类,求圆的周长//圆求周长的公式:2*pi*半径//class 代表设计一个类,类后面紧跟着的就是类名称class Circle{ //访问权限 //公共权限public原创 2022-02-07 16:35:40 · 462 阅读 · 0 评论 -
C++学习日志9---设计一个学生类
前言这篇文章中主要讲述C++中如何设计一个学生类。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>#include<string>using namespace std;//设计一个学生类,属性有姓名和学号//可以给姓名和学号赋值,可以显示学生的姓名和学号//设计学生类class Student{public: //公共权限 //属性 string m_name;//姓名 int m_id;//学号原创 2022-02-07 16:51:47 · 1795 阅读 · 0 评论 -
C++学习日志10--公开、保护、私有三种权限
前言这篇文章主要讲述C++中公开、保护、私有三种权限。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>#include<string>using namespace std;//访问权限//三种//公共权限 public 成员类内可以访问 类外可以访问//保护权限 protected 成员类内可以访问 类外不可以访问 子也可以访问父中的保护内容//私有权限 private 成员类内可以访问 类外不可以访问 子原创 2022-02-10 18:01:17 · 1399 阅读 · 0 评论 -
C++学习日志11--class和struct的区别
前言这篇文章主要讲述C++中class和struct的区别。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>#include<string>using namespace std;//struct 默认权限为公共//class 默认权限为私有class C1{ int m_a;//默认权限是私有 private};struct C2{ int m_a;//默认权限 是公共};int main(){ //原创 2022-02-10 18:03:01 · 439 阅读 · 0 评论 -
C++学习日志12--成员属性私有化
前言这篇文章主要讲述C++中成员属性私有的相关知识。一、代码//跟着视频学习的代码,分享记录一下#include<iostream>#include<string>using namespace std;//成员属性私有//1.可以控制读写权限//2.可以写,可以检测数据的有效性//设计人类class Person{public: //设置姓名 void set_name(string name) { m_name = name; } /原创 2022-02-10 18:04:30 · 526 阅读 · 0 评论 -
C++学习日志13--类的应用-设计一个立方体
前言这篇文章主要讲述C++中如何利用类设计一个立方体一、代码//跟着视频学习的代码,分享记录一下#include<iostream>#include<string>using namespace std;//立方体类设计//1.创建立方体类//2.设计属性//3.设计行为 获取立方体的面积和体积//4.分别利用全局函数和成员函数 判断两个立方体是否相等class Cube{public: //设置长度 void set_length(int leng原创 2022-02-10 18:05:54 · 983 阅读 · 0 评论 -
C++学习日志14--设计一个圆类
前言这篇文章主要讲述C++中如何设计一个圆类一、代码//跟着视频学习的代码,分享记录一下#include<iostream>#include<string>using namespace std;//点和圆关系案例class Point{public: //设置x void setx(int x) { m_x = x; } //获取x int getx() { return m_x; } //设置y void sety(int y)原创 2022-02-14 23:39:05 · 1881 阅读 · 0 评论 -
C++学习日志15--将文件拆分到.c、.h中
前言这篇文章主要讲述C++中如何把类拆分到单独的.c和.h文件中1.C文件#include"circle.h"//设置半径void Circle::setr(int r){ m_r = r;}//获取半径int Circle::getr(){ return m_r; }//设置圆心void Circle::setcenter(Point center){ m_Center = center;} //获取圆心Point Circle::getcenter(){原创 2022-02-14 23:53:01 · 574 阅读 · 0 评论 -
C++学习日志--笔记
前言这篇文章主要记录系统学习C++过程中的笔记。1.枚举变量:(1)初始值赋0,后面依次加一。(2)若间隔赋值,后一个值为前一个值加1。2.strcmp(s1,s2):字符串比较若a,b完全相等,则返回0;若a大于b,则返回大于0的数值;若a小于b,同样返回小于0的数值.3.static 静态变量 全局只会执行一次4.lvalue和rvaluelvalue:变量在内存中的位置。通过它能够找到内存中存放的变量(location value);rvalue:存放在lvalue对应的内存原创 2022-04-11 09:53:37 · 139 阅读 · 0 评论 -
C++学习日志16--获取当前C++版本、设定命名空间、引用
目录一、获取当前C++版本号二、设定命名空间三、C++中的引用一、获取当前C++版本号int main(){ if (__cplusplus == 201703L) std::cout << "C++17\n"; else if (__cplusplus == 201402L) std::cout << "C++14\n"; else if (__cplusplus == 201103L) std::cout << "C++11\n";原创 2022-04-11 19:09:36 · 1447 阅读 · 0 评论 -
C++学习日志17--布尔类型、列表初始化与强制类型转化
目录一、布尔类型二、列表初始化与强制类型转化一、布尔类型#include <iostream>int main(){ bool isAlpha; isAlpha = false; if (!isAlpha) { std::cout << "isAlpha=" << isAlpha << std::endl; std::cout << std::boolalpha <&原创 2022-04-11 19:16:33 · 313 阅读 · 0 评论 -
C++学习日志18--nullptr空指针、内存的申请和释放
目录一、nullptr空指针二、内存的申请和释放一、nullptr空指针int main(){ auto x { 10 }; int* p = nullptr; int* q; q = & x; int* p, a = 3; p = &a; return 0; }二、内存的申请和释放int main(){ int* p = nullptr; int* q { nullptr };原创 2022-04-11 19:20:27 · 683 阅读 · 0 评论 -
C++学习日志19--变量的定义方法auto、declpyte
目录一、auto二、declpyte一、auto#include <iostream>#include <typeinfo>using std::cout;using std::endl;using std::cin;auto max(int x, int y){ return x > y ? x : y;}int main(){ //auto 变量必须在定义时初始化 auto x = 3; auto y{ 42 };原创 2022-04-11 19:24:53 · 300 阅读 · 0 评论 -
C++学习日志20--变量的作用域与重载
目录一、变量的作用域二、函数的重载一、变量的作用域#include <iostream>#include <string>using namespace std;int i = 100;int main(){ //Variable Scope 局部变量i //int i{100} for (int i = 1; i < 5; i++) { cout << i << " "; //C++11 std::to_s原创 2022-04-13 10:55:25 · 132 阅读 · 0 评论 -
C++学习日志21--常量与指针、带初始化器的switch
目录一、常量与指针二、带初始化器的switch一、常量与指针#include <iostream>#include <typeinfo>using std::cout;using std::endl;using std::cin;int main(){ //变量声明 const int x = 42; //常量不可变 //x=21; 错误 //常量指针所指数据不可变 int y = 2; const int* p = &x; //(*原创 2022-04-13 13:02:17 · 317 阅读 · 0 评论 -
C++学习日志22--对象的拷贝、匿名对象的使用
目录一、对象的拷贝二、匿名对象的使用一、对象的拷贝#include<iostream>class Square {private: double side = 1.0;public: Square() = default; Square(double side) { this->side = side; } double getArea() { return (side * side); }};int main(){ Square s1原创 2022-04-13 20:59:28 · 181 阅读 · 0 评论 -
C++学习日志23--对象指针
目录一、对象指针二、辅助文件一、对象指针#include<iostream>#include"circle.h"int main(){ auto* c1 = new Circle{ 1.0 }; Circle c3{ 2.0 }; auto c2 = &c3; std::cout << (*c1).getArea() << std::endl; std::cout << c2->getAre原创 2022-04-13 21:02:59 · 332 阅读 · 0 评论 -
C++学习日志24---string类和array类
目录一、string类(字符串)二、array类(数组)一、string类(字符串)#include<iostream>#include<string>using namespace std;int main(){ //创建字符串 string s{ "Hello" }; //clear s.clear(); //用数组为字符串赋值 char arr[]{ 'W','o','r','l','d' }; s +=原创 2022-04-14 16:57:33 · 661 阅读 · 0 评论 -
C++学习日志25---默认构造函数与对象就地初始化
目录一、默认构造函数二、对象就地初始化一、默认构造函数#include<iostream>using namespace std;class Circle{private: double radius;public: Circle() = default; Circle(double r) { radius = r; } double getArea() { return 3.14 * rad原创 2022-04-14 16:59:50 · 441 阅读 · 0 评论 -
C++学习日志26--对象数组
目录一、对象数组的构建二、相关.c三、相关.h一、对象数组的构建#include<iostream>#include"circle.h"int main(){ Circle ca1[]{ Circle{1.0},Circle{2.0},Circle{3.0} }; Circle ca2[]{ 10.0,11.0,12.0 }; ca1[2].setRadius(4.0); ca2[1].setRadius(100.0); auto are原创 2022-04-14 17:05:08 · 312 阅读 · 0 评论 -
C++学习日志27--对象作为函数的参数
目录一、对象作为函数的参数二、相关.c三、相关.h一、对象作为函数的参数#include<iostream>#include"circle.h"void print(Circle& c){ std::cout << c.getArea() << std::endl;}void print(Circle* c){ std::cout << c->getArea() << std::endl;}i原创 2022-04-14 17:08:03 · 335 阅读 · 0 评论 -
C++学习日志28--对象作为函数的返回值
目录一、对象作为函数的返回值二、相关.c三、相关.h一、对象作为函数的返回值#include<iostream>#include"circle.h"int main(){ Circle c{ 1.0 }; std::cout << c.setRadius(2.0).setRadius(3.0).getArea() << std::endl;}结果如上图所示。二、相关.c#include"circle.h"Circle::原创 2022-04-14 17:10:19 · 2686 阅读 · 0 评论 -
C++学习日志29--代理构造、创建不可变对象
目录一、代理构造、创建不可变对象二、相关.h一、代理构造、创建不可变对象#include<iostream>#include"Date.h"#include"Employee.h"//任务4:创建Employ对象,然后修改其生日/*注释:创建不可变对象1.所有数据成员都是私有的2.没有set函数3.不返回成员的指针和引用*///下面的例子,返回了指针,也有set函数,所以创建的不是不可变对象int main(){ Employee e; //1.setter原创 2022-04-16 16:52:55 · 343 阅读 · 0 评论 -
C++学习日志30--断言和静态断言、静态成员函数
目录一、断言和静态断言二、静态成员函数一、断言和静态断言#include<iostream>#include<array>#include<cassert> //使用assert()断言必须包含的头文件using std::cout;using std::endl;//任务1:用递归计算factorial,用assert检查3的竭诚//任务2:将factorial变成常量表达式,用static_assert检查3的阶乘;//任务3:创建factoria原创 2022-04-16 16:57:46 · 204 阅读 · 0 评论 -
C++学习日志31------拷贝构造、析构函数
目录一、析构函数二、拷贝构造一、析构函数#include<iostream>#include<string>//任务1:定义Date类class Date{private: int year = 2019, month = 1, day = 1;public: int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } void原创 2022-04-16 17:04:15 · 301 阅读 · 0 评论 -
C++学习日志32------Vector容器、结构化绑定
目录一、Vector容器二、结构化绑定一、Vector容器Vector容器是一个长度可变的数组(称为向量)。#pragma once#include<vector>#include<iostream>#include<string>#define PRINT(x) std::cout<<#x<<":"<<x<<std::endl;template<typename T>std::ostrea原创 2022-04-17 18:34:26 · 602 阅读 · 0 评论 -
C++学习日志33-----栈操作与简单的继承
目录一、栈操作二、简单的继承一、栈操作#include<iostream>#include<cassert>#include"StackOfIntegers.h"//任务2:创建Stack对象,展示入栈出栈操作int main(){ StackOfIntegers s1{}; for (int i = 0; i < 5; i++) //底 1 2 3 4 5 顶 { s1.push(i + 1); } std::cout << "St原创 2022-04-17 18:42:17 · 228 阅读 · 0 评论 -
C++学习日志34--深拷贝和浅拷贝
目录一、深拷贝和浅拷贝一、深拷贝和浅拷贝深拷贝与浅拷贝核心区别:浅拷贝:拷贝变量的地址,等于指向同一个存储空间。深拷贝:拷贝变量的内容,等于创建了新的同样的内容。#include<iostream>#include<string>#include"Date.h"#include"Employee.h"//任务1:构造Employee对象e1,拷贝构造e2//任务2:调式模式观察e1和e2的biethday成员//任务3:添加拷贝构造函数实现深拷贝//任务4:原创 2022-04-17 18:46:20 · 335 阅读 · 0 评论 -
C++学习日志35-----基类默认构造函数、构造与析构
目录一、基类默认构造函数二、构造与析构一、基类默认构造函数#include<iostream>using std::cout;using std::endl;//任务:基类默认构造函数的使用//B:public ;class A{ //base classpublic: A() = default; A(int i) { cout << "A(" << i << ")" << endl; }};class B :p原创 2022-04-19 13:32:17 · 564 阅读 · 0 评论 -
C++学习日志36-----继承与构造、控制访问属性
目录一、继承与构造二、控制访问属性一、继承与构造#include<iostream>using std::cout;using std::endl;//任务1:继承构造函数// 创建基类B及构造函数B(int),B( char)和派生类D; D中不继承/继承B的ctor时的效果//任务2:派生类中调用基类构造函数//D中增加成员double x; 及D(double), 在D(double)初始化列表调用B(i)并初始化x//任务3:派生类先调基类ctor,再构造内嵌对象/原创 2022-04-19 13:35:35 · 472 阅读 · 0 评论 -
C++学习日志37------纯虚函数
目录一、纯虚函数二、相关文件一、纯虚函数纯虚函数不可实例化,且其派生类必须实例化。#include<iostream>#include"Shape.h"#include"circle.h"using std::cout;using std::endl;int main(){ //Shape s{Color::black,true} 含有纯虚函数,不可以实例化 Circle c{ 1.2,Color::green,false }; Shape* p = &原创 2022-04-19 13:40:08 · 147 阅读 · 0 评论 -
C++学习日志38---运行时多态、虚函数与覆写
目录一、运行时多态一、运行时多态#include<iostream>#include<string>using std::cout;using std::endl;//任务1:创建A/B/C三个类,B继承A,C继承B,ABC均有toString函数//任务2:重载print函数,接受B/C类型参数,调用toString()class A{public: virtual std::string toString() { return "A"; }};cla原创 2022-04-19 13:41:48 · 571 阅读 · 0 评论