输入输出函数

hello world

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. cout << "Hello World! 你好" << endl;
  5. int num = 0; cin >> num;
  6. cout << "num = " << num << endl;
  7. return 0;
  8. }

作用域运算符 ::

  1. #include <iostream>
  2. using namespace std;
  3. int a = 10;
  4. int main(){
  5. cout << "Hello World! 你好" << endl;
  6. int num = 0; cin >> num;
  7. cout << "num = " << num << ::a << endl;
  8. return 0;
  9. }

命名空间

  1. namespace A {
  2. int a = 100;
  3. void call(){
  4. printf("ssss");
  5. }
  6. void eat();
  7. }
  8. void A::eat()
  9. {
  10. printf("ddssssdddss");
  11. }

命名空间别名

  1. #include <iostream>
  2. using namespace std;
  3. namespace veryLongName {
  4. int a = 10;
  5. }
  6. int main(){
  7. namespace b = veryLongName;
  8. cout << "veryLongName = " << b::a <<endl;
  9. return 0;
  10. }

使用命名空间

  1. #include <iostream>
  2. using namespace std;
  3. namespace veryLongName {
  4. int a = 10;
  5. }
  6. int main(){
  7. using namespace veryLongName;
  8. cout << "veryLongName = " << a <<endl;
  9. return 0;
  10. }

只使用命名空间中的某一成员

  1. #include <iostream>
  2. using namespace std;
  3. namespace veryLongName {
  4. int a = 10;
  5. }
  6. int main(){
  7. using veryLongName::a;
  8. cout << "veryLongName = " << a <<endl;
  9. return 0;
  10. }

C++里面的结构体

  1. struct 结构体名 { 结构体成员列表 };
  2. using namespace std;
  3. //全局变量声明区
  4. //函数声明区
  5. //结构体声明
  6. struct student {
  7. //成员列表
  8. string name;
  9. //姓名
  10. int age;
  11. //年龄
  12. int scoro;
  13. //分数
  14. };

#define 宏,作用于他所在位置的下面代码部分

  1. #include <iostream>
  2. using namespace std;
  3. void my_func(void){ const int num = 0; #define MY_NUM 10}
  4. int main(){
  5. cout << "veryLongName = " << MY_NUM << endl;
  6. return 0;
  7. }

命名空间里面无法使用宏

Ifndef 用于检测宏是否定义

  1. #ifndef _HEAD_H_ //如果没有定义_HEAD_H_
  2. #define _HEAD_H_ //定义_HEAD_H_

C++ 中的引用

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int num = 10;
  5. int &a = num;
  6. a = 100;
  7. cout<< "a = " << num << endl;
  8. return 0;
  9. }

数组里面的引用方式一

  1. #include <iostream>
  2. using namespace std;
  3. void test02(){
  4. int arr[5] = {10,20,30,40,50};
  5. // 给arr起一个别名-
  6. int (&my_arr)[5] = arr;
  7. int i = 0;
  8. for(i = 0; i<5;i++){
  9. cout << my_arr[i] << " ";
  10. }
  11. cout<<endl;
  12. }
  13. int main(){
  14. test02(); return 0;
  15. }

数组里面的引用方式二

  1. #include <iostream>
  2. using namespace std;
  3. void test03(){
  4. int arr[5] = {10,20,30,40,50};
  5. // 使用typedef定义数组类型
  6. typedef int TYPE_ARR[5];
  7. // myArr是数组别名
  8. TYPE_ARR &myArr = arr;
  9. int i = 0;
  10. for(i = 0; i<5;i++){
  11. cout << myArr[i] << " ";
  12. }
  13. cout<<endl;
  14. }
  15. int main(){
  16. test03();
  17. return 0;
  18. }

C++ 中引用作为函数参数传递

  1. #include <iostream>
  2. using namespace std;
  3. void sum(int *a ,int *b){
  4. int temp = *a; *a = *b; *b = temp;
  5. }
  6. int main(){
  7. int num1 = 10; int num2 = 20;
  8. cout << "num1 == " << num1 << endl;
  9. cout << "num2 == " << num2 << endl;
  10. sum(&num1,&num2);
  11. cout << "after num1 == " << num1 << endl;
  12. cout << "after num2 == " << num2 << endl;
  13. return 0;
  14. }


C++中函数的返回值是引用

  1. #include <iostream>
  2. using namespace std;
  3. int temp;
  4. int fun1()
  5. {
  6. temp = 10;
  7. return temp;
  8. }
  9. int& fun2()
  10. {
  11. temp = 10;
  12. return temp;
  13. }
  14. int main()
  15. {
  16. int a = 0, b = 0;
  17. // 1. 返回函数的普通类型
  18. a = fun1();
  19. // 2. 返回函数的引用
  20. b = fun2();
  21. // 3. 返回函数的引用去初始化一个新的引用
  22. // 函数返回值为左值时,返回值必须是引用
  23. int &c = fun2() = 2000;
  24. cout << "a = " << a << endl;
  25. cout << "b = " << b << endl;
  26. cout << "c = " << c << endl;
  27. return 0;
  28. }
  29. // 编译结果:a = 10 b = 10 c = 10

