




已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一章 5: #include using namespace std; int main() cout using namespace std; int main() int a,b,c; a=10; b=23; c=a+b; cout using namespace std; int main() int a,b,c; int f(int x,int y,int z); cinabc; c=f(a,b,c); cout using namespace std; int main() int a,b,c; cinab; c=a+b; cout using namespace std; int main() int add(int x,int y); int a,b,c; cinab; c=add(a,b); cout using namespace std; int main() void sort(int x,int y,int z); int x,y,z; cinxyz; sort(x,y,z); return 0; void sort(int x, int y, int z) int temp; if (xy) temp=x;x=y;y=temp; / 内 3 个语句的作用是将 x 和 y 的值互换) if (z using namespace std; int main() int max(int a,int b,int c=0); int a,b,c; cinabc; couta) a=b; if(ca) a=c; return a; 12: #include using namespace std; int main() void change(int ,int ); int a,b; cinab; if(a using namespace std; int main() void sort(int int a,b,c,a1,b1,c1; coutabc; a1=a;b1=b;c1=c; sort(a1,b1,c1); coutj) change(i,j); if (ik) change(i,k); if (jk) change(j,k); void change(int temp=x; x=y; y=temp; 14: #include #include using namespace std; int main() string s1=“week“,s2=“end“; cout #include using namespace std; int main() string str; int i,n; char temp; coutstr; n=str.size(); for(i=0;i #include using namespace std; int main() int i; string str5=“BASIC“,“C“,“FORTRA N“,“C+“,“PASCAL“; void sort(string ); sort(str); coutsi+1) t=si;si=si+1;si+1=t; 17: #include #include using namespace std; int main() long c5=10100,- 123567, 1198783,-165654, 3456; int a5=1,9,0,23,-45; float b5=2.4, 7.6, 5.5, 6.6, -2.3 ; void sort(int ); void sort(float ); void sort(long ); sort(a); sort(b); sort(c); return 0; 3 void sort(int a) int i,j,t; for (j=0;jai+1) t=ai;ai=ai+1;ai+1=t; coutai+1) t=ai;ai=ai+1;ai+1=t; coutai+1) t=ai;ai=ai+1;ai+1=t; cout #include using namespace std; template void sort(T a) int i,j,min; T t; for(i=0;iaj) min=j; t=ai; ai=amin; amin=t; cout using namespace std; class Time public: void set_time(); void show_time(); private: /成员改为公用的 int hour; int minute; int sec; ; void Time:set_time() /在 main 函数之前定义 cinhour; cinminute; cinsec; void Time:show_time() /在 main 函数之前定义 cout using namespace std; class Time public: void set_time(void) cinhour; cinminute; cinsec; void show_time(void) cout using namespace std; class Time public: void set_time(void); void show_time(void); private: int hour; int minute; int sec; ; void Time:set_time(void) cinhour; cinminute; cinsec; void Time:show_time(void) cout using namespace std; #include “xt2-4.h“ int main() Student stud; stud.set_value(); stud.display(); return 0; /xt2-4-2.cpp(即 student.cpp) #include “xt2-4.h“ /在此文件中进行函数的定 义 #include using namespace std; /不要漏写此行 void Student:display( ) coutnum; cinname; cinsex; 5: /xt2-5-1.cpp(file1.cpp) #include #include “xt2-5.h“ int main() Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0; /xt2-5-2.cpp(arraymax.cpp) #include using namespace std; #include “xt2-5.h“ void Array_max:set_value() int i; for (i=0;iarrayi; void Array_max:max_value() int i; max=array0; for (i=1;imax) max=arrayi; void Array_max:show_value() cout using namespace std; class Box public: void get_value(); float volume(); void display(); public: float lengh; float width; float height; ; void Box:get_value() coutlengh; cinwidth; cinheight; float Box:volume() return(lengh*width*height); void Box:display() cout using namespace std; class Box public: void get_value(); void volume(); void display(); public: float lengh; float width; float height; float vol; ; void Box:get_value() coutlengh; cinwidth; cinheight; void Box:volume() vol=lengh*width*height; void Box:display() cout using namespace std; class Date public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; ; Date:Date(int m,int d,int y):month(m),day(d),year(y) Date:Date(int m,int d):month(m),day(d) year=2005; Date:Date(int m):month(m) day=1; year=2005; Date:Date() month=1; day=1; year=2005; void Date:display() cout using namespace std; class Date public: Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; ; Date:Date(int m,int d,int y):month(m),day(d),year(y) void Date:display() cout using namespace std; class Student public: Student(int n,float s):num(n),score(s) void display(); private: int num; float score; ; void Student:display() coutdisplay(); return 0; 5: #include using namespace std; class Student public: Student(int n,float s):num(n),score(s) int num; float score; ; void main() Student stud5= Student(101,78.5),Student(1 02,85.5),Student(103,98.5), Student(104,100.0),Student( 105,95.5); void max(Student* ); Student *p= max(p); void max(Student *arr) float max_score=arr0.score; int k=0; for(int i=1;imax_score) max_score=arri.score;k=i; cout using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display()cout using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) const num=n;score=s; void display() const cout using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() coutdisplay(); p-change(101,80.5); p-display(); return 0; 8: #include using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout using namespace std; class Product public: Product(int n,int q,float p):num(n),quantity(q),price( p); void total(); static float average(); static void display(); private: int num; int quantity; float price; static float discount; static float sum; static int n; ; void Product:total() float rate=1.0; if(quantity10) rate=0.98*rate; sum=sum+quantity*price*ra te*(1-discount); n=n+quantity; void Product:display() cout using namespace std; class Date; class Time public: Time(int,int,int); friend void display(const Date private: int hour; int minute; int sec; ; Time:Time(int h,int m,int s) hour=h; minute=m; sec=s; class Date public: Date(int,int,int); friend void display(const Date private: int month; int day; int year; ; Date:Date(int m,int d,int y) month=m; day=d; year=y; void display(const Date class Time; class Date public: Date(int,int,int); friend Time; private: int month; int day; int year; ; Date:Date(int m,int d,int y):month(m),day(d),year(y) class Time public: Time(int,int,int); void display(const Date private: int hour; int minute; int sec; ; Time:Time(int h,int m,int s):hour(h),minute(m),sec(s) void Time:display(const Date template class Compare public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; ; template Compare:Compa re(numtype a,numtype b) x=a;y=b; template numtype Compare:max() return (xy)?x:y; template numtype Compare:min() return (x cmp1(3,7); cout cmp2(45.78,93.6); cout cmp3(a,A); cout using namespace std; class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void display(); private: double real; double imag; ; double Complex:get_real() return real; double Complex:get_imag() return imag; void Complex:display() cout using namespace std; class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex Complex operator- (Complex Complex operator*(Complex Complex operator/(Complex void display(); private: double real; double imag; ; Complex Complex:operator+(Comple x c.real=real+c2.real; c.imag=imag+c2.imag; return c; Complex Complex:operator- (Complex c.real=real-c2.real; c.imag=imag-c2.imag; return c; Complex Complex:operator*(Comple x c.real=real*c2.real- imag*c2.imag; c.imag=imag*c2.real+real*c 2.imag; return c; Complex Complex:operator/(Comple x c.real=(real*c2.real+imag*c2.i mag)/(c2.real*c2.real+c2.im ag*c2.imag); c.imag=(imag*c2.real- real*c2.imag)/(c2.real*c2.re al+c2.imag*c2.imag); return c; void Complex:display() cout /用 VC+时改为 #include using namespace std; /用 VC+时为取消此行 class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex Complex operator+(int friend Complex operator+(int void display(); private: double real; double imag; ; Complex Complex:operator+(Comple x Complex Complex:operator+(int void Complex:display() cout using namespace std; class Matrix /定义 Matrix 类 public: Matrix(); /默认构造函数 friend Matrix operator+(Matrix /重载运算符 “+” void input(); /输入数据函数 void display(); /输出数据函数 11 private: int mat23; ; Matrix:Matrix() /定义构造函数 for(int i=0;imatij; void Matrix:display() /定义输出数据函数 for (int i=0;i /using namespace std; class Matrix public: Matrix(); friend Matrix operator+(Matrix friend ostream private: int mat23; ; Matrix:Matrix() for(int i=0;i(istream return in; ostream cinb; cout using namespace std; class Complex public: Complex()real=0;imag=0; Complex(double r)real=r;imag=0; Complex(double r,double i)real=r;imag=i; operator double()return real; void display(); private: double real; double imag; ; void Complex:display() cout using namespace std; class Student public: Student(int,char,char,float); int get_num()return num; char * get_name()return name; char get_sex()return sex; void display() cout using namespace std; class Student public: void get_value() cinnumnamesex; void display( ) coutageaddr; void display_1() cout using namespace std; class Student public: void get_value() cinnumnamesex; void display( ) coutageaddr; void display_1() display(); cout using namespace std; class Student /声明基类 public: /基类公用成员 void get_value(); void display( ); protected : /基类保护成员 int num; char name10; char sex; ; void Student:get_value() cinnumnamesex; void Student:display( ) coutageaddr; void Student1:display1( ) cout using namespace std; class Student /声明基类 public: /基类公用成员 void get_value(); void display( ); protected : /基类保护成员 int num; char name10; char sex; ; void Student:get_value() cinnumnamesex; void Student:display( ) coutageaddr; void Student1:display1( ) cout using namespace std; class Student /声明基类 public: /基类公用成员 void get_value(); void display( ); protected : /基类保护成员 int num; char name10; char sex; ; void Student:get_value() cinnumnamesex; void Student:display( ) coutageaddr; void Student1:display1( ) cout using namespace std; class A public: void f1( ); protected: void f2(); private: int i; ; class B: public A public: void f3( ); int k; private: int m; ; class C: protected B public: void f4(); protected: int n; private: int p; ; class D: private C public: void f5(); protected: int q; private: int r; ; int main() A a1; B b1; C c1; D d1; return 0; 7: #include using namespace std; class A public: A()a=0;b=0; A(int i)a=i;b=0; A(int i,int j)a=i;b=j; void display()cout using namespace std; class A public: A()cout #include using namespace std; class Teacher public: Teacher(string nam,int a,char s,string tit,string ad,string t); void display(); protected: string name; int age; char sex; string title; string addr; string tel; ; Teacher:Teacher(string nam,int a,char s,string tit,string ad,string t): name(nam),age(a),sex(s),titl e(tit),addr(ad),tel(t) void Teacher:display() cout #include using namespace std; class Teacher /教师类 public: Teacher(int,char ,char); / 声明构造函数 void display(); /声明输出函数 private: int num; char name20; char sex; ; Teacher:Teacher(int n,char nam,char s) /定义构 造函数 num=n; strcpy(name,nam); sex=s; void Teacher:display() /定义输出函数 cout / 如用 VC+应改为#include using namespace std; / 如用 VC+应取消此行 #include “cylinder.h“ #include “point.cpp“ #include “circle.cpp“ #include “cylinder.cpp“ int main() Cylinder cy1(3.5,6.4,5.2,10); cout using namespace std; class Point public: Point(float a,float b):x(a),y(b) Point()cout using namespace std; class Point public: Point(float a,float b):x(a),y(b) Point()cout using namespace std; class Point public: Point(float a,float b):x(a),y(b) virtual Point()cout using namespace std; /定义抽象基类 Shape class Shape public: virtual double area() const =0; /纯虚函 数 ; /定义 Circle 类 class Circle:public Shape public: Circle(double r):radius(r) /结构函数 virtual double area() const return 3.14159*radius*radius; /定义虚函数 protected: double radius; /半径 ; /定义 Rectangle 类 class Rectangle:public Shape public: Rectangle(double w,double h):width(w),height(h) /结构函数 virtual double area() const return width*height; /定义虚函数 protected: double width,height; /宽与高 ; class Triangle:public Shape public: Triangle(double w,double h):width(w),height(h) /结构函数 virtual double area() const return 0.5*width*height; /定义虚函数 protected: double width,height; /宽与高 ; /输出面积的函数 void printArea(const Shape /定义抽象基类 Shape class Shape public: virtual double area() const =0; /纯虚函数 ; /定义 Circle(圆形 )类 class Circle:public Shape public: Circle(double r):radius(r) /结构函数 virtual double area() const return 3.14159*radius*radius; /定义虚函数 protected: double radius; /半径 ; /定义 Square(正方形)类 class Square:public Shape public: Square(double s):side(s) /结构函数 virtual double area() const return side*side; /定义虚函数 protected: double side; ; /定义 Rectangle(矩形)类 class Rectangle:public Shape public: Rectangle(double w,double h):width(w),height(h) /结构函数 virtual double area() const return width*height; /定义虚函数 protected: double width,height; /宽与高 ; /定义 Trapezoid(梯形)类 class Trapezoid:public Shape public: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) /结构函数 virtual double area() const return 0.5*(top+bottom)*height; /定义虚函数 protected: double top,bottom,height; /上底、下底与高 ; /定义 Triangle(三角形) 类 class Triangle:public Shape public: Triangle(double w,double h):width(w),height(h) /结构函数 virtual double area() const return 0.5*width*height; /定义虚函数 protected: double width,height; /宽与高 ; int main() Circle circle(12.6); /建立 Circle 类对象 circle Square square(3.5); /建立 Square 类对象 square Rectangle rectangle(4.5,8.4); /建立 Rectangle 类对象 rectangle Trapezoid trapezoid(2.0,4.5,3.2); /建立 Trapezoid 类对象 trapezoid Triangle triangle(4.5,8.4); /建立 Triangle 类对象 Shape *pt5= /定义基类指针数组 pt, 使它每一个元素指向一个 派生类对象 double areas=0.0; /areas 为总面积 for(int i=0;iarea(); cout #include using namespace std; int main() double a,b,c,s,area; coutabc; if (a+b #include using namespace std; void input(double a,double b,double c) coutabc; void area(double a,double b,double c) double s,area; if (a+b #include using namespace std; int main() float a5; coutai; cout using namespace std; int main() float a5; int i; coutai; cout.setf(ios:fixed); cout.precision(2); for(i=0;i #include using namespace std; int main() for(int n=1;n #include using namespace std; /VC+ 6.0 要此行 /fun1 函数从键盘输入 20 个整数,分别存放在两个 磁盘文件中 void fun1() int a10; ofstream outfile1(“f1.dat“),outfile2(“f 2.dat“); /分别定义两个 文件流对象 if(!outfile1) /检查打开 f1.dat 是否成功 cerrai; outfile1ai; outfile2a; /磁盘文件 f2.dat 读入一个 整数 outfileai; /从 磁盘文件 f2.dat 读入 20 个 数放在数组 a 中 for(i=0;iaj+1) t=aj;aj=aj+1;aj+1=t; infile.close(); /关闭输入文件 f2.dat ofstream outfile(“f2.dat“,ios:out); / f2.dat 作为输出文件,文 件中原有内容删除 if(!outfile) cerr #include using namespace std; struct staff int num; char name20; int age; double pay; ; int main() staff staf7=2101,“Li“,34,1203,2 104,“Wang“,23,674.5,2108,“ Fun“,54,778, 3006,“Xue“,45,476.5,5101,“L ing“,39,656.6,staf1; fstream iofile(“staff.dat“,ios:in|ios: out|ios:binary); if(!iofile) cerrstaf1.numstaf1.nam estaf1.agestaf1.pay; 23 iofile.seekp(0,ios:end); iofile.write(char *) iofile.s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 佳木斯市向阳区2024-2025学年三年级数学第二学期期末质量跟踪监视模拟试题含解析
- 江苏省苏州昆山市、太仓市2025届中考冲刺预测卷(六)语文试题含解析
- 昆明幼儿师范高等专科学校《药用高分子材料》2023-2024学年第一学期期末试卷
- 华中科技大学《大学英语写作》2023-2024学年第一学期期末试卷
- 山西科技学院《城市地理学》2023-2024学年第二学期期末试卷
- 江西生物科技职业学院《生物教材分析与应用》2023-2024学年第二学期期末试卷
- 宁夏医科大学《城市设计1》2023-2024学年第二学期期末试卷
- 乌海职业技术学院《工程制图(Ⅱ)》2023-2024学年第二学期期末试卷
- 云南外事外语职业学院《英语国家社会与文化(1)》2023-2024学年第二学期期末试卷
- 上海师范大学天华学院《绿色建筑检测与评价》2023-2024学年第一学期期末试卷
- 街道零星劳务外包投标方案技术标
- 铸件外观缺陷图
- 冰箱温度监测登记表
- 基于核心素养下初中道德与法治大单元教学的实践探析
- 文化旅游融合发展详述
- 2023年10月江苏省宿迁市宿豫区部分乡镇公开招考23名返乡兴村新村干笔试历年高频考点试题含答案带详解
- 模板安装三检记录表
- 物业服务费用收支情况明细表
- 益阳万达广场项目总承包工程施工组织设计
- 材料采购计划监理意见
- 水利工程建设文明工地创建措施
评论
0/150
提交评论