c++案例练习

流程结构练习

选择结构

if语句-3只小猪比重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int main2() {
//1.请用户输入3只小猪的体重
//2.将体重进行对比
//3.输出最重的小猪
int a, b, c;
cout << "请输入第一只小猪的体重" << endl;
cin >> a;
cout << "请输入第二只小猪的体重" << endl;
cin >> b;
cout << "请输入第三只小猪的体重" << endl;
cin >> c;

if ((a > b) && (a > c)) { cout << "第一只小猪最重,为" << a << "斤" << endl; }
else if ((b>a)&&(b>c)) { cout << "第一只小猪最重,为" << b << "斤" << endl; }
else if ((c > a) && (c > b)) { cout << "第一只小猪最重,为" << c << "斤" << endl; }

//也可以一层一层嵌套,用冒泡排序,但是麻烦

system("pause");
return 0;
}

循环结构

while语句-猜数字游戏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
int main6()
{
//随机生成一个0-100数字,提示猜大小

//生成随机数字用 rand()%区间

int a=rand() % 100 + 1; //生成 0+ 1~99 +1 的随机数字
//玩家输入数字
cout << "请输入一个数字" << endl;
int b;
cin >> b;

//判断大小
while (b < a) {
cout << "小了" << endl;
cin >> b;
}
while (b>a){
cout << "大了" << endl;
cin >> b;

}


if (a = b) { cout << "答对了" <<endl; }

// 老师教程的
int c = rand() % 100 + 1;
cout << "请输入一个数字" << endl;
int d;

while (1)
{
cin >> d;
if (c < d) { cout << "大了"<<endl; }
else if (c > d) { cout << "小了" << endl; }
else {
cout << "答对了" << endl;
break; //结束循环
}

}




//为了防止随机数字一样,可以用
srand((unsigned int)time(NULL));//根据时间随机
cout << "请输入一个数字" << endl;
int e;
int g = 0;
int h = 4;
int f = rand() % 100 + 1;
while (g<5)
{
cin >> e;
if (f < e) { cout << "大了,你还有" <<h-g<<"次机会" << endl; g++; }
else if (f > e) { cout << "小了,你还有" << h - g << "次机会" << endl; g++; }
else {
cout << "答对了" << endl;
break; //结束循环
}

}

system("pauce");
return 0;
}

do while语句-求出所有水仙花数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
using namespace std;
int main8()
{ //水仙花数是指一个3位数,它每个位数上数字的3次幂之和等于它本身
//如:1^3+5^3+3^3=153
//请用do...while语句求出所有水仙花数


//1.找到所有三位数
// 2.找到所有水仙花数
// 1.取个位数 对数字%10
// 2.取十位数 对数字/10 %10 在四位数在取3位数,数字/100%10
// 3.取百位数 对数字/100
//

int sz=100;
int a = 0;
int b = 0;
int c = 0;

do {

a = sz % 10;
b = sz / 10 % 10;
c = sz / 100;

if ((a * a * a) + (b * b * b) + (c * c * c) == sz)//如果是水仙花数才打印
{
cout << sz << endl;
}
sz++;
}
while (sz < 1000);


//找到100-999的所有数字
//找到水仙花数
//aaa+bbb+ccc=xz

int A = 0;
int B = 0;
int C = 0;
int SZ = 100;

do {
A = SZ % 10;//个位数
B = SZ / 10 % 10;//十位数
C = SZ / 100;//百位数
if ((A * A * A) + (B * B * B) + (C * C * C) == SZ)
{
cout << SZ << endl;
}
SZ++;





} while (SZ < 1000);


system("pauce");
return 0;
}

for语句-拍桌子案例