C++中指针的引用

  1. #include <iostream>
  2. using namespace std;
  3. int temp;
  4. int& fun()
  5. {
  6. temp = 10;
  7. return temp;
  8. }
  9. int main()
  10. {
  11. // 函数返回值为左值时,返回值必须是引用
  12. int &c = fun() = 2000;
  13. // 二级指针
  14. int &d = *&c;
  15. d = 120;
  16. cout << "c = " << c << endl;
  17. return 0;
  18. }
  19. // 编译结果: c = 120

C++中常引用

  1. #include <iostream>
  2. using namespace std;
  3. typedef struct {
  4. int num;
  5. char name[32];
  6. }STU;
  7. // 直接传入,可以修改
  8. void myPrintSTU1(STU temp){
  9. cout<< sizeof(temp) << endl;
  10. cout << "name = " << temp.name << "age = " << temp.num << endl;
  11. }
  12. // 直接传入, 防止值进行修改
  13. void myPrintSTU2(const STU &temp){
  14. cout<< sizeof(temp) << endl;
  15. cout << "name = " << temp.name << "age = " << temp.num << endl;
  16. }
  17. void test(){
  18. STU lucy = {20,"liuyun 中国"};
  19. myPrintSTU2(lucy);
  20. }
  21. int main()
  22. {
  23. test();
  24. return 0;
  25. }

C++内联函数

  1. #include <iostream>
  2. using namespace std;
  3. inline int Max(int x, int y)
  4. {
  5. return (x > y)? x : y;
  6. }
  7. // 程序的主函数
  8. int main( )
  9. {
  10. cout << "Max (20,10): " << Max(20,10) << endl;
  11. cout << "Max (0,200): " << Max(0,200) << endl;
  12. cout << "Max (100,1010): " << Max(100,1010) << endl;
  13. return 0;
  14. }

C++中函数的默认参数

  1. #include <iostream>
  2. using namespace std;
  3. inline int Max(int x = 100, int y = 23)
  4. {
  5. return (x > y)? x : y;
  6. }
  7. // 程序的主函数
  8. int main( )
  9. {
  10. cout << "Max (20,10): " << Max(20,10) << endl;
  11. cout << "Max (0,200): " << Max(0) << endl;
  12. cout << "Max (100,1010): " << Max(100,1010) << endl;
  13. return 0;
  14. }

C++中的函数声明和函数定义

fun.cpp

extern int func(int a,int b =12, int c = 20);

// 这里函数声明,函数定义不能同时设置默认参数

main.cpp

#include <iostream>
#include "fun.cpp"
using namespace std;


int func(int a,int b, int c){

    cout << "a: " << a  << " b: " << b << " c: " << c << endl;
}

inline int Max(int x = 100, int y = 23)
{
   return (x > y)? x : y;
}

// 程序的主函数
int main( )
{

    func(10);
   return 0;
}

C++的函数占位参数

#include <iostream>
#include "fun.cpp"
using namespace std;


void funs(int a ,int){
    cout << "func = call" << endl;
}

// 程序的主函数
int main( )
{

   // 只有传入此参数才会调用此函数
   funs(10,11);
   return 0;
}

C++的函数的重载

#include <iostream>
#include "fun.cpp"
using namespace std;


void funs(int a ,int){
    cout << "func = call" << endl;
}

void funs(int a ,int b,int c){
    cout << "func = call2" << endl;
}


// 程序的主函数
int main( )
{
   funs(10,11,12);
   return 0;
}

C++中的类,构造函数

#include <iostream>

using namespace std;


class Line{

    public:
        void setHeight(double len);
        double getHeight();
        Line(double len);   // 这是构造函数声明
        ~Line();  // 这是析构函数声明

private:
    double height;

};

void Line::setHeight(double len){
    height = len;
}

double Line::getHeight(void){
    return height;
}

Line::Line(double len){
    cout << "Object is being created, length = " << len << endl;
    height = len;
}

Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}


// 程序的主函数
int main()
{
   Line line(10);

   // 获取默认设置的长度
   cout << "Length of line : " << line.getHeight() <<endl;
   // 再次设置长度
   line.setHeight(6.0);
   cout << "Length of line : " << line.getHeight() <<endl;

   return 0;
}

C++中new对象
#include <iostream>  
using namespace std;  

class TestNew  
{  
private:  
    int ID;  
public:  
    TestNew(int ID);  
    ~TestNew();  
};  

TestNew::TestNew(int ID)  
{  
    this->ID = ID;  
}  

TestNew::~TestNew()  
{  
    std::cout<<"对象 "<<this->ID<<" 执行析构函数"<<std::endl;  
}  
void Test()  
{  
    TestNew test(1);//创建对象1,不使用new  
    TestNew *pTest = new TestNew(1);//创建对象2,使用new  
}  

int main()  
{  
    Test();//这个地方有点问题,pTest没有进行处理,会导致内存泄露,实际应用中要注意呀  

}  


new创建类对象需要指针接收,一处初始化,多处使用
new创建类对象使用完需delete销毁
new创建对象直接使用堆空间,而局部不用new定义类对象则使用栈空间
new对象指针用途广泛,比如作为函数返回值、函数参数等
频繁调用场合并不适合new,就像new申请和释放内存一样