笔记 C++ c++案例练习 小楼夜听雨 2024-05-07 2025-01-03 流程结构练习 选择结构 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 () { 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 () { int a=rand () % 100 + 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 () { 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 ); 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 (int a = 0 ; a < 10 ; a++) { cout << a << 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 ) { 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 () { 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]; } } 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 }; int a = 0 ; int b = sizeof (arr) / sizeof (arr[0 ]) - 1 ; 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]; } 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 () { 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]; } 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]; 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 ; 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 () { 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 ; }
英雄项目-按年龄排序
设计一个英雄的结构体,包括姓名,年龄,性别,创建结构体数组,数组中存放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 #include <iostream> #include <string> using namespace std;struct hero { string name; int age; string gender; };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 () { 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 ; } } int main () { circle c1; c1.setc (10 , 0 , 10 ); point p1; p1.setc (10 , 11 ); 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) ; void sety (int y1) ; int getx () ; int gety () ; };
圆的头文件:
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) ; void setyx (point yx) ; int getr () ; 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" void point::setx (int x1) { x = x1; } void point::sety (int y1) { y = y1; } int point::getx () { return x; } 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 = r1; } void circle::setyx (point yx) { YX = yx; } int circle::getr () { 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; p1.setx (10 ); p1.sety (10 ); point p2; p2.setx (10 ); p2.sety (9 ); point p3; 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> 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; 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> 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> 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; }; 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 ; cout << "-------------------" << endl; cout << "第2台电脑" << endl; dn* d2 = new dn (cpu1, xk1, nc1); d2->work (); delete d2; d2 = NULL ; 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 ; }