拍桌子
从1数到100,如果数字中有7,或者是7的倍数,就输出拍桌子,其他的就输出数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;
int main9()
{
//for(起始表达式1;条件表达式2;末尾循环体3) {循环语句4}
//先1,然后判断2,执行4,然后3,再判断2-4-3
//输出1-9

for (int a = 0; a < 10; a++) { cout << a << endl;}


//拍桌子
//从1数到100,如果数字中有7,或者是7的倍数,就输出拍桌子,其他的就输出数字
//先找到1-100,输出
//再找拍桌子的数字
//最后输出


for (int b = 1; b < 101; b++) {
int a1 = 0;
int a2 = 0;
a1 = b % 10;
a2 = b / 10 % 10;
if (a1 == 7 || a2 == 7) { cout << "拍桌子" << endl; }
else if(b%7==0) { cout << "拍桌子" << endl; }
else cout << b << endl; }


//优化
for (int b = 1; b < 101; b++) {
int a1 = 0;
int a2 = 0;
a1 = b % 10;
a2 = b / 10 % 10;
if (a1 == 7 || a2 == 7|| b % 7 == 0) { cout << "拍桌子" << endl; }
else cout << b << endl;
}

system("pauce");
return 0;
}

嵌套循环-99乘法表

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main11()
{
//b是竖,a是横
for (int b = 1; b <= 9; b++) { for (int a = 1; a <= b; a++) { cout << a << "*" << b << "=" << a * b << " "; } cout << endl; }

//倒过来
for (int c = 9; c >= 1; c--) { for (int d = 1; d <= c; d++) { cout << c << "*" << d << "=" << c * d << " "; } cout << endl; }
system("pauce");
return 0;
}

数组案例

一维数组

5只数组小猪称体重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int main2()
{
//找出数组中最重的小猪
int arr[5] = { 300,330,450,400,345 };
int a = 0;

for (int b = 0; b < 5; b++) { if (a < arr[b]) { a = arr[b]; } }// 用for循环将数组中的每个数和a比较,如果大的话就代替,最后输出a
cout << a << endl;

system("pause");
return 0;
}

数组元素逆置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
using namespace std;
int main4()
{
int arr[5] = { 1,2,3,4,5 };
//先创造一个临时空间,把第一个数字放进去,再把最后一个数字代替第一个数字,然后把临时空间的数字代替最后一个数字
// 用for循环定义第一个数字的下标++,最后数字的下标--,条件为++<--
//1.创造临时空间
int a = 0; //临时空间
int b = sizeof(arr) / sizeof(arr[0]) - 1; //最后一个数的下标

//2.把第一个数放进去
for (int i = 0; b > i; i++,b--) {
a=arr[i];
arr[i] = arr[b];
arr[b]=a;
}

for (int c = 0; c < 5; c++) { cout << arr[c]; }


//也可以用while语句
int i = 0;
while (b < i) {
a = arr[i];
arr[i] = arr[b];
arr[b] = a;
i++;
b--;
}

for (int c = 0; c < 5; c++) { cout << arr[c]; }

system("pause");
return 0;
}

二维数组

考试成绩统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
#include<string>
int main()
{
//3位同学的成绩如下表
// 语文 数学 英语
//张三 100 100 100
//李四 90 50 100
//王五 60 70 80
// 求上面3位同学的总成绩
int arr[3][3] = {
{100,100,100},
{90,100,100},
{60,70,80}
};
string arr1[] = { "张三","李四","王五" };
int c=0;
int a = 0;
for (; a < 3; a++) {
for (int b = 0; b < 3; b++) { c += arr[a][b]; }//c = c + arr[a][b],,,,+=就是累加
cout <<arr1[a] << "同学的成绩为" << c << endl;
c = 0;

}

system("pause");
return 0;
}

结构体案例

毕设项目-1名老师带5名学生

学校正在做毕设项目,每名老师带领5个学生,需求如下
设计老师和学生的结构体,在老师的结构体中有老师的姓名和一个存放5名学生的数组作为成员
学生的成员有姓名,分数
通过函数给每个老师及所带的学生赋值
最终打印出老师和学生的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<string>
#include <ctime>//根据时间变化的随机数种子的头文件
using namespace std;

struct student { string sname; int score; };
struct teacher { string tname; struct student stu[5]; };

string nameSeed = "ABCDE";//这个可以让字符串最后加一个字母,让每个字符串都不一样

