作业3-类与对象(中)
先回顾一下第三次课学了啥:
1.this指针(指向成员函数所作用的对象) 2.静态成员 3.成员对象和封闭类 4.友元 5.常量成员函数
静态成员函数中,不能访问/调用非静态成员变量/函数,因为解释不了是在用哪个对象的。
如果想在对象生成时统计某项数据,消亡时减去,就有必要就要自己写复制构造函数,来避免复制出一个对象没有增加但消亡却减少的情况。
封闭类的初始化:通过封闭类的构造函数的初始化列表。
友元类之间的关系不能继承或传递。
常量对象:在前面加const; 常量函数:在后面加const
作业:
1.apple(静态成员)
1 //程序填空,使其输出4 5 1 2 #include <iostream> 3 using namespace std; 4 class Apple { 5 // 在此处补充你的代码 6 private: 7 static int nTotalNumber; //静态成员变量 8 public: 9 Apple(){ 10 nTotalNumber++; 11 } 12 ~Apple(){ 13 nTotalNumber--; 14 } 15 static void PrintTotal() { //静态成员函数只能访问静态成员变量,否则解释不通是哪个对象的成员变量 16 cout << nTotalNumber << endl; 17 } 18 19 }; 20 int Apple::nTotalNumber = 0; //静态成员变量要在外面重新声明一次 21 Apple Fun(const Apple & a) { 22 a.PrintTotal(); 23 return a; //函数返回一个对象会生成一个临时对象,但这里用的是复制构造函数!!! 24 } 25 int main() 26 { 27 Apple * p = new Apple[4]; //生成了四个对象 28 Fun(p[2]); //4 这里用完函数的返回值生成的临时对象后,临时对象会消亡 ,所以总数会减一 29 Apple p1,p2; 30 Apple::PrintTotal ();//5 这两种调用PrintTotal的方式是等价的,因为是静态的。 31 delete [] p;//记得删除 32 p1.PrintTotal (); //1 33 return 0; 34 }
2.返回什么好呢(this)
1 /*输入 2 多组数据,每组一行,是整数 m 和 n 3 输出 4 先输出一行: 5 123 6 然后,对每组数据,输出两行,第一行是m,第二行是n 7 */ 8 #include <iostream> 9 using namespace std; 10 class A { 11 public: 12 int val; 13 A(int vv=123){ 14 val = vv; 15 } 16 // 在此处补充你的代码 17 A& GetObj(){ //返回值必须是一个引用,这样返回的结果才能写到等号左边,等价于对返回值进行赋值 18 return *this; 19 } 20 }; 21 int main() 22 { 23 int m,n; 24 A a; 25 cout << a.val << endl; 26 while(cin >> m >> n) { 27 a.GetObj() = m; 28 cout << a.val << endl; 29 a.GetObj() = A(n); 30 cout << a.val<< endl; 31 } 32 return 0; 33 }
3.Big & Base 封闭类问题(封闭类)
1 /*输入 2 多组数据,每组一行,是一个整数 3 输出 4 对每组数据,输出两行,每行把输入的整数打印两遍*/ 5 #include <iostream> 6 #include <string> 7 using namespace std; 8 class Base { 9 public: 10 int k; 11 Base(int n):k(n) { } 12 }; 13 class Big 14 { 15 public: 16 int v; 17 Base b; 18 Big(int n):v(n),b(n){} //通过初始化列表来初始化 19 // 在此处补充你的代码 20 }; 21 int main() 22 { 23 int n; 24 while(cin >>n) { 25 Big a1(n); 26 Big a2 = a1; //复制构造函数 27 cout << a1.v << "," << a1.b.k << endl; 28 cout << a2.v << "," << a2.b.k << endl; 29 } 30 }
4.魔兽世界之一:备战(!!!令人头大)
描述
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。
红司令部,City 1,City 2,……,City n,蓝司令部
两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。
双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。
武士在刚降生的时候有一个生命值。
在每个整点,双方的司令部中各有一个武士降生。
红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。
蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。
制造武士需要生命元。
制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。
如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。
给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下:
1) 武士降生
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
2) 司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在10点整,红方司令部停止制造武士
输出事件时:
首先按时间顺序输出;
同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。
输入第一行是一个整数,代表测试数据组数。
每组测试数据共两行。
第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 10000)。
第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。输出对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 。
接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。样例输入
1 20 3 4 5 6 7
样例输出
Case:1 000 red iceman 1 born with strength 5,1 iceman in red headquarter 000 blue lion 1 born with strength 6,1 lion in blue headquarter 001 red lion 2 born with strength 6,1 lion in red headquarter 001 blue dragon 2 born with strength 3,1 dragon in blue headquarter 002 red wolf 3 born with strength 7,1 wolf in red headquarter 002 blue ninja 3 born with strength 4,1 ninja in blue headquarter 003 red headquarter stops making warriors 003 blue iceman 4 born with strength 5,1 iceman in blue headquarter 004 blue headquarter stops making warriors
1 #include <iostream> 2 #include <string> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 //武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性 7 //红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。 8 //蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。 9 //编程规范:变量名中间字母大写,类名、函数名首字母大写 10 //莫名其妙的问题看看循环的下标是不是写错了! 11 class Command; //复合关系 12 class Soldier{ //武士 13 private: 14 Command *com; //属于哪个司令部 15 int kind; //是哪种武士 16 int id; //编号 17 public: 18 Soldier(Command*p, int kindd, int idd); 19 void Print(int time); 20 static int life[5]; //生命值 21 static string name[5]; //dragon 、ninja、iceman、lion、wolf 22 }; 23 24 class Command{ //司令部 25 private: 26 Soldier *mySoldier[1000]; //都有哪些士兵 27 int HP; //总血量 28 int color; //蓝方为1, 红方为0 29 int sNum[5]; //存放每种士兵的数量 30 int curID; //当前制造到了第curID个士兵 31 int curkind; //当前制造的种类(用在该颜色的order里是第几个表示,即相对编号) 32 bool flag; //用来记录生命值用完了没有 33 public: 34 static int order[2][5]; //制造顺序 35 void Init(int col, int life); 36 bool Creat(int time); 37 ~Command(); 38 friend class Soldier; 39 }; 40 41 string Soldier::name[5]={"dragon","ninja","iceman","lion","wolf"}; 42 int Soldier::life[5]; 43 int Command::order[2][5]={{2,3,4,1,0},{3,0,1,2,4}}; 44 45 Soldier::Soldier(Command *p, int kindd, int idd){ 46 com = p; 47 kind = kindd; 48 id = idd; 49 } 50 51 void Soldier::Print(int time){ 52 char color[6]; 53 if(com->color==1) strcpy(color, "blue"); //char字符串可以直接用=初始化,但不能这么赋值 54 else strcpy(color, "red"); 55 printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n", 56 time, color, name[kind].c_str(), id, life[kind], com->sNum[kind],name[kind].c_str(),color); 57 } 58 59 void Command::Init(int col, int life){ //初始化 60 color = col; 61 HP = life; 62 flag = false; 63 curID = 1; 64 curkind = 0; 65 memset(sNum, 0, sizeof(sNum)); 66 } 67 68 bool Command::Creat(int time){ 69 if(flag) return false; //已经不能再造了 70 int i, kind; 71 int orikind = curkind; 72 for(i = 0; i < 5; i++){ 73 curkind = (orikind+i)%5; //这块不能写成curkind = (curkind+i)%5;!!!!!注意!!!! 74 kind = order[color][curkind]; //这里是绝对编号 75 if(Soldier::life[kind]<=HP) break; 76 } 77 if(i >= 5){ //已经轮完一轮了,并且这是第一次不能制造新兵 78 flag = true; 79 if(color) printf("%03d blue headquarter stops making warriors\n",time); //格式化输出 80 else printf("%03d red headquarter stops making warriors\n",time); 81 return false; 82 } 83 //成功制造士兵 84 mySoldier[curID]=new Soldier(this, kind, curID); 85 sNum[kind]++; 86 mySoldier[curID]->Print(time); 87 HP-=Soldier::life[kind]; 88 //各种序号要+1 89 curkind = (curkind+1)%5; 90 curID++; 91 return true; 92 } 93 94 Command::~Command(){ 95 for(int i=0;i<curID;i++) 96 delete mySoldier[i]; 97 } 98 99 int main(){ 100 int T, M; 101 Command Blue, Red; 102 scanf("%d", &T); 103 for(int i = 1; i <= T; i++){ 104 printf("Case:%d\n",i); 105 scanf("%d",&M); 106 Blue.Init(1,M); 107 Red.Init(0,M); 108 for(int j = 0; j <= 4; j++) 109 scanf("%d", &Soldier::life[j]); 110 int time = 0; 111 while(1){ 112 bool flagred = Red.Creat(time); 113 bool flagblue = Blue.Creat(time); 114 time++; 115 if(!flagred&&!flagblue) break; 116 } 117 } 118 return 0; 119 }
备注:虽然这道题很让人头大,但无疑还是对oop的理解很有帮助的orz 勤加练习才可以。。