Microsoft Word - 第7章 类与对象.docx

Similar documents
Microsoft Word - 第3章.doc

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

C 1

C/C++程序设计 - 字符串与格式化输入/输出

Strings

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

untitled

C/C++语言 - C/C++数据

Strings

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

新版 明解C++入門編

untitled

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

c_cpp

FY.DOC

untitled

Microsoft Word - chap10.doc

C/C++ - 函数

C/C++ - 字符输入输出和字符确认

新・解きながら学ぶJava

概述

Chapter 9: Objects and Classes

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

C/C++ - 文件IO

C/C++ 语言 - 循环

全国计算机技术与软件专业技术资格(水平)考试

第3章.doc


C/C++ - 数组与指针

untitled

C C

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

提问袁小兵:

ebook39-5

C/C++语言 - 运算符、表达式和语句

qq

团 学 要 闻 我 校 召 开 共 青 团 五 届 九 次 全 委 ( 扩 大 ) 会 议 3 月 17 日, 我 校 共 青 团 五 届 九 次 全 委 ( 扩 大 ) 会 议 在 行 政 办 公 楼 五 楼 会 议 室 举 行, 校 团 委 委 员 各 院 ( 系 ) 团 委 书 记 校 学 生

IO

真 理 大 學 統計與精算學系

< F20B4F2D3A1D7F7D2B5>

腊八粥的来历 南宋陆游诗云 今朝佛粥更相馈 反觉江村节 物新 说的就是腊八粥 可见 腊八节 吃 腊八 粥 的风俗 由来已久 每逢腊八这一天 不论是朝 廷 官府 寺院还是黎民百姓家都要做腊八粥 这一 天 人们还要祭祀祖先 众神并庆祝丰收 后来 逐 渐演变成吃腊八粥祝来年五谷丰登 对于腊八粥的来历说法也

nooog

CC213

神州傳播協會

CHAPTER VC#

Microsoft Word - ch04三校.doc

_汪_文前新ok[3.1].doc

ebook39-6

日 期 课 堂 内 容 生 词 语 法 和 重 点 练 习 家 庭 作 业 内 容 9/6/2014 第 一 课 在 公 园 里 ( 上 ) 生 字 组 词 : 附 近 显 得 格 外 宁 静 艺 1 做 作 业 册 A 第 1 页 到 第 3 页 术 复 制 1 介 绍 自 己, 宣 读 学 校

C++ 程式設計

JLX

Microsoft Word 新聞稿附件.doc

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

二零一五年施政報告 - 施政綱領 - 第三章 扶貧及為弱勢社群提供支援

育 部 分 則 由 陳 淑 貞 委 員 及 李 兆 環 委 員 共 同 執 行, 在 此 先 感 謝 各 位 委 員 及 學 者 專 家 之 參 與 二 目 前 評 論 報 告 初 稿 之 架 構 區 分 為 對 政 府 機 關 回 應 意 見 之 觀 察 優 點 及 待 改 進 事 項, 以 及

<4D F736F F D20BACBB0B2C8ABD3EBB7C5C9E4D0D4CEDBC8BEB7C0D6CEA1B0CAAEB6FECEE5A1B1B9E6BBAEBCB C4EAD4B6BEB0C4BFB1EA2E646F63>

<4F4BBEFAA576A470BBA15FC160AAED E786C73>

58, ,769 51,911 74,666 35, , , , ,

辽宁铁~1

壹、學校背景

游戏攻略大全(十).doc

保 险 公 司 金 富 月 盈 两 产 全 品 保 名 险 称 ( 分 红 型 ) 产 分 品 红 类 型 缴 费 年 类 缴 型 缴 10 费 年 期 缴 限 保 险 期 限 ( 年 ) 聚 富 2 号 两 全 保 险 ( 万 能 型 ) 万 能 型 趸 缴 趸 缴 6 年 龙 享 安 康 重 疾

I

海底捞你学不会

YEN/MIAO CHING MS 27FEB DEL HKG

Microsoft Word - 02文本.docx

Transcription:

第 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