//给老师和学生赋值的函数
void ABC(teacher tarr1 [], int len1)
{
for(int a=0;a<len1;a++)
{
tarr1[a].tname = "teachername";
tarr1[a].tname += nameSeed[a];//加第a个字母


for (int b=0;b<5;b++)
{
student sarr[5];
tarr1[a].stu[b].sname = "studentname";
tarr1[a].stu[b].sname += nameSeed[b];

//这里可以定义一个随机数,让分数随机
int sj = rand() % 60 + 40;//0`59 加40后40~99
tarr1[a].stu[b].score = sj;
}

}

}

//输出函数
void sc(struct teacher tarr2[], int len2)
{
for (int a = 0; a < len2; a++)
{
cout << "老师名字:" << tarr2[a].tname << endl;
for (int b = 0; b < 5; b++)
{
cout <<"\t学生名字:" << tarr2[a].stu[b].sname << " 成绩:" << tarr2[a].stu[b].score << endl;
}
}

}

int main8()
{
//学校正在做毕设项目,每名老师带领5个学生,需求如下
//设计老师和学生的结构体,在老师的结构体中有老师的姓名和一个存放5名学生的数组作为成员
// 学生的成员有姓名,分数
// 通过函数给每个老师及所带的学生赋值
// 最终打印出老师和学生的数据

srand((unsigned int)time(NULL));//随机数种子

teacher tarr[3]{};
int len = sizeof(tarr) / sizeof(tarr[0]);
ABC(tarr, len);
sc(tarr, len);



system("pause");
return 0;
}

英雄项目-按年龄排序

  1. 设计一个英雄的结构体,包括姓名,年龄,性别,创建结构体数组,数组中存放5名英雄
  2. 通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最后打印排序后的结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<string>
using namespace std;

struct hero { string name; int age; string gender; };
//设计一个英雄的结构体,包括姓名,年龄,性别,创建结构体数组,数组中存放5名英雄
//通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最后打印排序后的结果
void maopao (hero herosz[], int len)
{
hero tamp;
for (int a = 0; a < len - 1; a++)
{
for (int b = 0; b < len - a - 1; b++)
{
if(herosz[b].age>herosz[b+1].age)
{
tamp = herosz[b];
herosz[b] = herosz[b + 1];
herosz[b + 1]= tamp;
}
}
}
}
void schu(hero herosz[], int len)
{
for(int a=0;a<5;a++)
{
cout << "姓名:" << herosz[a].name << "\t年龄" << herosz[a].age << "\t性别" << herosz[a].gender << endl;
}
}

int main()
{
hero herosz[5]{ {"李白",30,"男"},{"小乔",17,"女"} ,{"大乔",20,"女"} ,{"刘备",25,"男"} ,{"曹操",35,"男"} };
int len = sizeof(herosz) / sizeof(herosz[0]);

maopao(herosz, len);
schu(herosz, len);
system("pause");
return 0;
}

类和对象案例

封装

设计学生类

设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;
#include <string>

//设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
class student {
public:
string name;
int id;
void daying()
{
//cin >> name;这样赋值不了
//cin >> id;
cout << "姓名"<<name << endl;
cout << "学号"<<id << endl;
}
//可以写一个给姓名赋值的函数
void setname(string name1)
{
name = name1;
}
void setid(int id1)
{
id = id1;
}
};

int main1()
{
//第一种赋值方式(直接访问)
student stu1;
stu1.name = "小王";
stu1.id = 123456789;
stu1.daying();

//第二种赋值方式(通过函数访问)
student stu2;
stu2.setname("张三");
stu2.setid(1234567);
stu2.daying();
system("pause");
return 0;
}

设计立方体类

设计立方体
求出体积和面积
分别用全局函数和成员函数判断两个立方体是否相等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;
#include <string>

class cube {
private:
double x;
double y;
double z;
public:
//输入边长的函数
void seth(double a, double b, double c)
{
x = a;
y = b;
z = c;
}

//输出边长的函数
double getx()
{
return x;
}

double gety()
{
return y;
}

double getz()
{
return z;
}

//面积函数
double mj()
{
return(x * y) * 2 + (x * z) * 2 + (y * z) * 2;
}

//体积函数
double tj()
{
return x * y * z;
}

//成员函数判断是否相同
bool issame(cube& c1)//传一个和本身判断
{
if (c1.getx() == getx() && c1.gety() == gety() && c1.getz() == getz())
{
return true;
}
else return false;
}
};

