C++
-1.helloWorld
#include <iostream>
using namespace std;
int main(){
cout<<"hello world!"<<endl;
system("pause");
return EXIT_SUCCESS;
}
- 双冒号作用域
#include <iostream>
using namespace std;
void test01(){
int atk = 1000;
std::cout<<"atk="<<atk<<std::endl;
std::cout<<"atk="<<atk<<std::endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int getRectS(int w,int h);
int getRectS(int w, int h) {
return w*h;
};
void test01(){
printf("%d\n",getRectS(10,10));
}
void* test02(){
char *p = (char *) malloc(sizeof(char ));
*p = '2';
printf("p的地址:%p",&p);
printf("%d",*p);
printf("p的地址:%p",p);
printf("\n");
return p;
}
int main(){
test01();
char *p = (char*)test02();
printf("%d",*p);
printf("p的地址:%p",&p);
return 0;
}
- 引用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//���û������ ���� &���� = ԭ��
void test01()
{
int a = 10;
int &b = a;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void test02()
{
int a = 10;
//int &b;
int &b = a;
int c = 100;
b = c;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
//�����齨������
void test03()
{
//1��ֱ�ӽ�������
int arr[10];
int(&pArr)[10] = arr;
for (int i = 0; i < 10; i++)
{
arr[i] = 100 + i;
}
for (int i = 0; i < 10; i++)
{
cout << pArr[i] << endl;
}
//2���ȶ�����������ͣ���ͨ������ ��������
typedef int(ARRAY_TYPE)[10];
//���� &���� = ԭ��
ARRAY_TYPE & pArr2 = arr;
for (int i = 0; i < 10; i++)
{
cout << pArr2[i] << endl;
}
}
int main(){
//test01();
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
- 命名空间
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
namespace KingGlory
{
int sunwukongId = 1;
}
namespace LOL
{
int sunwukongId = 3;
}
void test01()
{
int sunwukongId = 2;
//1、using声明
//using KingGlory::sunwukongId ;
//当using声明与 就近原则同时出现,出错,尽量避免
cout << sunwukongId << endl;
}
void test02()
{
//int sunwukongId = 2;
//2、using编译指令
using namespace KingGlory;
using namespace LOL;
//当using编译指令 与 就近原则同时出现,优先使用就近
//当using编译指令有多个,需要加作用域 区分
cout << KingGlory::sunwukongId << endl;
cout << LOL::sunwukongId << endl;
}
int main(){
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
对C语言的增强
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1、全局变量检测增强 C++检测出重定义
int a;
//int a = 10;
//2、函数检测增强 返回值检测、形参类型检测、函数调用参数个数
int getRectS(int w,int h)
{
return w *h;
}
void test01()
{
printf("%d\n", getRectS(10, 10));
}
//3、类型转换检测增强
void test02()
{
char * p = (char *)malloc(64);
}
//4、struct增强 C++可以放函数,创建结构体变量,可以简化关键字 struct
struct Person
{
int age;
void func()
{
age++;
}
};
void test03()
{
Person p;
p.age = 17;
p.func();
cout << "p的age = " << p.age << endl;
}
//5、bool类型扩展 C语言下 没有这个类型 C++有bool类型
bool flag = true; // bool类型 代表 真和假 true ---- 真(1) false ---- 假(0)
void test04()
{
cout << sizeof(bool) << endl; //结果是1个字节
//flag = false;
//flag = 100; //将非0的数都转为1
cout << flag << endl;
}
//6、三目运算符增强
void test05()
{
//?:
int a = 10;
int b = 20;
printf("ret = %d\n", a > b ? a : b);
(a < b ? a : b )= 100; // C++下返回的是变量 b = 100
printf("a = %d\n", a);
printf("b = %d\n", b);
}
//7、const增强
//全局const 和C语言结论一致
const int m_A = 100;
void test06()
{
//m_A = 200;
//int * p = (int *)&m_A;
//*p = 200;
//局部const
const int m_B = 100;
//m_B = 200;
int * p = (int *)&m_B;
*p = 200;
cout << "m_B = " << m_B << endl;
int arr[m_B]; //C++下const修饰的变量 称为常量 ,可以初始化数组
}
int main(){
test01();
test03();
test04();
test05();
test06();
system("pause");
return EXIT_SUCCESS;
}
引用其他变量
test.cpp
extern const int g_a = 1000;
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
extern const int g_a;
printf("g_a = %d\n", g_a);
system("pause");
return EXIT_SUCCESS;
}
引用的基本使用
#include <iostream>
using namespace std;
void test01(){
int a=10;
int &b = a;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void test02()
{
int a = 10;
//int &b; //引用必须要初始化
int &b = a;
//引用一旦初始化后,就不可以引向其他变量
int c = 100;
b = c; // 赋值
printf("%p\n",&b);
printf("%p\n",&a);
printf("%p\n",&c);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
//对数组建立引用
void test03()
{
//1、直接建立引用
int arr[10];
int(&pArr)[10] = arr;
for (int i = 0; i < 10; i++)
{
arr[i] = 100 + i;
}
for (int i = 0; i < 10; i++)
{
cout << pArr[i] << endl;
}
//2、先定义出数组类型,再通过类型 定义引用
typedef int(ARRAY_TYPE)[10];
//类型 &别名 = 原名
ARRAY_TYPE & pArr2 = arr;
for (int i = 0; i < 10; i++)
{
cout << pArr2[i] << endl;
}
}
int main(){
test01();
test02();
return 0;
}
引用注意事项
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1、值传递
void mySwap01(int a , int b)
{
int temp = a;
a = b;
b = temp;
/*cout << ":::a = " << a << endl;
cout << ":::b = " << b << endl;*/
}
//2、地址传递
void mySwap02(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//3、引用传递
void mySwap03(int &a , int &b) // int &a = a; int &b = b;
{
int temp = a;
a = b;
b = temp;
}
void test01()
{
int a = 10;
int b = 20;
//mySwap01(a, b);
//mySwap02(&a, &b);
mySwap03(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int& func()
{
int a = 10;
return a;
}
//引用注意事项
void test02()
{
//1、引用必须引一块合法内存空间
//int &a = 10;
//2、不要返回局部变量的引用
int &ref = func();
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
}
int& func2()
{
static int a = 10;
return a;
}
void test03()
{
int &ref = func2();
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
//当函数返回值是引用,那么函数的调用可以作为左值
func2() = 1000;
cout << "ref = " << ref << endl;
}
int main(){
//test01();
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
指针引用
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct Person {
int age;
};
void allocateSpace(Person **p) {
//p指向指针的指针 *p 指针 指向的是person 本体 **p person本体
printf("%p\n",p);
printf("%p\n",*p);
// printf("%p\n",*p);
*p = (Person *) malloc(sizeof(Person));
(*p)->age = 10;
}
void test01() {
Person *p = NULL;
allocateSpace(&p);
cout<<"p.age = "<<p->age<<endl;
};
void allocateSpace2(Person* &pp){
pp = (Person*) malloc(sizeof (Person));
pp->age = 20;
};
void test02(){
Person *p = NULL;
allocateSpace2(p);
cout<<"p.age="<<p->age<<endl;
};
int main(){
test01();
test02();
return EXIT_SUCCESS;
}
常量引用
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
void test01() {
const int &ref = 10; // 加了const之后, 相当于写成 int temp = 10; const int &ref = temp;
printf("%p\n",&ref);
int *p = (int *)&ref;
printf("%p\n",p);
*p = 10000;
cout<<"ref="<<ref<<endl;
};
void showValue(const int &a){
//常量引用的使用场景 修饰函数中的形参,防止误操作
// a = 100000000;
cout<<"a"<<a<<endl;
};
void test02(){
int a=100;
showValue(a);
}
int main() {
test01();
test02();
return EXIT_SUCCESS;
}