解析C例题代码第3章例题_第1页
解析C例题代码第3章例题_第2页
解析C例题代码第3章例题_第3页
解析C例题代码第3章例题_第4页
解析C例题代码第3章例题_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上第3章例题【例3-1】/employee2.h#include <iostream>#include <string>using namespace std;class Employee /定义Employee类public:Employee(); /构造函数的声明 void Setname(string); /设置姓名函数 void Setpay(float pay); /设置工资函数 string Getname();/提取姓名函数void Display();/显示信息函数protected:string name; /姓名int no;

2、 /编号float salary; /月工资 static int maxno; /静态数据成员,表示员工编号基数;/employee2.cpp#include "employee2.h"int Employee:maxno=1000; /静态数据成员初始化Employee:Employee() /构造函数的实现 no=+maxno;void Employee:Setname(string namep) /设置姓名函数,指针作函数参数 name=namep;void Employee:Display()/显示信息函数 cout<<"编号为:"

3、<<no<<","<<"本月工资为:"<<salary<<endl;string Employee:Getname()/提取姓名函数return name;void Employee:Setpay(float pay)/设置工资函数salary=pay;/example3_34.cpp#include "employee2.h"int main() Employee employy4; /创建对象数组float pay;string nameptr;/字符数组int i;fo

4、r(i=0;i<4;i+)cout<<"请输入下一个员工的姓名:"cin>>nameptr;employyi.Setname(nameptr);cout<<"请输入员工的月工资:"cin>>pay;employyi.Setpay(pay);for(i=0;i<4;i+)cout<<employyi.Getname()<<"的"employyi.Display();return 0;【例3-2】/example3_2.cpp#include <io

5、stream>using namespace std;void f1()int a=1,b=2; static int c; a+; b+; c+; cout<<a<<' '<<b<<' '<<c<<endl;int main() int a=1,b=2,c=3; f1(); f1(); f1(); cout<<a<<' '<<b<<' '<<c<<endl; return 0;【例

6、3-3】/example3_3.cpp#include <iostream>using namespace std; int main()int *iptr; /声明 int型指针 iptrint i; /声明int型数 iiptr=&i; /取 i的地址赋给 iptri=100; /int型数赋初值cout<<"Output int i="<< i<< endl; /输出int型数的值cout<<"Output int pointer i="<<*iptr<<e

7、ndl;/输出指针所指地址的内容return 0;【例3-4】/example3_4.cpp#include <iostream>using namespace std;int main()int *a=new int; /在堆中分配int型变量所需空间,并将起始地址赋给指针a*a=76; /将76存入指针a指向的内存空间cout<<*a<<endl; /输出指针a指向的内存空间的值delete a;/释放a指向的动态存储空间return 0;【例3-5】/example3_5.cpp#include <iostream>using namesp

8、ace std;int main()int arraysize;int *array;cout<<"please input a number of array: "cin>>arraysize;if(array=new intarraysize)=NULL) /申请一块连续的存储空间cout<<"Can't allocate memory, terminating."/未分配到存储空间exit(1); /发生错误,退出程序for(int count=0;count<arraysize;count+)ar

9、raycount=count*2; cout<<arraycount<<" "cout<<endl;delete array; /释放array指向的连续存储空间return 0;【例3-6】/example3_6.cpp#include <iostream>using namespace std;struct list/结构体int data;list *next;class Queue/队列类public:Queue()/构造函数ptrf=ptrb=NULL; /将两个指针初始化为空void enqueue(int); /

10、入队函数int dequeue(); /出队函数private:list *ptrf; /队首指针list *ptrb;/队尾指针;void Queue:enqueue(int x) /入队函数的实现list *newnode=new list; /动态分配内存 newnode->data=x;newnode->next=NULL;if(ptrb=NULL) /队空时入队的情况,如图3-4所示ptrf=ptrb=newnode;else /队非空时入队的情况,如图3-5所示ptrb->next=newnode; ptrb=newnode;int Queue:dequeue()