//全局函数判断是否相等
bool issame(cube& c1, cube& c2)
{
if (c1.getx() == c2.getx() && c1.gety() == c2.gety() && c1.getz() == c2.getz())
{
return true;
}
else return false;
}
int main8()
{
//设计立方体
//求出体积和面积
//分别用全局函数和成员函数判断两个立方体是否相等

cube c1;
c1.seth(1, 2, 3);
cout << "面积:" << c1.mj() << endl;
cout << "体积:" << c1.tj() << endl;

cube c2;
c2.seth(1, 2, 1);
cout << "面积:" << c2.mj() << endl;
cout << "体积:" << c2.tj() << endl;

//全局函数的
bool ret = issame(c1, c2);
if (ret)
{
cout << "这两个立方体相同" << endl;
}
else cout << "这两个立方体不相同" << endl;



//类函数的
bool ret1 = c1.issame(c2);
if (ret1)
{
cout << "这两个立方体相同" << endl;
}
else cout << "这两个立方体不相同" << endl;
system("pause");
return 0;
}

点和圆的关系

设计一个圆形类(circle),和一个点类(point),计算点和圆之间

我的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
using namespace std;
#include <string>
#include<math.h>

//圆类
class circle
{
private:
double c_x;
double c_y;
double c_r;

public:
//输入函数
void setc(double x, double y, double r)
{
c_x = x; c_y = y; c_r = r;
}

//输出坐标函数
double getc_x(){return c_x;}
double getc_y(){return c_y;}
double getc_r(){return c_r;}
};

//点类
class point
{
private:
double p_x;
double p_y;

public:
//输入函数
void setc(double x, double y)
{
p_x = x, p_y = y;
}

//输出坐标函数
double getp_x() { return p_x; }
double getp_y() { return p_y; }
};

//判断函数
void gx(circle c1, point p1)
{
int a;
int b = p1.getp_x() - c1.getc_x();
int c = p1.getp_y() - c1.getc_y();
int d = c1.getc_r();
if ((b * b + c * c) > (d * d))
{
a = 1;
}
else if ((b*b + c*c) == (d*d))
{
a = 2;
}
else a = 3;

switch (a)
{
case 1:cout << "点在圆外" << endl; break;
case 2:cout << "点在圆上" << endl; break;
case 3:cout << "点在圆内" << endl; break;

}

}

/*也可以将圆心的坐标归为点类(在一个类中,可以让另一个类当成员)

class circle
{
private:
point 圆心
double c_r;
};

使用的时候为圆类.圆心.x/y
*/

int main()
{
// 设计一个圆形类(circle),和一个点类(point),计算点和圆之间的关系
// 设计一个坐标轴
// 输入圆心和半径的坐标,点的坐标
// 判断点到圆心的距离和半径的关系

circle c1;
c1.setc(10, 0, 10);//x,y,r

point p1;
p1.setc(10, 11);//x,y
gx(c1,p1);

system("pause");
return 0;
}

优化的:

1.将类的声明放在头文件

2.将类的实现放在单独的源文件

3.在一个类中,可以让另一个类当成员

点的头文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once//防止头文件重复包含
#include<iostream>
using namespace std;

class point
{
private:
int x;
int y;

public:
void setx(int x1);//设置x
void sety(int y1);//设置y
int getx();//输出x
int gety();//输出y

};

圆的头文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once//防止头文件重复包含
#include<iostream>
using namespace std;
#include"point.h"


class circle
{
private:
int r;//半径
point YX;//圆心

public:
void setr(int r1);//设置r
void setyx(point yx);//设置圆心
int getr();//输出r
point getyx();//输出圆心
};

点的实现:

下面的函数实现本身为成员函数

但写在下面就成为全局函数
所以需要 类:: 表示作用域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include"point.h"

//下面的函数实现本身为成员函数
//但写在下面就成为全局函数
//所以需要 类:: 表示作用域

//设置x
void point::setx(int x1)//point::表示作用域
{
x = x1;
}

//设置y
void point::sety(int y1)
{
y = y1;
}

