第 7 章类与对象 案例 1 地址类 #include <iostream> class Address char Name[21]; char Street[51]; char City[51]; char Postcode[10]; SetAddress(char *name,char *street,char *city,char *postcode); void ChangeName(char *name); void Display(); ; Address::SetAddress(char *name,char *street,char *city,char *postcode) strcpy(street,street); strcpy(city,city); strcpy(postcode,postcode); void Address::ChangeName(char *name) void Address::Display() cout<<name<<" "<<Street<<" "<<City<<" "<<Postcode<<endl; int main() Address a1; a1.setaddress(" 张三 "," 平安大街 3 号 "," 北京 ","100000"); cout<<" 地址信息如下 :"<<endl; a1.display(); a1.changename(" 李四 "); cout<<" 修改后的地址信息如下 :"<<endl; Address *ptr=&a1; ptr->display(); 1
案例 2 点类和圆类 #include <iostream> class Point // 点类 int x, y; // 点的 x 和 y 坐标 void SetPoint( int a, int b ) // 设置坐标 x = a; y = b; int GetX() return x; // 取 x 坐标 int GetY() return y; // 取 y 坐标 void Print() // 输出点的坐标 cout << '[' << x << ", " << y << ']'; ; class Circle // 圆类 double Radius; Point Center; void SetRadius(double r) // 设置半径 Radius = ( r >= 0? r : 0 ); void SetCenter(Point p) // 设置圆心坐标 Center = p; double GetRadius() // 取半径 return Radius; Point GetCenter() // 取圆心 return Center; double Area() // 计算面积 return 3.14159 * Radius * Radius; double Circumference() // 计算圆的周长 return 2*3.14159*Radius; void Print() // 输出圆的信息 cout << " 圆心坐标 = "; Center.Print(); cout << "; 半径 = " << Radius << endl; ; ; int main() 2
int a,b,r; Point center; cout<<" 请输入圆心坐标 :"; cin>>a>>b; center.setpoint(a,b); Circle c; c.setcenter(center); cout<<" 请输入圆的半径 :"; cin>>r; c.setradius(r); cout << " 圆的信息如下 : "<< endl; c.print(); cout << " 圆的面积 = " << c.area()<< endl; cout << " 圆的周长 = " << c.circumference() << endl; 案例 3 电子时钟类 #include <iostream> class ElectronicClock private: int year,month,day; int hour,minute,second; ElectronicClock() year=2000; month=1; day=1; hour=0; minute=0; second=0; ElectronicClock(int y,int m,int d,int h,int mi,int s) year=y; month=m; day=d; hour=h; minute=mi; second=s; 3
void print() cout<<" 现在电子时钟的时间 :"<<endl; cout<<year<<" 年 "<<month<<" 月 "<<day<<" 日 "<<endl; cout<<hour<<" 时 "<<minute<<" 分 "<<second<<" 秒 "<<endl; ; int main() char ch; int y,m,d,h,mi,s; cout<<" 是否要设初始值 (Y/N):"; cin>>ch; if (ch=='n' ch=='n') ElectronicClock datetime1; datetime1.print(); cout<<" 请输入初始值 ( 年 月 日 时 分 秒 ):"; cin>>y>>m>>d>>h>>mi>>s; ElectronicClock datetime2(y,m,d,h,mi,s); datetime2.print(); 案例 4 学生成绩信息类 #include<iostream> #include <cstring> class Info int Id; char Name[20]; int Programming; int Network; int Database; int Total; void Set_info(int id, char * name, int programming, int network, int database); int Get_Pro(); 4
int Get_Net(); int Get_Dat(); int Get_Tol(); void Show(); ; void Info::Set_info(int id, char * name, int programming, int network, int database) Id=id; Programming=programming; Network=network; Database=database; Total=programming+network+database; int Info::Get_Pro() return Programming; int Info::Get_Net() return Network; int Info::Get_Dat() return Database; int Info::Get_Tol() return Total; void Info::Show() cout<<id<<"\t"; cout<<name<<"\t"; cout<<programming<<"\t"; cout<<network<<"\t"; cout<<database<<"\t"; cout<<total<<endl; int main() const int COUNT=5; int i=0,j=0; int id; 5
char name[20]; int programming; int network; int database; Info student[count], tmp; cout<<" 请输入学生成绩的信息 "<<endl; cout<<" 学号姓名程序设计计算机网络数据库 "<<endl; for(i=0;i<count;i++) cin>>id>>name>>programming>>network>>database; student[i].set_info(id,name,programming,network,database); cout<<" 按总分高低排名如下 :"<<endl; for(i=0; i<count; i=i+1) for(j=count-1; j>i; j=j-1) if(student[j].get_tol()>student[j-1].get_tol()) tmp=student[j]; student[j]=student[j-1]; student[j-1]=tmp; cout<<" 学号姓名程序设计计算机网络数据库总分 "<<endl; for(i=0; i<count; i++) student[i].show(); cout<<" 每门课程成绩都大于 85 分的学生名单 :"<<endl; cout<<" 学号姓名程序设计计算机网络数据库总分 "<<endl; for(i=0; i<count; i++) if(student[i].get_pro()>85 && student[i].get_net()>85 && student[i].get_dat()>85) student[i].show(); 案例 5 阿猫阿狗的爱情游戏 #include<iostream> #include <cstring> class Boy bool House; // 有房 ; int Age; // 年龄 ; char Name[20]; Boy(char *name); 6
bool Gift(char gift); int AnnualSalary(int salary); ; Boy::Boy(char *name) if(rand() == 100) House=true; bool Boy::Gift(char gift) if(gift=='y' gift=='y') return true; return false; int Boy::AnnualSalary(int salary) cout<<name<<" 日夜操劳, 两眼发黑, 为的是早日赚到老婆本 "<<endl; salary=salary*365; return salary; class Girl int Feeling; // 情感指数 bool Willing; // 是否愿意等 int Birthday; // 生日 char Name[20]; Girl(char *name); void Love(char *name); void Marry(char *name); ; Girl::Girl(char *name) void Girl::Love(char *name) cout<<"\n 新的故事开始了,"<<Name<<" 与 "<<name<<" 进入相恋阶段..."<<endl; void Girl::Marry(char *name) 7
cout<<"\n 大团圆结局,"<<Name<<" 同意嫁给 "<<name<<", 从此两人过上了幸福生活..."<<endl; int main( ) Boy theboy("dog"); Girl thegirl("cat"); theboy.age=24; thegirl.feeling=1; thegirl.willing=true; thegirl.birthday=1001; int year=1; char gift='n'; int money=0; int salary=0; thegirl.love(theboy.name ); // 故事开始 if(theboy.house == true) thegirl.marry(theboy.name); while(thegirl.willing==true) cout<<"\n***** 第 "<<year<<" 年 *****\n"; for( int day=1; day<=365; day++) if( day == 45) // if ( 节日转换 (day) == 情人节 ) cout<<" 情人节到了, 是否给女朋友送礼物呢?(Y/N)"; cin>>gift; if( theboy.gift(gift)) thegirl.feeling++; thegirl.feeling--; if( day == 276) //if ( 日期转换 (day) == thegirl.birthday) cout<<" 生日到了, 是否给女朋友送礼物呢?(Y/N)"; cin>>gift; if( theboy.gift(gift)) thegirl.feeling++; thegirl.feeling--; 8
cout<<" 请输入今年的日工资 :"; cin>>salary; money=money+theboy.annualsalary(salary); cout<<" 年终小结 :\t"<<" 男孩年龄 :"<<theboy.age<<", 男孩存款 :"<<money<<", 女孩情感指数 :"<<thegirl.feeling; if(money>1000000) theboy.house=true; break; year++; theboy.age++; thegirl.feeling--; if(theboy.age>30 thegirl.feeling<0) thegirl.willing=false; if(theboy.house==true) thegirl.marry(theboy.name); Boy anotherboy("mouse"); thegirl.love(anotherboy.name); 9