11、 /出队函数的实现list *tmp;int value;value=ptrf->data;tmp=ptrf;ptrf=ptrf->next; /出队后的情况如图3-6所示delete tmp;return value; /返回出队的数据项int main()Queue A; /队列对象Aint arr=2,3,4,5,7; cout<<"入队顺序:"for(int i=0;i<5;i+)cout<<arri<<" "A.enqueue(arri); /将数组元素作为数据项依次入队cout<&l

12、t;endl<<"出队顺序:"for(i=0;i<5;i+)cout<<A.dequeue()<<" " /将队列中数据项依次出队cout<<endl; return 0;【例3-7】/student.h#include <iostream>using namespace std;class Student /学生类的声明 int id; /私有数据成员,外部不可见char name10; float score; /为了简化程序,只保留了一个学生成绩public: /公有成员函数,外部接

13、口void input(int pid,char *pname,float s);void modify(float s);void display();/student.cpp#include "student.h"void Student:input(int pid,char *pname,float s) /成员函数的实现id=pid;strcpy(name,pname);score=s;void Student:modify(float s)score=s; void Student:display() cout<<" id:"<

14、;<id<<endl; /虽在类外,成员函数仍可访问私有成员cout<<" name:"<<name<<endl;cout<<"score:"<<score<<endl;/example3_7.cpp#include "student.h"int main()Student *pstu=new Student; /动态申请内存空间(*pstu).input(,"Wang Li",90); /通过堆对象访问公有成员函数(*pst

15、u).display(); /通过堆对象访问公有成员函数(*pstu).modify(85); /通过堆对象访问公有成员函数(*pstu).display(); /通过堆对象访问公有成员函数delete pstu;return 0;【例3-8】/example3_8.cpp#include <iostream>using namespace std; namespace myown1 /定义名字空间myown1char *user_name="myown1"/在名字空间myown1中定义指针名user_namenamespace myown2 /定义名字空间my

16、own2char *user_name="myown2"/在名字空间myown2中定义指针名user_nameint main()cout<<"Hello, " <<myown1:user_name /用名字空间限制符访问变量user_name<<". and goodbye!"<<endl;cout <<"Hello, " <<myown2:user_name /用名字空间限制符访问变量user_name<<". and

17、 goodbye!"<<endl;return 0;【例3-9】/example3_9.cpp#include <iostream>using namespace std;/void v; /错误,不能声明void类型的变量void *vp; /正确,可以声明void类型的指针int main()int *ip;int i=10;vp=&i; /void类型指针指向整型变量/cout<<"*vp="<<*vp<<endl;/错误,void型指针在使用前必须确定类型ip=(int *)vp; /类型

18、强制转换, void类型指针赋值给int类型指针cout<<"*ip="<<*ip<<endl; return 0;【例3-10】/example3_10.cpp#include "student.h"int main()Student s1; /定义Student类的对象s1s1.input(,"Zhang Hua",95);/通过s1访问公有成员函数s1.display(); /通过对象访问公有成员函数Student *pstu=&s1; /定义Student类的对象指针pstu,并指向

19、对象s1pstu->input(,"Wang Li",90); /通过指针访问公有成员函数pstu->display(); /通过指针访问公有成员函数(*pstu).modify(85); (*pstu).display();return 0;【例3-11】/example3_11.cpp#include <iostream>using namespace std;class Boxpublic:Box(); Box(int h,int w ,int len):height(h),width(w),length(len)int volume();Bo

20、x();private:int height;int width;int length;Box:Box() height=10;width=10;length=10;Box:Box() cout<<"Destructor is called"<<endl;int Box:volume()return(height*width*length);int main()Box *pbox1=new Box; /定义指向Box堆对象的指针变量pbox1,cout<<"The volume of box1 is "<<

21、pbox1->volume()<<endl;delete pbox1; /释放pbox1指向的对象空间Box *pbox2=new Box(15,30,25);/定义指向Box堆对象的指针变量pbox2, /在pbox2中存放堆对象的起始地址并初始化堆对象 cout<<"The volume of box2 is "<<pbox2->volume()<<endl;/指针访问成员delete pbox2; /释放pbox2指向的对象空间return 0; 【例3-12】/example3_12.cpp#include

