C++基础知识
C++基础知识
Charlesffff
守得云开见月明,静待花开终有期
展开
-
STL:二进制文件读写(将Student类对象st1、st2、st3写入data.txt。再把data.txt内容写入data1.txt,之后输出data1.txt内容)
将Student类对象st1、st2、st3写入data.txt。再把data.txt内容写入data1.txt,之后输出data1.txt内容原创 2022-01-07 13:50:58 · 833 阅读 · 0 评论 -
文件输入输出(文本文件、二进制文件)(ifstream、ofstream)
source.txt文件target.txt文件功能:将source.txt文件中内容,写进target.txt文件中。一.读文件两种方法,如下:#include <iostream>#include <fstream>//引入文件读写的头文件using namespace std;//文件路径string source = "C:\\Users\\19a405\\Desktop\\source.txt";string target = "C:\\Users原创 2022-01-07 10:27:42 · 2925 阅读 · 0 评论 -
C++:变量作用域:局部变量、全局变量、文件级变量
变量作用域:根据变量可被访问到的范围定义。一.局部变量只在{ }括号内有效。例如:下面四处a在各自{ }范围内可见。class Person {private: int a;//1};void func1() { int a = 10;//2 { int a = 10;//3 }}int main() { int a = 10;//4}二.全局变量在整个工程文件中都可见,包括参与链接的文件。同时,这样全局都可见,也会导致访问二义性。例如:在下面2个文件中都存在a变量原创 2022-04-07 09:34:20 · 1331 阅读 · 0 评论 -
C++:八进制、十进制、十六进制 hex、oct、dec使用
cout << "Hex(后面输出16进制):" << hex << 255 << endl;cout << "Oct(后面输出8进制):" << oct << 255 << endl;cout << "Dec(后面输出10进制):" << dec << 255 << endl;16进制:255/16=15余15,15/16=0余15;(15)(15)=f原创 2022-04-02 14:02:51 · 4348 阅读 · 0 评论 -
C++:类中函数重载(+const)
一.类中函数加const重载例子1:class VAC{public: int f() const { return 1; } int f() { return 2; }//重载上面函数};int main(){ VAC v1; const VAC v2; cout << v1.f() << v2.f(); return 0;}函数int f() const 中const修饰this指针。const VAC v2,const也修饰this指针,因此匹原创 2022-03-28 11:13:38 · 1030 阅读 · 0 评论 -
C++:cout.width()与cout.fill()
一.cout.width()cout.width()用来调整字段宽度,默认为0;cout.width(6)为调整字段宽度为6;cout.width()默认为右对齐,且只对后面第一个输入有作用。例子1:cout.width(5); cout << 1 ;cout.width(5)设置5个字段宽度,所以1前面有4个空格,一共5个字符。例子2:cout.width(5); cout << 1 << 2;cout.width()默认为右对齐,且只对后面原创 2022-03-25 10:25:01 · 6252 阅读 · 1 评论 -
C++:静态联编、动态联编
一.静态联编、动态联编定义将源代码中的函数调用解释为执行特定的函数代码块称为函数名联编。在C语言中,这非常简单,因为每个函数名都对应一个不同的函数。在C++中,由于函数重载的缘故,这项任务变得十分复杂。编译器必须查看函数参数以及函数名才能确定使用哪个函数。然而,C/C++编译器可以在编译过程中完成这项联编。在编译过程中进行联编称为静态联编,又称为早期联编。然而,虚函数使这项工作变得更困难。使用哪一个函数是不能再编译时确定的,因为编译器不知道用户将选取那种类型对象。所以编译器必须生成能在运行时选择正确的虚函原创 2022-03-24 15:08:45 · 624 阅读 · 0 评论 -
(重载+、*、-、<<、=)操作符重载、函数模板、类中有指针变量联合使用
函数功能:实现集合的并集(重载+号)、交集(重载*号)、补集(重载-号)文件目录:1.set.h#pragma once#ifndef _SET#define _SET#include <iostream>using namespace std;template<class T>class set{public: //友元函数、在实现类模板时,前面必须加上 template<typename T> template<typename原创 2022-03-16 10:53:20 · 390 阅读 · 0 评论 -
随机数:rand()、srand()产生随机整数、浮点数
rand()产生的随机数每次都是相同的值。#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(void) { cout << rand() % 10 + 1 << endl;//产生1-10随机数 cout << rand() % 10 + 1 << endl; cout << ran原创 2022-03-08 17:54:13 · 2131 阅读 · 0 评论 -
C++:char数组、自定义copy数组
一.’\0’问题1.char数组大小为3,只给两个元素,那么最后一个默认为’\0’。 char res[3] = { 'a','b' }; for (int i = 0; res[i] != '\0'; i++) { cout << res[i]; }输出:2.char数组大小为3,给三个元素。 char res[3] = { 'a','b' ,'c'}; for (int i = 0; res[i] != '\0'; i++) { cout << res原创 2022-03-03 16:44:32 · 3900 阅读 · 0 评论 -
C++:有效位、精确到小数点后n位
#include <iostream>#include <iomanip>//使用setprecisionusing namespace std;int main() { double y = 3.141000000000; cout << y << endl;//普通输出,精确到小数点后6位,但是遇到0自动不输出 cout << setprecision(7) << y << endl;// 有效位(精确7位原创 2022-02-28 11:34:28 · 1548 阅读 · 0 评论 -
C++ qsort 排序函数
是C中的函数,需要添加:#include <cstdio>用法:qsort(起始地址,数组长度,元素大小,比较函数)例子:#include <iostream>#include <cstdio>using namespace std;//比较函数int fcmp(const void* elem1, const void* elem2) {//void* 是万能指针 const int* i1 = (const int*)elem1; const原创 2022-02-11 15:21:15 · 662 阅读 · 0 评论 -
C++:初始化char* 问题:const char类型的值不能用于初始化char类型的实体
错误:const char类型的值不能用于初始化char类型的实体如下:修改:方法一:const char* source = "abcdefg";方法二:char s[] = "abcdefg";char* source = s;原创 2022-01-06 13:41:40 · 3115 阅读 · 0 评论 -
C++:左值、右值、取左值引用、取右值引用
int q = 1;右值:1,是一个临时变量。左值:q,它是一个变量,可以在代码多个地方使用。如下:q = q + 1;q = q * q;左值引用、右值引用:void test1(int& a) { cout << "左值引用:a=" << a << endl;}void test2(int&& b) { cout << "右值引用:b=" << b << endl;}int mai原创 2022-01-05 09:50:11 · 225 阅读 · 0 评论 -
C++纯虚函数和抽象类
一.基本概念1. 纯虚函数是一个在基类中说明的虚函数,在基类中没有定义,要求任何派生类都定义自己的版本。2. 纯虚函数为个派生类提供一个公共界面(接口的封装和设计、设计的模块功能划分)。3. 一个具有纯虚函数的基类称为抽象类。4. 抽象类不能被实例化。5. 子类继承抽象类,如果不重写这个虚函数,子类依然还是抽象类。6. 抽象类中有多个纯虚函数,子类每一个都要重写,否则依然不能 被实例化。语法:virtual 类型 函数名 (参数表)=0;二.使用1.不正确使用方法//图形类如果说一原创 2021-12-25 22:09:51 · 663 阅读 · 1 评论 -
C++父类指针和子类指针的步长
一.先看一段代码class Parent {public: Parent(int a) { this->a = a; } virtual void print() { cout << "Parent::print a=" << a << endl; } int a;};class Child :public Parent {public: Child(int a) :Parent(a) { } virtual void print(原创 2021-12-25 18:15:02 · 306 阅读 · 0 评论 -
C++vptr指针分布初始化
一.先看代码class Parent{public: Parent(int a) { cout << "Parent(int )..." << endl; this->a = a; } virtual void print() { cout << "parent::print().....a="<<a << endl; }private: int a;};class Child :public Parent原创 2021-12-25 13:56:31 · 397 阅读 · 0 评论 -
C++验证虚函数指针vptr存在
原创 2021-12-25 12:45:51 · 322 阅读 · 0 评论 -
C++多态的原理
一.先看问题class Parent{public: Parent(int a) { this->a = a; } virtual void func(int a) { cout << "Parent::func(int)..." << endl; }private: int a;};class Child :public Parent{public: Child(int a, int b) :Parent(a) { this->原创 2021-12-24 15:16:12 · 320 阅读 · 0 评论 -
C++智能指针(来源、C++自带智能指针、手动定义智能指针)
一.来源int* p = new int; *p = 10; delete p;看上面这段代码,每次定义指针,分配空间后,都需要最后delete,释放空间。因此,C++想怎么可以让指针自动释放。先看普通指针:public: A(int a, int b) { this->a = a; this->b = b; } ~A() { cout << "-------执行析构函数" << endl; } void print() { cou原创 2021-12-21 10:21:39 · 94 阅读 · 0 评论 -
C++ const与常量成员变量、成员函数、引用;this指针与const、static
不加const情况class Test {private: int m_k;public: Test(int k) { this->m_k = k; } int get_m_k() //const { this->m_k = 100;//可以修改m_k的值 return m_k; }};int main(int argc, char** argv) { Test t(1); cout << t.get_m_k() << endl;}原创 2021-12-19 10:30:26 · 182 阅读 · 0 评论 -
C++操作符重载+、-、*、/、[]、()、<<、>>、=、new、delete、&&、||、++、--、+=、强制转换重载
注意:为什么ostream& operator原创 2021-12-18 23:09:11 · 2038 阅读 · 0 评论 -
C++friend(友元函数、友元类)
一.来源下面是一个计算两点之间距离的方法。#include <iostream >#include <math.h>class Point {private: int x; int y;public: Point(int x, int y) { this->x = x; this->y = y; } int getX() { return x; } int getY() { return y; }};//计算两点之间距离原创 2021-12-18 20:54:49 · 709 阅读 · 0 评论 -
C++new、delete和malloc、free内存的申请与释放
一.new和delete介绍在C语言中使用malloc和free来分配和释放内存空间。在C++中提供了更简单且功能强大的运算符new和delete来取代malloc和free函数。new和delete是运算符,不是函数,因此执行效率高。在C++中建议使用new和delete。二.malloc和free使用//申请空间int* a = (int*)malloc(sizeof(int));//申请10个int空间int* b = (int*)malloc(sizeof(int)*10);原创 2021-12-15 16:23:49 · 1309 阅读 · 0 评论 -
C++构造函数的初始化列表、作用
1.问题class A {private: int m_a;public: A(int a) { cout << "A(int a)......." << endl; m_a = a; } void print() { cout <<"m_a=" << m_a << endl; }};class B {private: int m_b; A m_a1; A m_a2;public: B(A& a原创 2021-12-15 14:29:25 · 414 阅读 · 0 评论 -
C++浅拷贝和深拷贝
一.浅拷贝#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>using namespace std;class Teacher {private: int id; char *name;public: //构造函数 Teacher(int id,char* m_name) { cout << "Teacher(int m_id,char* m_name)..."原创 2021-12-15 10:59:25 · 292 阅读 · 0 评论 -
C++strcpy和strcpy_s,解决:C4996This function or variablemay be unsafe;strcpy_s函数不接受两个参数
一.解决:C4996不符合函数“strcpy”规范;This function or variablemay be unsafe;代码:#include <iostream>#include <cstring>using namespace std;int main() { char b[4]; char a[4] = "abc"; strcpy(b, a); cout << b << endl;错误:解决方案:#define _C原创 2021-12-15 09:51:04 · 2722 阅读 · 0 评论 -
C++对象的构造和析构(构造函数和析构函数)
一.构造1.构造的作用看如下代码:class text {private: int x, y;public: void set_xy(int x, int y) { this->x = x; this->y = y; }};int main(int argc, char** argv) { text t1; //如果没有构造函数,在这两句之间t1可能会被其他线程调用,产生不安全现象。 t1.set_xy(1, 2);}因此,构造作用:在对象被创造的时候,用原创 2021-12-14 15:27:08 · 547 阅读 · 0 评论 -
C++类class、结构体struct
1.类为class,对象是类class定义的变量。猫(类),胖橘(对象)。class cat{};cat fat_ju;女朋友(类),傻屌(对象)。class gf{};gf _250;2.结构体为struct。struct cat{};一.struct和class的区别例子1:#include <iostream>using namespace std;//结构体struct hero{ char name[60]; int sex;};原创 2021-12-13 17:44:39 · 1606 阅读 · 0 评论 -
C++指针(定义、使用、(p、p+1、*p+1)关系)、指针与数组的关系
1.指针的定义2.指针的使用如下代码://1.指针的定义int a = 10;int* p;p = &a;//实际上是让p指向a的地址//上面两行等价于int* p=&a;cout << "a的地址(&a):" << (int) & a << endl;cout << "a的地址(p):" << (int)p << endl;//2.指针的使用//可以通过解引用的方式来找到指针指原创 2021-12-12 22:58:46 · 1921 阅读 · 0 评论 -
C++函数的分文件编写
函数的分文件编写作用:让代码结构更加清晰。编写步骤:1.创建后缀为.h的头文件。2.创建后缀名为.cpp的源文件。3.在头文件中写函数的声明。4.在源文件中写函数的定义。例子:文件结构:头文件:放函数声明。源文件:swap.cpp放函数定义。源.cpp放函数调用。运行结果:...原创 2021-12-12 22:26:41 · 945 阅读 · 8 评论 -
c++二维数组、二维指针、数组指针、指针数组
二维数组1.定义://方法一int arr1[2][3];//方法二int arr2[2][3] = { {1,2,3},{4,5,6} };//方法三int arr3[2][3] = { 1,2,3,4,5,6 };//方法四int arr4[][3] = { 1,2,3,4,5,6 };2.数组名称作用:查看二维数组所占内存空间大小;获取二维数组首地址。int arr[2][3] = { {1,2,3},{4,5,6} };cout << "二维数组所占内存大小:原创 2021-12-12 20:58:50 · 1983 阅读 · 3 评论 -
C++:数组(数组第一元素地址、数组首地址、数组下一地址)、指针(指针第一元素地址、指针首地址、指针下一地址)
一.一维数组一维数组名称的用途:1.可以统计整个数组在内存中的长度。2.可以获取数组在内存中的首地址。原创 2021-12-12 17:33:45 · 1696 阅读 · 0 评论 -
C++:程序流程结构(switch)
一.switch结构如下:switch (expression) { case e1: break; case e2: break; case e3: break; default: break; }1.expression:必须是一个整型或枚举类型,例如:int型、char型,需要与下面e1、e2、e3匹配。例子:char型char a ;cin >> a;switch(a){ case 'a':cout << "one" <原创 2021-12-12 15:56:54 · 309 阅读 · 0 评论 -
C++数据类型
1.整形数据类型取值范围short(短整型)2字节(-2^15~ 2^15-1)int(整形)4字节(-2^31~ 2^31-1)long(长整型)Windoxs为4字节、Linux为4字节(32位)、8字节(64位)long long(长长整形)8字节(-2^63 ~2^63-1)#include <iostream>using namespace std;int main(int arg, char** args) { //1.短原创 2021-12-11 15:04:05 · 155 阅读 · 1 评论 -
C++函数重载和函数指针
一.函数指针定义、使用方法如下:#include <iostream>using namespace std;//函数指针int func1(int a, int b) { return a * b;}//1.定义一种函数类型typedef int(my_func) (int, int);//2.定义一种指向这种函数类型的指针类型typedef int(*my_func1) (int, int);int main(int arg, char** args) { //1.原创 2021-12-10 14:18:18 · 513 阅读 · 0 评论 -
C++函数的默认参数和占位参数
默认参数例子1:#include <iostream>using namespace std;//默认参数int func1(int a = 10) { return a;}int main(int arg, char** args) { cout << "默认参数:a=" << func1() << endl; cout << "传递参数:a=" << func1(5) << endl;}例子2原创 2021-12-10 11:33:40 · 221 阅读 · 0 评论 -
C++宏函数和内联函数
**宏函数宏函数在预处理阶段进行。例子:**#include <iostream>using namespace std;//宏函数#define Max(a,b) ((a)>(b)?(a):(b))//预处理阶段进行没有语法检测,只有编译器才会语法检测int max(int a, int b){ return a > b ? a : b;}int main(int arg, char** args){ int a = 1, b = 2; cout原创 2021-12-10 11:02:15 · 217 阅读 · 0 评论 -
C++重载、重写、重定义
一.重载重载一定是在同一个作用域下。例子:是重载class A {public: void func() { } void func(int a) { }};不是重载class A {public: void func() { }};void func(int a) {}原因:不在同一个作用域A中。二.重定义发生在两个不同的类中,且一个是父类,一个是子类。class A {public: void func() { cout << "我是爸原创 2021-12-24 10:45:29 · 117 阅读 · 0 评论 -
C++虚析构函数、虚函数、虚继承
一.来源重点关注下面函数中内容!void Test()void func(A* ap)例子:class A {public: A() { cout << "A()-----" << endl; this->p = new char[64]; memset(this->p, 0, sizeof(this->p)); strcpy_s(this->p,strlen("A string-----")+1 ,"A string-----原创 2021-12-24 10:15:59 · 289 阅读 · 0 评论