//输出x
int point::getx()
{
return x;
}

//输出y
int point::gety()
{
return y;
}

圆的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "circle.h"

void circle ::setr(int r1)//设置r
{
r = r1;
}
void circle::setyx(point yx)//设置圆心
{
YX = yx;
}
int circle::getr()//输出r
{
return r;
}
point circle::getyx()//输出圆心
{
return YX;
}

函数主体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
#include <string>
#include"point.h"
#include"circle.h"
//判断函数
void pd(circle a, point b)
{
int x = a.getyx().getx()-b.getx();
int y = a.getyx().gety()-b.gety();
int r = a.getr();

if (x * x + y * y > r * r) { cout << "点在圆心外" << endl; }
else if (x * x + y * y == r * r) { cout << "点在圆心上" << endl; }
else { cout << "点在圆心内" << endl; }
}
int main()
{
point p1;//点1
p1.setx(10);
p1.sety(10);

point p2;//点2
p2.setx(10);
p2.sety(9);

point p3;//点3
p3.setx(10);
p3.sety(11);

point yx;//圆心
yx.setx(10);
yx.sety(0);

circle c1;//圆
c1.setr(10);
c1.setyx(yx);

pd(c1, p1);
pd(c1, p2);
pd(c1, p3);

system("pause");
return 0;
}

类和对象

多态

计算器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
using namespace std;
#include<string>

//利用多态和正常方式写一个计算器

//多态的好处:
//1.组织结构清晰
//2.可读性强
//3.对于前期和后期的扩展以及维护性高

//正常
class calculator
{
public:
int get(string a)//输入符号
{
if (a == "+")
{
return num1 + num2;
}
else if (a == "-")
{
return num1 - num2;
}
else if (a == "*")
{
return num1 * num2;
}
}

int num1;
int num2;
};
//如果想要扩展新的功能(如除法),需要修改源码
//在真实开放中 提倡 开闭原则
//开闭原则:对扩展进行开发,对修改进行关闭

void test1()
{
calculator c1;
c1.num1 = 10;
c1.num2 = 20;
cout << c1.get("+") << endl;
}



//多态

class jsq //计算器抽象类 做父类
{
public:
virtual int get()
{
return 0;
}
int num1;
int num2;

};

//加类
class add :public jsq
{
int get()
{
return num1 + num2;
}
};

//减类
class sb :public jsq
{
int get()
{
return num1 - num2;
}
};

//乘法类
class mul :public jsq
{
int get()
{
return num1 * num2;
}
};

void test2()//多态的使用条件:父类的指针或者引用指向子类对象
{
//加法运算
jsq* jsq1 = new add;//开辟堆区创建一个加法类的对象
jsq1->num1 = 10;
jsq1->num2 = 10;

cout << jsq1->get() << endl;//计算器类的虚函数指针已经指向了加类的表,并执行表里面的地址的加法虚函数get

//在这里直接可以清空栈区不用if,因为栈区是一定有的
delete jsq1;
jsq1 = NULL;

//减法运算
jsq1 = new sb;//开辟堆区创建一个减法类的对象
jsq1->num1 = 10;
jsq1->num2 = 10;

cout << jsq1->get() << endl;

delete jsq1;
jsq1 = NULL;
}

int main()
{
test1();
test2();

system("pause");
return 0;
}

制作饮品

1.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
using namespace std;
#include<string>

//制作饮品的流程大致为:煮水-冲泡-倒入杯子-加入辅料
//利用多态实现,提供抽象制作基类,提供子类制作咖啡和茶叶

class drink
{
public:
virtual void drink1() = 0;
};

class coffee:public drink
{
public:
virtual void drink1()
{
cout << "煮水" << endl;
cout << "冲泡咖啡" << endl;
cout << "倒入杯子" << endl;
cout << "加入糖和牛奶" << endl;
}
};

class tea :public drink
{
public:
virtual void drink1()
{
cout << "煮水" << endl;
cout << "冲泡茶叶" << endl;
cout << "倒入杯子" << endl;
cout << "加入柠檬" << endl;
}
};