22、 <iostream>using namespace std;void swap(int *x,int *y) /形参为指针int *temp; temp=*x;*x=*y;*y=temp;int main() int a=1,b=2; /定义变量cout<<"Before Swap a="<<a<<",b="<<b<<endl; swap(&a,&b); /函数调用 cout<<"After Swap a="<<a<

23、<",b="<<b<<endl; return 0;【例3-13】/example3_13.cpp#include <iostream>using namespace std;class CStrtemppublic: CStrtemp(char *s); CStrtemp(const CStrtemp&); /复制构造函数,省略形参 CStrtemp(); /析构函数 void show(); /成员函数 void set(char *s); /赋值新串函数private: char *str; /私有数据成员;CStrt

24、emp:CStrtemp(char *s)cout<<"constructor."<<endl;str=new charstrlen(s)+1; /动态申请内存单元,见3.2.3节if(!str)cerr<<"Allocationg Error"<<endl;/cerr是标准流对象,参看第8章exit(1);/发生错误,退出程序strcpy(str,s); /将字符串复制到str指向的存储空间中CStrtemp:CStrtemp(const CStrtemp& temp) /复制构造函数的实现 co

25、ut<<"copy constructor."<<endl; str=new charstrlen(temp.str)+1; if(!str) cerr<<"error in apply new space."<<endl; exit(1); /发生错误,退出程序 strcpy(str,temp.str);CStrtemp:CStrtemp() /析构函数cout<<"destructor."<<endl;if(str!=NULL)delete str; /释放s

26、tr指向的存储空间void CStrtemp:show() /成员函数cout<<str<<endl;void CStrtemp:set(char *s) /成员函数的实现 delete str; /释放str指向的存储空间 str=new charstrlen(s)+1; if(!str) cerr<<"Allocation Error"<<endl; exit(1); /发生错误,退出程序 strcpy(str,s);CStrtemp Input(CStrtemp *temp) /对象指针用做函数参数 char s20;

27、cout<<"Please input the string:" cin>>s; /输入新字符串 temp->set(s); /指针访问成员函数 return *temp; /返回指针所指向的对象int main() CStrtemp A("hello"); /用字符串"hello"初始化对象A A.show(); CStrtemp B=Input(&A); /用对象指针所指向的对象初始化对象B A.show(); B.show(); return 0;【例3-14】/point.h #inclu

28、de<iostream> #include<cmath> using namespace std; class point /声明类 public: point(double=0,double=0);/构造函数 double Getx(); /成员函数原型 double Gety(); void Setxy(double,double); point(); private: double x,y; ; const int num=10; /申请数量 void Set(point *); /普通函数原型 void Display(point *);double Lenth

29、(point *); /point.cpp#include"point.h" /定义类的成员函数 point:point(double a,double b) x=a; y=b; double point:Getx() return x; double point:Gety() return y; void point:Setxy(double a,double b) x=a; y=b; point:point() cout<<"delete it:"<<x<<","<<y<<

30、;endl; /example3_14.cpp#include"point.h" int main() point *p=new point10;/申请10个对象的内存 if(p=NULL) /如果没有申请到,则退出程序 cout<<"地址申请失败,结束程序运行。n" return 0; Set(p); /调数据输入函数 cout<<"内存块的数据如下:"<<endl; Display(p); /显示数据 cout<<"组成的折线长度为:" cout<<L

31、enth(p)<<endl; /输出折线长度 delete p; /释放内存 return 0; /给内存块赋值的函数 void Set(point *p) double a,b; for(int i=0;i<num;i+) cout<<"Input 第"<<i+1<<"个对象的两个数据成员的值:" cin>>a>>b; (p+i)->Setxy(a,b); /给申请的内存块赋值 /显示内存块的值的函数 void Display(point *p) for(int i=0

32、;i<num;i+) cout<<(p+i)->Getx()<<","<<(p+i)->Gety()<<endl; /计算折线长度的函数 double Lenth(point *p) double sum(0.0), a1,b1,a2,b2; a1=p->Getx(); /保存第一个点坐标的x值 b1=p->Gety(); /保存第一个点坐标的y值 for(int i=1;i<10;i+) a2=(p+i)->Getx(); /取下一点的x坐标 b2=(p+i)->Gety();

