C++基本语法
c++基础教程
c++基础
2
1
using namespase A; //名称空间
2
#include <iostream> //#include引用包含的意思 , iostream标准库,输入输出的意思
八进制and十六进制
3
1
0开头 0-8是八进制
2
ox开头 表示十六进制
3
0-9表示十进制
常量
1
1
#define 常量名 内容;
输入输出
13
1
输出使用cout 输入使用cin
2
3
cout <<"Hello Word!";
4
5
char a; //变量
6
7
cin >>a; //接受输入
8
9
printf(控制格式,数值列表); 输出
10
printf("123 \n");
11
printf("%s","你是不是猪!\n"); //输出字符串
12
printf("%f\n",1.00); //输出小数点
13
printf("%d\n",10); //输出整数
格式字符
9
1
%d 以十进制形式输出带符号整数
2
%o以八进制形式输出无符号整数
3
%x 以十六进制形式输出无符号整数
4
%u以十进制输出无符号整数
5
%c输出单个字符
6
%s输出字符串
7
%f以小数形式输出单精度,双精度实数
8
%e 以指数形式输出单精度,双精度实数
9
%g以%f%e中较短的输出宽度输出单精度。双精度实数
流操作的控制具体函数
6
1
#include<iomanip> //引入类库
2
3
4
5
6
运算符
6
1
cout << (8^12) <<endl; //等于4
2
cout << (~123) <<endl; //等于-124
3
cout << (14|12) << endl; //等于14
4
cout << (14&12) << endl; //等于12
5
6
判断字符占用几个字节
5
1
Sizeof();
2
3
char a = '和';
4
cout << sizeof(a) << endl;
5
cout << sizeof("和") << endl;
重命名数据类型
6
1
typedef int T;
2
现在 int 可以用T来表示
3
4
T A = 10;
5
6
科学计数法
2
1
flot a = 1.2345e2; // = 123.45;
2
flot a = 1.2345e-2; // = 00.12345;
entern变量,允许其他文件调用
1
1
extern int a ; //
字符串复制
1
1
strcpy("被复制变量","复制字符串");
指针
107
1
int A = 10; //普通变量
2
int * a; //声明指针变量
3
4
a = &A;//赋值指针使用&符号
5
a = (int *) 10;//强行转换赋值
6
7
cout << "输出指针地址" << a <<endl;
8
cout << "输出指针里面的内容" << *a <<endl;
9
10
*a = 11; //改变指针里面的值
11
12
cout << "指针数组---------------------------------------------------------------"<<endl;
13
14
int a[10] = {1,2,3,4,5,6,7,8,9,10};
15
int *P = a;//数组不需要加&符号
16
//第一种方式
17
for(int i = 0; i < 10 ; *p++,i++)
18
{
19
cout << *P << endl;
20
}
21
//第二中方式
22
for(int i = 0;i<10;i++)
23
{
24
cout << *p++ <<endl;
25
}
26
27
cout << "二维数组----------------------------------------------------------------------------------" <<endl;
28
29
/// <summary>
30
/// 二维数组
31
/// </summary>
32
void P5()
33
{
34
int a[4][3] = {1,2,4,5,6,7,8,9,10.11,12};
35
int* p = a[0];
36
37
//sizeof(a) = 48 除 sizeof(int) = 4
38
for(int i = 0;i<sizeof(a)/sizeof(int);i++)
39
{
40
cout << "address: ";
41
cout << a[i];
42
cout << " is ";
43
cout << *p++ << endl;
44
45
}
46
47
48
}
49
/// <summary>
50
/// 指针字符串-连接字符串
51
/// </summary>
52
void P6()
53
{
54
char a[100], b[100];
55
char* p1 = a;
56
char* p2 = b;
57
gets_s(a);
58
gets_s(b);
59
while (*p1 != '\0')
60
{
61
p1++;
62
}
63
while(*p2 !='\0')
64
{
65
*p1++ = *p2++; //最简单的方式
66
/* *p1 = *p2;
67
p1++;
68
p2++;*/
69
}
70
*p1 = '\0';
71
puts(a);
72
}
73
void P7()
74
{
75
char a[100] = "hello word!";
76
char* p = 0;
77
int i;
78
cout << "指针访问,指针输出" << endl;
79
for(p = a;*p!='\0';p++)
80
{
81
cout << *p;
82
}
83
cout << endl;
84
cout << "数组访问,数组输出" << endl;
85
for(i=0;a[i]!='\0';i++)
86
{
87
cout << a[i];
88
}
89
cout << endl;
90
cout <<"数组访问,指针输出"<< endl;
91
for (i=0;*(a+i);i++)
92
{
93
cout << *(a + i);
94
}
95
cout << endl;
96
cout << "指针和下标访问,指针表示" << endl;
97
for(i=0,p=a;*(p+i);i++)
98
{
99
cout << *(p+i);
100
}
101
cout << endl;
102
cout << "指针和下标访问,下标表示" << endl;
103
for(i=0,p=a;p[i];i++)
104
{
105
cout << p[i];
106
}
107
}
指针数组
49
1
void array1(char * [],int);
2
void array2();
3
int main()
4
{
5
//一维指针数组使用
6
//char* a[5] = { (char*)"a",(char *)"b" ,(char*)"c",(char*)"d"};
7
//int n = 4;
8
//array1(a, n);
9
10
//二维数组使用
11
array2();
12
}
13
/// <summary>
14
/// 一维指针数组
15
/// </summary>
16
/// <param name="a">指针数组</param>
17
/// <param name="b">计数变量</param>
18
void array1(char * a[] , int b)
19
{
20
char *p = a[0];
21
int i = 0;
22
while (i < b)
23
{
24
p = (char *)*(a + i);
25
i++;
26
cout << p << endl;
27
}
28
}
29
30
/// <summary>
31
/// 二维数组
32
/// </summary>
33
void array2()
34
{
35
int *p[5];
36
int a = 1,b=2,c=3,e=4,f=5;
37
p[0] = &a;
38
p[1] = &b;
39
p[2] = &c;
40
p[3] = &e;
41
p[4] = &f;
42
int** q = p; //申明而二维数组
43
44
cout << *(*q++) << endl;
45
cout << *(*q++) << endl;
46
cout << *(*q++) << endl;
47
cout << *(*q++) << endl;
48
cout << *(*q) << endl;
49
}
指针和函数
54
1
char* str(char *name);
2
int call(int, int);
3
int main()
4
{
5
int a = 3;
6
int b = 6;
7
int input;
8
int (*pfun)(int,int);//申明指针函数
9
pfun = call;//给指针函数赋值
10
input = (*pfun)(a, b); //运用指针函数
11
cout << input << endl;
12
13
return 0;
14
}
15
/// <summary>
16
/// 函数指针
17
/// </summary>
18
/// <param name="name">user</param>
19
/// <returns></returns>
20
char *str(char *name)
21
{
22
char* str1 = (char*)"尊敬的会员:";
23
char* str2 = (char*)",欢迎登录;";
24
char* newstr = (char*)malloc(100); //malloc申请空间
25
char* r = newstr;
26
while (*str1)
27
*r++ = *str1++;
28
while (*name)
29
*r++ = *name++;
30
while (*str2)
31
*r++ = *str2++;
32
*r= '\0';
33
return newstr;
34
35
//使用方式
36
//char name[100] = { '\0' };
37
38
//gets_s(name);
39
//char* s = str(name);
40
//cout << s << endl;
41
//free(s); //释放malloc申请的空间
42
//s = 0;//赋值为空
43
}
44
45
/// <summary>
46
/// 指针函数
47
/// </summary>
48
/// <param name="a"></param>
49
/// <param name="b"></param>
50
/// <returns></returns>
51
int call(int a, int b)
52
{
53
return (a * b) / a;
54
}
引用 (&符号)
59
1
2
using namespace std;
3
void yy();
4
void ys(int & a , int & b);
5
int& fh(int a , int b);
6
int J;
7
int main()
8
{
9
fh(4,5);
10
11
cout << J << endl;
12
fh(4, 5)=100; //引用函数可以赋值:fh(4,5) = 100;
13
cout << J << endl;
14
}
15
16
/// <summary>
17
/// &引用变量
18
/// </summary>
19
void yy()
20
{
21
int a = 10;
22
int & Aa = a; //引用变量a
23
cout << Aa << endl;
24
a = 20; //a改变Aa也跟着改变
25
cout << a << endl;
26
Aa = 30; //Aa改变a也跟着改变
27
cout << Aa << endl;
28
29
}
30
31
/// <summary>
32
/// 更换外部变量内容
33
/// </summary>
34
/// <param name="a">a= 5 </param>
35
/// <param name="b">b= 6 </param>
36
void ys(int & a , int & b)
37
{
38
//使用方法
39
//int a = 5;
40
//int b = 6;
41
//cout << "更换前a= " << a << " 更换前b= " << b << endl;
42
//ys(a, b);
43
//cout << "更换后a= " << a << " 更换后b= " << b << endl;
44
int tmp = a;
45
a = b;
46
b = tmp;
47
48
}
49
50
/// <summary>
51
/// 函数返回值是引用类型,提前声明全局变量
52
/// </summary>
53
/// <param name="a"></param>
54
/// <param name="b"></param>
55
/// <returns></returns>
56
int & fh( int a , int b)
57
{
58
return J = (a + b) * 5;
59
}
结构体 (struct)
51
1
2
using namespace std;
3
4
struct struct_a //结构体 struct
5
{
6
int Aa;
7
int Ab;
8
int Ac;
9
}A,*B; //可以在后面直接添加变量(可以添加多个)
10
11
struct struct_b //结构体嵌套
12
{
13
int a;
14
int b;
15
struct_a c;
16
};
17
18
int main()
19
{
20
21
cout << "结构体-----------------------------" << endl;
22
A.Aa = 43;
23
A.Ab = 63;
24
A.Ac = 97;
25
26
cout << A.Aa << endl;
27
cout << A.Ab << endl;
28
cout << A.Ac << endl;
29
30
cout << "指针结构体---------------------" << endl;
31
B = &A; //数组结构体:使用->访问
32
cout << B->Aa << endl;
33
cout << B->Ab << endl;
34
cout << B->Ac << endl;
35
cout << (*B).Aa << endl; //还可以这样
36
37
cout << "结构体嵌套-----------------------" << endl;
38
struct_b Sb = { 1,2,{1,2,3} }; //使用方括号初始化
39
cout << Sb.a << endl;
40
cout << Sb.b << endl;
41
cout << (Sb.c).Aa << endl; //使用括号
42
cout << (Sb.c).Ab << endl;
43
cout << (Sb.c).Ac << endl;
44
cout << Sb.c.Ac << endl; //不要括号也可以
45
46
cout << "结构体大小-----------------------" << endl;
47
cout << sizeof(Sb) << endl;
48
49
50
51
}
结构体和函数
59
1
struct Show//声明结构体
2
{
3
int age;
4
int height;
5
int weight;
6
};
7
8
void Message(struct Show S);
9
void Message2(struct Show * S);
10
void Message3(struct Show & S);
11
12
int main()
13
{
14
//使用结构体函数
15
Show S = { 18,170,110 };
16
Message(S);
17
18
//使用结构体指针
19
Show S2 = { 18,170,110 };
20
Message2(&S2);
21
22
//使用引用,
23
Show S4 = { 18,170,110 };
24
Message3(S4);
25
cout << "测试引用在外部是否被改变" << endl;
26
cout << S4.age << endl;
27
cout << S4.height << endl;
28
cout << S4.weight << endl;
29
30
}
31
32
33
void Message(struct Show S)
34
{
35
cout << "函数结构体----------变量" << endl;
36
cout << S.age << endl;
37
cout << S.height << endl;
38
cout << S.weight << endl;
39
40
}
41
42
void Message2(struct Show * S)
43
{
44
cout << "函数结构体----------指针" << endl;
45
cout << S->age << endl;
46
cout << S->height << endl;
47
cout << S->weight << endl;
48
49
}
50
51
void Message3(struct Show& S)
52
{
53
cout << "函数结构体----------引用" << endl;
54
cout << S.age << endl;
55
cout << S.height << endl;
56
cout << S.weight << endl;
57
S = { 0,0,0};
58
59
}
结构体和数组
27
1
struct Show
2
{
3
int age;
4
int weight;
5
int height;
6
};
7
8
int main()
9
{
10
Show S[5] = { {1,2,3},{11,2,3},{111,2,3},{1111,2,3},{1111,2,3} };
11
//cout << S[0].age << endl;
12
//cout << S[1].age << endl;
13
//cout << S[2].age << endl;
14
//cout << S[3].age << endl;
15
//cout << S[4].age << endl;
16
17
Show* S2;
18
S2 = S;
19
20
for (int i = 0; i<5 ; i++,S2++)
21
{
22
cout << S2->age << endl;
23
cout << S2->height << endl;
24
cout << S2->weight << endl;
25
}
26
}
27
共用体
23
1
union(共用体),跟结构体相似,但是同时只能存在一个值
2
3
union Show //声明共用体
4
{
5
int age;
6
int witght;
7
int height;
8
};
9
10
11
int main()
12
{
13
cout << "共用体同时只能存在一个值,字节占用最大那个" << endl;
14
15
Show S;
16
S.age= 1;
17
S.height = 2;
18
S.witght = 3;//共用体仅存在最后一次赋的值,每次只能存在一个值
19
cout << S.age << endl; // = 3
20
cout << S.witght << endl;// = 3
21
cout << S.height << endl;// = 3
22
23
}
枚举
28
1
enum Show
2
{
3
age,
4
weight,
5
height
6
};
7
8
9
int main()
10
{
11
Show S =(enum Show)0;
12
13
cout << S << endl;
14
if (S == (Show)weight)
15
{
16
cout << "枚举值是:weight" << endl;
17
}
18
else if(S == (Show)age)
19
{
20
cout << "枚举值是:age" << endl;
21
}
22
else if (S == (Show)height)
23
{
24
cout << "枚举值是:height" << endl;
25
}
26
27
}
28
class类:创建头文件
14
1
//创建一个头文件cs.h
2
class Cs
3
{
4
public: //公开类型
5
int get_age();
6
int set_age(int age ,int weight ,int height);
7
8
private: //私有类型
9
int age;
10
int weight;
11
int height;
12
protected: //保护类型
13
14
};
class类:类文件
21
1
#include "Cs.h"
2
3
using namespace std;
4
5
int main()
6
{
7
Cs C;//使用Cs声明一个变量
8
C.set_age(10);//使用C.set_age();调用函数
9
int a = C.get_age();
10
cout << a << endl;
11
}
12
13
void Cs::set_age(int age )//初始化Cs类里面的set_age();函数
14
{
15
Cs::age = age;
16
}
17
18
int Cs::get_age()//初始化Cs类里面的get_age();函数
19
{
20
return age;
21
}
class和指针
21
1
#include "Cs.h"
2
3
using namespace std;
4
5
int main()
6
{
7
Cs C;//声明普通类
8
Cs *P = &C;//声明指针类 , &符号取C的地址
9
C.set_age(10);//使用普通类
10
cout << P->get_age() << endl;//指针调用函数使用->
11
}
12
13
void Cs::set_age(int age )
14
{
15
Cs::age = age;
16
}
17
18
int Cs::get_age()
19
{
20
return age;
21
}
构造函数和析构函数and变量初始化
31
1
class Cs
2
{
3
public:
4
Cs();//构造函数
5
~Cs();//析构函数
6
int add();//
7
8
public:
9
int age;
10
int weight;
11
int height;
12
};
13
14
std::cout<<"函数定义--------------------------------------------------------------------"<<endl;
15
16
Cs::Cs():age(18),weight(110),height(170)//函数后面跟着:加变量()即可给变量赋值
17
{
18
std::cout << "构造函数初始化" << std::endl;
19
}
20
21
int Cs::add()
22
{
23
return age + weight + height;
24
}
25
26
Cs::~Cs()//析构函数
27
{
28
age = 0;
29
weight = 0;
30
height = 0;
31
}
本文来自博客园,作者:Entity110,转载请注明原文链接:https://www.cnblogs.com/rdr2/p/15232091.html

浙公网安备 33010602011771号