C++基础

1.从hello world开始

#include <iostream>
using namespace std;


int main()
{
    cout << "hello world" << endl;
    system("pause");
    return EXIT_SUCCESS;

}

解释:(1)#include <iostream>就好比C#里面引用程序集一样,引用iostream这个“程序集”;

(2)using namespace std 其实就是和C#中的using一样,使用命名空间std,因为cout在命名空间为std的iostream中,

如果不写using namespace std这行,那么cout << "hello world" << endl要写成这样:std::cout << "hello world" <<std::endl;

(3)main为入口函数;

(4)cout<< “输出”  ,endl  “换行”;

(5)EXIT_SUCCESS 是一个宏;

(6)system("pause") 阻塞,相当于C#中Console.readkey();

2.冒号作用域

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;

//::作用域运算符

int ark = 200;
void test()
{
int ark = 100;
cout << "攻击力为:" << ark << endl;
cout << "全局攻击力为:" << ::ark << endl;
std:cout << "ok" << endl;
}
int main()
{
test();
system("pause");

}

解释:

#define CRT_SECURE_WARINGS 定义一个宏,定义这个宏后在用到C里面的类库及方法时编译不会报错,默认都是加上这个宏的;
 ::ark表示全局下的方法,代码一幕了然,无须废话;

3.命名空间nameSpace
定义2个头文件:
//game1.h

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;


namespace King
{
   void goArk();
}
//game2.h

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;

namespace LOL
{
    void goArk();
}

定义2个实现:

//game1.cpp

#include "game1.h"

void King::goArk()
{
    cout << "王者荣耀输出攻击" << endl;
}
//game2.cpp

#include "game2.h"

void LOL::goArk()
{
    cout << "LOL输出攻击" << endl;
}

主函数调用并输出:

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;
#include "game1.h"
#include "game2.h"

int main()
{
    King::goArk();
    LOL::goArk();
    system("pause");
}

 4.using的声明与编译指令

using的声明:

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;



namespace LoLof
{
    int testId = 20;
}
void  test()
{
    int testId = 10;
    //写了using后,意思是:以后看见testId是命名空间LOLof下的testId了,但是又由于“就近原则”,所以产生了“二义性”编译错误
    using LoLof::testId;
    
    cout << testId << endl;
}



int main()
{
    test();
    system("pause");
}

 

 

 

 using的编译指令:

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;



namespace LoLof
{
    int testId = 20;
}
void  test()
{
    int testId = 10;
    //写了using namespace LoLof后相当于打开了这扇门,但是没有用哦
    using namespace LoLof;
    
    cout << testId << endl;
}



int main()
{
    test();
    system("pause");
}

结果还是:10;

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;



namespace LoLof
{
    int testId = 20;
}
namespace KingGlory
{
    int testId = 20;
}
void  test()
{
    /*int testId = 10;*/
    //写了using namespace LoLof后相当于打开了这扇门,但是没有用哦
    using namespace LoLof;
    //using namespace KingGlory;
    cout << testId << endl;
}



int main()
{
    test();
    system("pause");
}

结果:20;

 
#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;



namespace LoLof
{
    int testId = 20;
}
namespace KingGlory
{
    int testId = 20;
}
void  test()
{
    //int testId = 10;
    //写了using namespace LoLof后相当于打开了这扇门,但是没有用哦
    using namespace LoLof;
    using namespace KingGlory;
    cout << testId << endl;
}
int main()
{
    test();
    system("pause");
}

报错:

 5.const的加强

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;

const int m_A = 10;

int main()
{
    const int m_B = 20;
    int* p = (int*)&m_B;
    *p = 200;
    cout << "*p:" << *p << endl;
    cout << "m_B:" << m_B << endl;
    system("pause");
}

 6.const 在c语言中默认是外部使用的:

C语言中:

源.c文件


const int a = 10;
//main.c文件


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    extern const a;
    printf("a : %d", a);
    system("pause");
    return EXIT_SUCCESS;
}

而C++中:必须加extern 

//源1.cpp文件

 extern const int a = 10;
//源.cpp文件


#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;


int main()
{
    extern const int a;
    cout << a << endl;
    system("pause");
}

7 引用

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;

//::作用域运算符

int ark = 200;

int main()
{
    //引用格式:用&   就是起别名
    int& a = ark;
    cout << "a=" << a << endl;
    system("pause");

}

对数组起别名:

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;

//对数组来做引用
void test()
{
    int arr[10];
    for (int i = 0;i < 10;i++)
    {
        arr[i] = i;
    }
    //给数组起别名
    int(&pArr)[10] = arr;
    for (int i = 0;i < 10;i++)
    {
        cout << pArr[i] << endl;
    }

    //第2种方式 起别名
    typedef int(ARRAYREF)[10];//一个具有10个元素的int类型的数组
    ARRAYREF& pArr2 = arr;
    for (int i = 0;i < 10;i++)
    {
        cout << pArr2[i] << endl;
    }
}

int main()
{
    //引用格式:用&   就是起别名
    test();
    system("pause");

}

 

8 C++ 参数的三种传递

https://blog.csdn.net/qq_41973378/article/details/89970323

 9 引用的本质:是一个指针常量;

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;


void test(int &b)
{
    b = 15;//本质是:*b=15;
}
int main()
{
    int a = 20;
    int& aref = a;//本质 :int* const aref = &a;
    aref = 10;//本质*aref=10

    test(a);
    cout << a << endl;
    cout << aref << endl;
    system("pause");

}

 10.指针的引用

#define CRT_SECURE_WARINGS
#include <iostream>
using namespace std;


struct Person
{
    int m_Age;
};
//C 语言中利用指针开辟内存
void allocMemery(Person** p)  //**p对象本身   *p对象的指针   p对象指针的指针
{
    *p = (Person*)malloc(sizeof(Person));
    (*p)->m_Age = 100;
}

void test()
{
    Person* p = NULL;
    allocMemery(&p);
    cout << "p的年龄:" << p->m_Age << endl;
}

//C++中利用引用开辟内存

void allocMemeryByRef(Person* &p)
{
    p= (Person*)malloc(sizeof(Person));
    p->m_Age = 200;
}
void tets2()
{
    Person* p = NULL;
    allocMemeryByRef(p);
    cout << "p的年龄:" << p->m_Age << endl;
}

int main()
{
    test();
    tets2();
    system("pause");

}

 

posted @ 2020-09-23 09:23  _MrZhu  阅读(105)  评论(0)    收藏  举报