void test(string a)
{
if (a == "咖啡") { drink* a = new coffee; a->drink1(); }
else if (a == "茶水") { drink* a = new tea; a->drink1(); }
else cout << "没有" << endl;

}
int main()
{
test("咖啡");
test("茶水");
test("0");

system("pause");
return 0;
}

2.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
using namespace std;
#include<string>

//制作饮品的流程大致为:煮水-冲泡-倒入杯子-加入辅料
//利用多态实现,提供抽象制作基类,提供子类制作咖啡和茶叶

class drink
{
public:
virtual void zs() = 0;//煮水
virtual void cp() = 0;//冲泡
virtual void dr() = 0;//倒入杯子
virtual void jr() = 0;//加入辅料

void bz()//执行所有步骤
{
zs();
cp();
dr();
jr();
}
};

class coffee :public drink
{
public:
void zs() { cout << "煮水" << endl;};
void cp() { cout << "冲泡咖啡" << endl; };
void dr() { cout << "倒入杯子" << endl; };
void jr() { cout << "加入糖和牛奶" << endl; };

};

class tea :public drink
{
public:
void zs() { cout << "煮水" << endl; };
void cp() { cout << "冲泡茶叶" << endl; };
void dr() { cout << "倒入杯子" << endl; };
void jr() { cout << "加入柠檬" << endl; };

};

//制作函数
void dowork(drink * a)
{
a->bz();
delete a;
}
//选择函数
void test(string a)
{

if (a == "咖啡") { dowork(new coffee); }
else if (a == "茶水") {dowork(new tea) ; }
else cout << "没有" << endl;

}
int main()
{
test("咖啡");
test("茶水");
test("0");

system("pause");
return 0;
}

组装电脑

使用引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
using namespace std;
#include<string>

//电脑组成部件为:CPU(运算函数) 显卡(显示函数) 内存条(内存管理函数)
//将每个封装出抽象基类,并且提供不同厂商生产不同的零件,例如Intel厂商和Lenovo厂商
//创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
//测试组装3台不同的电脑进行工作


//思路
//首先创造三个零件的抽象类
//抽象类中是工作虚函数
//
//再创造厂商类继承
//工作虚函数重写
//
//再写电脑函数
//电脑函数负责传入零件类的厂商引用进行虚函数调用
//
//最后进行组装函数
//实例化各种厂商的零件,通过电脑函数传入进行组装

class cpu
{
public:
virtual void cpuwork()=0;
};

class xk
{
public:
virtual void xkwork() = 0;
};

class nc
{
public:
virtual void ncwork() = 0;
};

class Icpu :public cpu
{
virtual void cpuwork()
{
cout << "i厂的CPU开始工作" << endl;
}
};
class tcpu :public cpu
{
virtual void cpuwork()
{
cout << "t厂的CPU开始工作" << endl;
}
};

class Ixk :public xk
{
virtual void xkwork()
{
cout << "i厂的显卡开始工作" << endl;
}
};
class txk :public xk
{
virtual void xkwork()
{
cout << "t厂的显卡开始工作" << endl;
}
};

class Inc :public nc
{
virtual void ncwork()
{
cout << "i厂的内存条开始工作" << endl;
}
};

class tnc :public nc
{
virtual void ncwork()
{
cout << "t厂的内存条开始工作" << endl;
}
};


class dn
{
public:
dn(cpu&cpu1, xk& xk1, nc& nc1)
{
cpu1.cpuwork();
xk1.xkwork();
nc1.ncwork();
}
};
void zzhs()
{
Icpu c1;
Ixk x1;
Inc n1;
tcpu c2;
txk x2;
tnc n2;

cout << "第1台电脑" << endl;
dn(c1,x1,n1);
cout << "第2台电脑" << endl;
dn(c1, x2, n1);
cout << "第3台电脑" << endl;
dn(c2, x2, n2);

}
int main()
{
zzhs();

system("pause");
return 0;
}

使用指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include<iostream>
using namespace std;
#include<string>

//电脑组成部件为:CPU(运算函数) 显卡(显示函数) 内存条(内存管理函数)
//将每个封装出抽象基类,并且提供不同厂商生产不同的零件,例如Intel厂商和Lenovo厂商
//创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
//测试组装3台不同的电脑进行工作


