![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
Jokic_Rn
北航
展开
-
Linux环境下C/C++代码编写
gcc编译C程序,g++编译C++C程序编写hello world程序1.新建hello.cpp文件2.vim编写代码,i进入编写,编写结束后esc,键入:wq保存退出3.g++编译代码没有输出报错则编译成功,ls命令可以看到一个a.out文件,这个文件为可执行文件4.运行可执行文件结果编写C++工程文件安装CMake1.新建CMakeLists.txt文件2.编写CMakeList文件3.cmake编译生成了一些中间文件可以用cat查看文件内容,比如 cat Makefile4原创 2022-06-01 11:02:43 · 3213 阅读 · 4 评论 -
C++异常
异常处理对于产生的异常,通过try-catch来处理,让程序继续运行,…表示接受所有类型的异常for (int i = 0; i <= 100000; i++){ try { int *p = new int[100000]; } catch (...) { cout << "内存不够,异常" << endl; break; }}主动抛异常像内存不够,系统会自动抛异常,然后用try-catch就可以处理,但有时候系统不会抛异常,比如被除数原创 2021-07-19 11:55:28 · 46 阅读 · 0 评论 -
C++11新特性
自动识别类型//自动识别类型auto a = 5.6;//decltype取类型decltype(a) b = 5;专用空指针nullptrauto p = new int();//空指针p = nullptr;cout << p<< endl;快速遍历//快速遍历int array[] = { 11,22,33,44,55,66 };for (int item : array){ cout << item << .原创 2021-07-19 10:01:08 · 67 阅读 · 0 评论 -
C++类型转换
const_cast去除constPerson *p1 = new Person();Person *p2 = const_cast<Person *>(p1);dynamic_cast多态类型的转换,有安全检测(子类指针指向父类对象这种操作就不安全,会把指针指向为NULL)原创 2021-07-16 17:14:32 · 53 阅读 · 0 评论 -
C++模板
泛型C++中用模板来实现泛型(将变量类型作为函数参数,以实现代码的复用)痛点:写一个add函数实现加法功能,要实现整型,浮点型,Point型(自定义)加法,就需要重写3次addint add(int a,int b);double add(double a, double b);Point add(Point p1, Point p2);采用模板则可以优化代码>>template <typename T>T add(T a, T b){ return a +原创 2021-07-16 11:47:00 · 98 阅读 · 0 评论 -
C++运算符重载
运算符重载可为运算符加入新的功能这里重载了“+”运算Point operator+(Point p1, Point p2){ return Point(p1.m_x + p2.getX(), p1.getY() + p2.getY());}#include <iostream>using namespace std;class Point{ friend Point operator+(Point p1, Point p2);private: int m_x;原创 2021-07-15 15:10:31 · 69 阅读 · 0 评论 -
C语言基础(上)
C语言程序设计简介:适用于快速复习1.常量、变量定义、输入输出#include <stdio.h>int main(){ //常量定义 const int Amount = 100; //变量定义 int a = 0; float b = 0; double c = 0; printf("请输入一个整数和两个小数:"); scanf("%d %f %lf",&a,&b,&c); printf("%d + %f + %lf = %l原创 2021-02-08 11:57:58 · 328 阅读 · 0 评论 -
C语言基础(下)
1.结构类型#include <stdio.h>struct date { int year; int month; int day;};int main() { struct date today = {2021, 03, 17}; struct date tomorrow = today; tomorrow.day = 18; printf("Today is %i-%i-%i\n", today.year, today.month, today.day); p原创 2021-03-17 17:43:51 · 192 阅读 · 0 评论 -
C语言题目及解析
1.输入一个整数(1~6),考虑这个整数的后三位,将其排列组合为三位数,按从小到大顺序输出,首位相同的三位数输出为一行,每个数以空格分隔,行末不能有空格。输入:6输出:678 679 687 689 697 698768 769 786 789 796 798867 869 876 879 896 897967 968 976 978 986 987解答:#include <stdio.h>int main() { int cn原创 2021-02-16 11:48:39 · 769 阅读 · 0 评论 -
C++友元函数/友元类、内部类、局部类
友元友元可以使得函数调用类的私有成员friend Point add(Point p1, Point p2);class Point{ friend Point add(Point p1, Point p2);private: int m_x; int m_y;public: int getX() { return m_x; } int getY() { return m_y; } Point(int x, int y) :m_x(x), m_y(y) {原创 2021-07-15 14:17:38 · 143 阅读 · 0 评论 -
C++拷贝
拷贝构造函数1.当用一个已存在的对象创建一个新对象时会自动调用拷贝构造函数2.car1和car2都是常规创建对象的方法,car3是拷贝了car23.如果不写拷贝构造函数,car3对象仍然会被创建,但是不会调用构造函数#include <iostream>using namespace std;class Car{private: int m_price; int m_height;public: Car(int a = 0,int b = 0):m_price(a),m原创 2021-07-14 15:15:10 · 262 阅读 · 0 评论 -
C++多继承
多继承1.一个子类可以继承多个父类,会增加程序的复杂度,不建议这么做2.如果子类继承的多个父类都有虚函数,子类对象则会创建多张虚表3.子类和父类有同名的函数调用Undergraduate:StudentUndergraduate ug;ug.eat();//调用子类Undergraduate的eatug.Undergraduate::eat();//调用子类Undergraduate的eatug.Student::eat();//调用父类Student的eat...原创 2021-07-08 09:17:46 · 914 阅读 · 0 评论 -
C++多态
父类指针、子类指针父类指针指向子类对象是合法的Animal *animal = new Dog();子类指针指向父类对象是危险的,因为dog指针可能访问不该访问的内存Dog *dog = (Dog *)new Animal();(×)原创 2021-07-06 14:50:21 · 82 阅读 · 0 评论 -
C++继承
成员访问权限struct默认成员权限是public,class默认成员权限是privatepublic:类外部、当前类、子类都可访问protected:当前类和子类可以访问private:仅当前类可以访问,子类不可访问继承1.在内存中,父类中继承过来的属性会排布在前面,子类自定义的属性在后面。2.继承方式有三种:公有继承、保护继承、私有继承class Student1:public Person{};class Student2:protected Person{};clas原创 2021-07-06 10:05:34 · 102 阅读 · 0 评论 -
C++堆空间指向栈空间的问题
命名空间命名空间用于解决命名冲突的问题,访问需要的类时加作用域即可,例如HE::Person person;person.m_age;namespace HE{ class Person{ int m_age; }}class Person{ int m_height}原创 2021-07-05 17:32:19 · 123 阅读 · 0 评论 -
C++构造函数/析构函数、初始化列表
构造函数构造函数会在对象创建时候自动调用,与类同名,一般用于完成类的初始化工作#include <iostream>using namespace std;class Person{public: Person() { m_age = 5; cout << "构造函数 go" << endl <<"age = "<<m_age<<endl; }private: int m_age;};in原创 2021-07-05 14:12:48 · 375 阅读 · 1 评论 -
C++内存
关于堆和栈堆是自己通过malloc申请的,用完要free,而栈在使用时是调用函数或定义变量时自动分配的,用完会自动回收堆空间申请int *p = (int *)malloc(4);自动分配栈空间int a = 10;什么时候需要用到栈空间,什么时候需要用堆空间呢?例:有一个对象叫僵尸,当其血量减为0时,僵尸对象的内存应该被释放。由于执行完函数,栈空间会自动回收内存,不满足我们对“僵尸”对象生命周期控制的需求,而堆空间则可根据需求来释放内存。...原创 2021-07-05 12:02:23 · 85 阅读 · 0 评论 -
C++对象和类
类定义类的构成:成员函数(方法),成员变量(属性)C++的类可以用class或者struct来定义,struct默认成员权限是public,class默认成员权限是private下面是一个简单案例,介绍如何定义一个类,创建一个对象,创建一个指向对象的指针。#include <iostream>using namespace std;struct Person{ int m_age; void run() { cout << "person run" &l原创 2021-07-05 10:39:58 · 60 阅读 · 0 评论 -
C++引用
C++引用引用相当于是一个别名,对ref的操作会对a的值进行操作int a =5;int &ref = a;ref = 6;知识点:1.引用在定义时就要初始化,而且不能改变(区别于指针)原创 2021-07-02 11:52:47 · 84 阅读 · 0 评论 -
C++常量const
常量constconst修饰的变量是不可修改的,修饰的类、结构体(的指针),其成员也不可修改从这里可以看到,修改a提示错误了错误示范,test2变量指针不可指向常量结构,test3常量结构的属性不可更改const修饰的是右边的内容const int *p 是值不能修改int * const p 是地址不能改...原创 2021-07-02 11:22:06 · 43 阅读 · 0 评论 -
C++静态成员
内敛函数(inline function)#include <iostream>inline double sum(double a,double b){ return a+b;}int main(){ cout<<sum(0.5,4)<<endl; /* cout<<sum(0.5,4)<<endl;将被替换成cout<<0.5+4<<endl; */ getchar(); return 0原创 2021-07-02 10:09:00 · 64 阅读 · 0 评论 -
C++基础
C++基础输入输出#include <iostream>int main(){ int a; cin >> a; cout << a << endl; getchar(); return 0;}函数重载#include <iostream>using namespace std;double sum(int a, int b){ return a + b;}double sum(double a,原创 2021-07-01 15:49:13 · 112 阅读 · 0 评论