标准命名空间
命名空间的使用
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
namespace zh { int m_id = 10; void Display(){ cout << "Display zh func" << endl; } }
namespace cn { int m_id = 20;
namespace cpp { int m_id = 30; } }
namespace{ int m_ptr = 10; }
int main(int argc, char* argv[]) { using namespace zh; cout << zh::m_id << endl; cout << cn::m_id << endl; cout << cn::cpp::m_id << endl; zh::Display();
system("pause"); return 0; }
|
C++ 引用知识
引用的实质就是取别名,&写到左侧叫引用,写到右侧叫取地址。
普通的引用
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
int a = 10; int &b = a;
cout << b << endl;
system("pause"); return 0; }
|
数组引用
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
int ary[10]; for (int x = 0; x < 10; x++) ary[x] = x;
int(&parr)[10] = ary;
for (int y = 0; y < 10; y++) { cout << parr[y] << " " ; }
typedef int(ARYS)[10];
ARYS &pary2 = ary; for (int y = 0; y < 10; y++) { cout << pary2[y] << " "; }
system("pause"); return 0; }
|
参数传递方式 参数传递两种方式,传递数值,传递地址,还可以传递引用
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
void mySwap(int *a, int *b) { int tmp = *a;
*a = *b; *b = tmp; cout << *a << *b << endl; }
void myswap2(int &a, int &b) { int tmp = a; a = b; b = tmp; }
int main(int argc, char* argv[]) { int x = 10; int y = 20;
myswap2(x,y);
cout << x << endl;
system("pause"); return 0; }
|
引用传递注意事项 int a = 10;
改成static 即可实现不销毁。
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
int& dowork() { int a = 10; return a; }
int main(int argc, char* argv[]) { int &ret = dowork();
cout << ret << endl; cout << ret << endl; cout << ret << endl; cout << ret << endl; cout << ret << endl;
dowork() = 1000;
system("pause"); return 0; }
|
引用的本质: 引用所占用的内存地址与指针类似,引用本质就是指针常量
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
using namespace std;
void test(int& ref) { ref = 1000; }
int main(int argc, char* argv[]) {
int a = 10; int& aref = a;
aref = 20;
cout << a << endl;
cout << aref << endl;
system("pause"); return 0; }
|
引用使用场景:指针的引用
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdlib.h>
using namespace std;
struct person { int m_age;
};
void allocat(person **p) { *p = (person *) malloc(sizeof(person)); (*p)->m_age = 100; }
void test() { struct person *p = NULL; allocat(&p); cout << "p的:" << p->m_age << endl; }
void allocatbyref(person* &p) { p = (person *)malloc(sizeof(person)); p->m_age = 1000; } void test2() { person *p = NULL; allocatbyref(p); cout << (*p).m_age << endl; }
int main(int argc, char* argv[]) {
test2(); system("pause"); return 0; }
|
常量引用:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdlib.h>
using namespace std;
void test() { const int &ref = 10;
int *p = (int*)&ref; *p = 1000; cout << ref << endl; }
int main(int argc, char* argv[]) { test(); system("pause"); return 0; }
|
常量引用使用场景: 用来修饰型can
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdlib.h>
using namespace std;
void test(const int &val) {
cout << val << endl; }
int main(int argc, char* argv[]) { int a = 10;
test(a); system("pause"); return 0; }
|
函数与类的一些知识点
内联函数,为了替换c中的宏,内联函数本身也是函数,只是加了一个关键字inline()
编译器会如何除了? 直接变成了宏,直接内嵌到调用的位置,并不增加新的函数。
在类中定义的,成员函数,默认会自动加上内联函数关键字声明,这是编译器为我们加上的。
但是,如果函数过大,或使用了循环等结构,则类则不会声明为内联函数。
#include <iostream> #include <string>
using namespace std;
inline void compare(int x, int y) { int ret = x < y ? x : y; cout << ret << endl; }
int main(int argc, char *argv[]) { compare(10, 20); system("pause"); return 0; }
|
函数的默认参数
#include <iostream> #include <string>
using namespace std;
void func(int x = 10, int y = 20) { cout <<x+y << endl; }
int main(int argc, char *argv[]) { func(1, 20); system("pause"); return 0; }
|
函数重载: 名称相同,但是符号不同,后面的参数不同,就是重载。
当使用重载时,编译器会偷偷在前面加上 _func 关键字,这样来使用重载,通过编译检查。
using namespace std;
void func(int x) { cout <<x << endl; }
void func(double x) { cout << x << endl; }
int main(int argc, char *argv[]) { func(1); func(1.1); system("pause"); return 0; }
|
extern c 的作用: 在c++ 中想要使用C代码,需要加上extern 关键字。
#include <iostream> #include <string>
using namespace std;
extern "C" void show() { printf("aaaaa \n"); }
int main(int argc, char *argv[]) { show(); system("pause"); return 0; }
|
类的基本定义
#include <iostream> #include <string>
using namespace std;
class Student { public: int uid; char *name; int age;
public: void set(int id,char *name,int age) { this->uid = id; this->name = name; this->age = age; } void display() { cout << this->uid << this->name << this->age << endl; } };
int main(int argc, char *argv[]) { Student stu;
stu.set(1001, "lyshark", 23); cout << stu.uid << stu.name << stu.age << endl; stu.display();
Student *ptr = &stu; cout << ptr->uid << ptr->name << ptr->age << endl;
system("pause"); return 0; }
|
C语言实现的类: 这个是不是看起来很麻烦了,我们继续改进。
#include <iostream> #include <string>
using namespace std;
struct Student { char name[64]; int age; };
void PrintPerson(struct Student *ptr) { printf("Name: %s --> Age: %d \n", ptr->name, ptr->age); }
int main(int argc, char *argv[]) { struct Student stu;
strcpy(stu.name, "lyshark"); stu.age = 23; PrintPerson(&stu);
system("pause"); return 0; }
|