#include<iostream>
using namespace std;
#include<string.h>
#include<string>
/*
预处理
宏定义 条件编译 文件包含
#if....#else....#endif
#include
istream cin
ostream cout
C 面向过程 函数 流程图
C++ 面向对象 类的设计 类图
struct
1.不能为空
2.在c中只是一个结构体名,在C++中是类型
3.在C++中,是空,则大小为1
4.内存对其
C++
struct class
1.public
private
sizeof strlen
1.sizeof是运算符 ,修饰类型(例如int是四个字节,当有三个字符,返回类型值4*3,包括/0)
strlen是库函数,修饰字符串,不包括/0,遇此停止访问。
2.所占空间,字符串长度
*/
#if 0
struct Data
{
char a;
char b;
int n;
void print() {}
};
class Data1
{
char a;
char b;
int n;
void print() {}
};
void main()
{
cout << sizeof(Data) << endl;
}
#endif
#if 0
void main()
{
char a[5] = { '1','2','3','4','5' };
char b[6] = { '1','2','3','4','5' };
char c[] = "abc\012abd";
char d[] = "abc\0abc";
cout << sizeof(a) << endl; //5
cout << strlen(a) << endl; // size_t strlen(char*)
cout << sizeof(b) << endl; //6
cout << strlen(b) << endl; //
cout << sizeof(c) << endl; //
cout << strlen(c) << endl;
cout << sizeof(d) << endl; //
cout << strlen(d) << endl;
}
#endif
/*
指针 指针变量
值类型 指针 引用
指针和引用
-----------------?
引用---别名
1.引用在声明时候必须初始化
2.sizeof(引用)就是实体的大小
3.没有空引用
4.引用没有多级
*p--从p所指向的首地址开始,以p的基类型所占的字节数为偏移量
*/
#if 0
void main()
{
//int b = 10;
char a[] = { 1,1,1,1,1 };
int* p = (int*)a;
short* q = (short*)a;
cout << *p << endl;
cout << *q << endl;
}
#endif
#if 0
void main1()
{
int* p = NULL;
//int& q = NULL;
int a = 10;
int& b = a;
b++;
cout << a << endl;
}
#endif
#if 0
void swap1(int a, int b)//
{
int t = a;
a = b;
b = t;
}
void swap2(int* a, int* b)
{
int* t = a;
a = b;
b = t;
}
void swap3(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void swap4(int& a, int& b)
{
int t = a;
a = b;
b = t;
}
void main()
{
int x = 10, y = 20;
swap1(x, y);
cout << x << " " << y << endl;
swap2(&x, &y);
cout << x << " " << y << endl;
swap4(x, y);
cout << x << " " << y << endl;
}
#endif
/*
*
多态
1.重载多态
函数重载 运算符重载
2.参数多态
模板
3.包含多态
virtual 多态
4.强制多态
4种强制多态 例如:const_cast
*/
/*
函数重载
1.函数名相同
2.参数列表不同
3.不能仅通过函数返回值区分
4.const函数可以构成重载
成员函数后面可以加const---称作常成员函数
*/
//int fn(){}
#if 0
void fn()
{
cout << "fn" << endl;
}
void fn(int n)//::fn(int)
{
cout << "fn : n = " << n << endl;
}
void fn(int n, int m) //::fn(int,int)
{
cout << "fn : " << n << " " << m << " " << endl;
}
struct Data
{
void fn()//Data::fn
{
cout << "Data :: fn" << endl;
}
void fn()const
{
cout << "Data ::fn const" << endl;
}
};
void main()
{
fn(4, 6);
::fn(10);
Data t;
const Data tt;
t.fn();
tt.fn();
}
#endif
/*
const
c中const是只读变量
C++中const是常量 提高程序健壮性
1.定义常量
2.作为函数参数 -
char *strcpy(char*,const char*)
int strcmp(const char*,const char*)
3.作为函数的返回值
4.修饰常成员函数
5.修饰指针
int*const p;
const int *q;
int const *m;
const int *const n;
编程:1+2+3+4....+100 之和,不能用循环,不能用判断,不能用公式,不能直接输出结果
函数指针数组
int f0(int n)
{
return 0;
}
int f1(int n)
{
static int(*p[2])(int) = {f0,f1};
return n + p[!!n](n-1);
}
int (*p[2])(int) 函数指针数组 int test(int)
int (*p)[2]; 数组指针
int *p[2]; 指针数组
*/
/*
1 3 2 5 4 3 2 1 3 4 5 6 7 1 3 7 1 2
*/
#if 0
void main()
{
const int n = 10;
//int a[n];
int a[] = {1,1,2,3,4,5,6,9,8,7,6,5};
int n = sizeof(a) / sizeof(a[0]);
int b[11] = { 0 };
for (int i = 0; i < n; ++i)
{
b[a[i]]++;
}
}
//b[a[i]]++; b[!!n]
#endif
/*
命名空间 ---词穷
sort
*/
namespace AA
{
void sort()
{
cout << "AA::sort" << endl;
}
};
namespace BB
{
void sort()
{
cout << "BB::sort" << endl;
}
};
using namespace AA;
void main()
{
sort();
BB::sort();
}