33、 /取下点的Y坐标 sum=sum+sqrt(a1-a2)*(a1-a2)+(b1-b2)*(b1-b2); /累计长度 a1=a2; /依次平移 b1=b2; /依次平移 return sum; 【例3-15】/example3_15.cpp#include <iostream>using namespace std;class Boxpublic: Box(int h=10,int w=12,int len=15):height(h),width(w),length(len) /带默认参数的构造函数,含成员初始化表int volume();private: int height

34、; int width; int length;int Box:volume()return(height*width*length); int main() Box a,b(15,18,20),c(16,20,26); cout<<"volume of a is "<<a.volume()<<endl; cout<<"volume of b is "<<b.volume()<<endl; cout<<"volume of c is "<<

35、c.volume()<<endl; return 0;【例3-16】/example3_16.cpp#include <iostream>using namespace std;class Testpublic:Test(int=0);void print();private:int x;Test:Test(int a) x=a;void Test:print()cout<<"x="<<x<<"nthis->x="<<this->x <<"n(*th

36、is).x="<<(*this).x<<endl;int main() Test t(12);t.print();return 0;【例3-17】/example3_17.cpp#include <iostream>using namespace std;class Apublic:A()i=0; void add()i+;A *sp()return this; /定义指针函数sp(),显式使用thisint Is(A *s)return s->i; /通过对象指针访问私有数据成员private: int i; ;A *p2; /定义全局对象

37、指针数组p2int main()A a1,*a2=new A; /定义对象指针a2p0=a1.sp();p1=a2;p0->add(); /通过对象指针访问公有成员函数a2->add();p1->add();cout<<"a1-this:"<<p0<<" a2-this:"<<p1<<endl;cout<<"i-(a1):"<<p1->Is(p0)<<" i-(a2):"<<p0-&g

38、t;Is(p1)<<endl;return 0;【例3-18】/example3_18.cpp#include <iostream>using namespace std;class Point public: Point(int xx=0,int yy=0)X=xx;Y=yy;/构造函数 int GetX()return X; int GetY()return Y;private: int X,Y;int main() Point A(4,5);/声明对象A Point *p1=&A;/声明对象指针并初始化 int (Point:*pGetX)()= Poin

39、t:GetX;/声明成员函数指针并初始化 cout<<(A.*pGetX)()<< 't' /使用成员函数指针访问成员函数 cout<<(p1->GetX)()<< 't' /使用对象指针访问成员函数 cout<<A.GetX()<<endl;/使用对象名访问成员函数 return 0;【例3-19】/example3_19.cpp#include <iostream>using namespace std;class Point public: Point(int xx=

40、0,int yy=0)X=xx;Y=yy;count+;/构造函数 Point(const Point &p);/复制构造函数的声明 int GetX()return X; int GetY()return Y; static int count;/静态数据成员private: int X,Y; ;int Point:count=0;/静态数据成员的初始化Point:Point(const Point &p) /复制构造函数的实现 X=p.X; Y=p.Y; count+;int main()/主函数 int *countp=&Point:count;/声明一个int型

41、指针,指向类的静态成员 Point A(4,5);/声明对象A cout<< "Point A: "<<A.GetX()<<","<<A.GetY();/通过对象访问成员函数 cout<< " Object id= "<<*countp<<endl;/通过指针访问静态数据成员 Point B(A);/声明对象B cout<< "Point B: "<<B.GetX()<<","

42、<<B.GetY(); cout<< " Object id= "<<*countp<<endl;/通过指针访问静态数据成员 return 0;【例3-20】/example3_20.cpp#include <iostream>using namespace std;class Point public: Point(int xx=0,int yy=0)X=xx;Y=yy;count+;/构造函数 Point(const Point &p);/复制构造函数 int GetX()return X; int G

43、etY()return Y; static void GetC()cout<<" Object id= "<<count<<endl;/静态成员函数private: int X,Y; static int count;/静态数据成员的初始化;Point:Point(const Point &p) X=p.X; Y=p.Y; count+;int Point:count=0; /静态数据成员的初始化int main()/主函数 void (*gc)()=Point:GetC;/声明一个指向函数的指针,指向类的静态成员函数 Point