//思路
//首先创造三个零件的抽象类
//抽象类中是工作虚函数
//
//再创造厂商类继承
//工作虚函数重写
//
//再写电脑函数
//电脑函数负责传入零件类的厂商引用进行虚函数调用
//
//最后进行组装函数
//实例化各种厂商的零件,通过电脑函数传入进行组装

class cpu
{
public:
virtual void cpuwork() = 0;
};

class xk
{
public:
virtual void xkwork() = 0;
};

class nc
{
public:
virtual void ncwork() = 0;
};

class Icpu :public cpu
{
virtual void cpuwork()
{
cout << "i厂的CPU开始工作" << endl;
}
};
class tcpu :public cpu
{
virtual void cpuwork()
{
cout << "t厂的CPU开始工作" << endl;
}
};

class Ixk :public xk
{
virtual void xkwork()
{
cout << "i厂的显卡开始工作" << endl;
}
};
class txk :public xk
{
virtual void xkwork()
{
cout << "t厂的显卡开始工作" << endl;
}
};

class Inc :public nc
{
virtual void ncwork()
{
cout << "i厂的内存条开始工作" << endl;
}
};

class tnc :public nc
{
virtual void ncwork()
{
cout << "t厂的内存条开始工作" << endl;
}
};


class dn
{
public:


dn(cpu* cpu1, xk* xk1, nc* nc1)
{
m_cpu = cpu1;
m_xk = xk1;
m_nc = nc1;
}

void work()
{
m_cpu->cpuwork();
m_xk->xkwork();
m_nc->ncwork();
}

cpu* m_cpu; xk* m_xk; nc* m_nc;

//~dn()//也可以在下面释放,在这里释放之后,再组装电脑就要再重新用指针开辟新的零件,零件无法重复利用
//{
// if (m_cpu != NULL) { delete m_cpu; m_cpu = NULL; }
// if (m_xk != NULL) { delete m_xk; m_xk = NULL; }
// if (m_nc != NULL) { delete m_nc; m_nc = NULL; }
//
//}
};
//void zzhs()//在电脑类内执行析构函数释放零件的堆区,在电脑类的对象(第一天电脑)的堆区被释放之后,零件也会释放,不能使用第二次
//{
// //第一台电脑的零件
// cpu* cpu0 = new (Icpu);
// xk* xk0 = new(Ixk);
// nc* nc0 = new(Inc);
//
// //第一台电脑的运行
// dn *d1=new dn(cpu0, xk0, nc0);
// d1->work();
// delete d1;
// d1 = NULL;
//
// //第2台电脑的零件
// cpu* cpu1 = new (tcpu);
// xk* xk1 = new(txk);
// nc* nc1 = new(tnc);
//
// //第2台电脑的运行
// dn* d2 = new dn(cpu1, xk1, nc1);
// d2->work();
// delete d2;
// d2 = NULL;
//}

void zzhs()
{
//电脑的零件
cpu* cpu0 = new (Icpu);
xk* xk0 = new(Ixk);
nc* nc0 = new(Inc);
cpu* cpu1 = new (tcpu);
xk* xk1 = new(txk);
nc* nc1 = new(tnc);

//第一台电脑的运行
cout << "第1台电脑" << endl;
dn *d1=new dn(cpu0, xk0, nc0);
d1->work();
delete d1;
d1 = NULL;

//第2台电脑的运行
cout << "-------------------" << endl;
cout << "第2台电脑" << endl;
dn* d2 = new dn(cpu1, xk1, nc1);
d2->work();
delete d2;
d2 = NULL;

//第2台电脑的运行
cout << "-------------------" << endl;
cout << "第3台电脑" << endl;
dn* d3 = new dn(cpu1, xk0, nc1);
d3->work();
delete d3;
d3 = NULL;

//也可以在最后统一释放
delete cpu0; cpu0 = NULL;
delete xk0; xk0 = NULL;
delete nc0; nc0 = NULL;
delete cpu1; cpu1 = NULL;
delete xk1; xk1 = NULL;
delete nc1; nc1 = NULL;

}

int main()
{
zzhs();

system("pause");
return 0;
}