C++

输入输出

1   cout <<  "请输入" << endl;
2   int x = 0;  (初始化变量)
3   cin >> x;
4   cout << x << endl;

命名空间

1 namespace A
2   {
3   int x = 2;
4   void f1();
5   }
6   cout << A::x << endl;
7   B::f1();

使用命名空间 

 1 using namespace A 

if语句的使用

1 if(flag)
2 {
3 cout << "true" << endl;
4 }
5 else
6 {
7 cout << "false" << endl;
8 }

引用以及指针

 1 #include<iostream>
 2 using namespace std;
 3 void fun(int &a, int &b);
 4 int main(void)
 5 {
 6     int x = 10, y = 20;
 7     fun(x, y);
 8     cout << x << " " << y << endl;
 9 }
10 void fun(int &a, int &b)
11 {
12     int c = 0;
13     c = a;
14     a = b;
15     b = c;
16 }
17 #引用格式int &y = x;
 1 #include<iostream>
 2 using namespace std;
 3 void fun(int *a, int *b);
 4 int main(void)
 5 {
 6     int x = 10, y = 20;
 7     fun(&x,&y);
 8     cout << x << " " << y << endl;
 9 }
10 void fun(int *a, int *b)
11 {
12     int c = 0;
13     c = *a;
14     *a = *b;
15     *b = c;
16 }
17 #指针

const修饰的即为常量,不可改变

函数有默认参数值的参数需要写到最右边,声明函数时写默认值,定义函数时不需要写默认值

函数重载:在相同作用域内,用同一函数名定义的多个函数,参数个数与参数类型不同

内联函数:关键字为inline,要求为逻辑简单的函数,递归函数无法使用内联方式

使用new申请内存,使用delete释放内存

申请和释放块内存 int *arr = new int[10]; delete []arr; 

类的实例化以及使用

 1 #include<iostream>
 2 using namespace std;
 3 class Coordinate
 4 {
 5 public:
 6     int x;
 7     int y;
 8     void printx(){
 9         cout << x << endl;
10     }
11     void printy() {
12         cout << y << endl;
13     }
14 };
15 int main(void)
16 {
17     Coordinate coor;#栈实例化
18     coor.x = 10;
19     coor.y = 20;
20     coor.printx();
21     coor.printy();
22 
23     Coordinate* p = new Coordinate();#堆实例化
24     if (NULL == p)
25     {
26         //failed
27         return 0;
28     }
29     p->x = 100;
30     p->y = 200;
31     p->printx();
32     p->printy();
33     delete p;
34     p = NULL;
35 
36     system("pause");
37     return 0;
38 }

 初始化string对象的方式

1 #include<string>
2 string s1;
3 string s2("abc");
4 string s3(s2);
5 string s4(n,'c');

封装

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 /**
 6   * 定义类:Student
 7   * 数据成员:m_strName
 8   * 数据成员的封装函数:setName()、getName()
 9   */
10 class Student
11 {
12 public:
13     // 定义数据成员封装函数setName()
14     void setName(string str)
15     {
16         m_strName = str;
17     }
18     // 定义数据成员封装函数getName()
19     string getName()
20     {
21         return m_strName;
22     }
23     
24     
25 //定义Student类私有数据成员m_strName
26 private:
27     string m_strName;
28 
29 };
30 
31 int main()
32 {
33     // 使用new关键字,实例化对象
34     Student *str = new Student();
35     // 设置对象的数据成员
36     str->setName("dry");
37     // 使用cout打印对象str的数据成员
38     cout << str->getName() << endl;
39     // 将对象str的内存释放,并将其置空
40     delete str;
41     str = NULL;
42     return 0;
43 }

类外定义 void Car::stop(){}; 

分文件定义需要加 #include"Car.h"

构造函数与类同名,没有返回值,可重载

Student(){m_strName = lucy;}#无参构造函数

Student(string name){m_strName = name;}#有参构造函数 

初始化列表 Student():m_strName("lucy"),m_intAge(10){} 

初始化列表先于构造函数执行,初始化列表只能用于构造函数

拷贝构造函数 Student(const Student& stu) stu为变量名

析构函数 ~Student(){cout << "Student" << endl;} 

四种变量类型,auto(自动),static(静态),register(寄存器),extern(外部)

要多次调用时用static,要多次修改时用register,extern即为全局变量

原文链接: https://www.cnblogs.com/hqhdry/p/12545965.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/336817

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午10:52
下一篇 2023年3月1日 下午10:52

相关推荐