44、 A(4,5);/声明对象A cout<<"Point A: "<<A.GetX()<<","<<A.GetY(); gc();/通过指针访问静态成员函数 Point B(A);/声明对象B cout<<"Point B: "<<B.GetX()<<","<<B.GetY(); gc();/通过指针访问静态成员函数 return 0;【例3-21】/example3_21.cpp#include <iostream

45、>using namespace std;int main() int a=5,b=5; int *ptr = &b; /定义并初始化指针 int & ref = a; /定义并初始化引用 *ptr += 2; ref += 2; cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl;【例3-22】/example3_22.cpp#include <iostream>using namespace std;int

46、main() int *ptr,a=10,b = 20; /定义指针,但未初始化 int & ref ; /定义引用,但未初始化 int &ref1 = 100; /用常量初始化非常量引用 const int &ref2 = 100; /用常量初始化常量引用 int & ref3 = a; ref2 += 2; ref3+;ref3 = b;cout<<" a = "<<a<<endl; return 0;【例3-23】/example3_23.cpp#include <iostream>usi

47、ng namespace std;int main() int a=1; int *p = &a; int & r = a; cout<<"取变量a的地址:"<<&a<<endl; /取变量a的地址 cout<<"取指针p的地址:"<<&p<<endl; /取指针p的地址 cout<<"取引用r的地址:"<<&r<<endl; /取引用r的地址return 0;【例3-24】/exampl

48、e3_24.cpp#include <iostream>using namespace std;void swap(int& x,int& y) /形参为引用 int temp; temp=x;x=y;y=temp;int main() int a=1,b=2; /定义变量cout<<"Before Swap a="<<a<<",b="<<b<<endl; swap(a,b); /函数调用 cout<<"After Swap a="&l

49、t;<a<<",b="<<b<<endl; return 0;【例3-25】/example3_25.cpp#include <iostream>using namespace std;class CStrtemppublic: CStrtemp(char *s); CStrtemp(const CStrtemp&); /复制构造函数,省略形参 CStrtemp(); /析构函数 void show(); /成员函数 void set(char *s); /赋值新串函数private: char *str; /私

50、有数据成员;CStrtemp:CStrtemp(char *s)cout<<"constructor."<<endl;str=new charstrlen(s)+1; /动态申请内存单元,见3.2.3节if(!str)cerr<<"Allocationg Error"<<endl;/cerr是标准流对象,参看第8章exit(1);/发生错误,退出程序strcpy(str,s); /将字符串复制到str指向的存储空间中CStrtemp:CStrtemp(const CStrtemp& temp) /复

51、制构造函数的实现 cout<<"copy constructor."<<endl; str=new charstrlen(temp.str)+1; if(!str) cerr<<"error in apply new space."<<endl; exit(1); /发生错误,退出程序 strcpy(str,temp.str);CStrtemp:CStrtemp() /析构函数cout<<"destructor."<<endl;if(str!=NULL)delet

52、e str; /释放str指向的存储空间void CStrtemp:show() /成员函数cout<<str<<endl;void CStrtemp:set(char *s) /成员函数的实现 delete str; /释放str指向的存储空间 str=new charstrlen(s)+1; if(!str) cerr<<"Allocation Error"<<endl; exit(1); /发生错误,退出程序 strcpy(str,s);CStrtemp Input(CStrtemp &temp) /对象引用做函

53、数参数 char s20; cout<<"Please input the string:" cin>>s; /输入新字符串 temp.set(s); /对象引用访问成员函数 return temp; /返回对象引用int main() CStrtemp A("hello"); /用字符串"hello"初始化对象A A.show(); CStrtemp B=Input(A); /用对象指针所指向的对象初始化对象B A.show(); B.show(); return 0;【例3-36】/example3_36.cpp#include <iostream>#include <string>#include <cstdlib>using namespace std;class CStrtemppublic: CStrtemp(char *s) str=s;/将字符串赋给string对象 CStrtemp(const CStrtemp&); /复制构造函数的声明,省略形参 void show() /成员函数 cout<<str<<endl; /输出string对象 void

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论