1、C+面向对象程序设计源代码.txt 心是自己的,干嘛总被别人伤没有伞的孩子必须努力奔跑敷衍旳青春 总昰想太多 怨,只怨现实太现实为什么在一起要两个人的同意丶而分手只需要一个人第 1 章 程序设计与 C+概述/e1_7.cpp#include void main()int a,b,c;cinab; /输入两个整数c = max(a,b);couty?x:y);return z;/e1_8.cpp#include void main() float r; /定义浮点型变量 r,用于存放圆的半径coutr; /从键盘上输入圆的半径送给变量 rcoutfloat area(float r) /定义求半
2、径为 r 的圆的面积的函数 area return 3.14159f * r * r; void main()float r; /定义浮点型变量 r,用于存放圆的半径coutr; /从键盘上输入圆的半径送给变量 rcoutclass Circle /定义一个计算圆的面积的类 Circleprivate:float r; /定义成员数据变量 r,用于存放圆的半径public:Circle(float a) r = a; /定义构造函数,用于创建和初始化对象Circle( ) /定义析构函数,用于清理和撤销对象void SetRadius(float a) r = a; /定义成员函数,用于设置圆
3、的半径 rfloat GetRadius() return r; /定义成员函数,用于获取圆的半径 rfloat Area() return 3.14159f * r * r; /定义成员函数 Area,用于计算圆的面积;void main()float r; /定义浮点型变量 r,用于存放圆的半径coutr; /从键盘上输入圆的半径送给变量 rCircle c(r); /定义 Circle 类的对象 ccoutconst float PI = 3.1416void main()float r,c,s;cinr;c=2*PI*r; s=PI*r*r; coutvoid main()char c
4、1,c2;int d1,d2;coutc1c2;d1=int(c1); /将 c1 强制转换为整型值赋给 d1d2=int(c2); /将 c2 强制转换为整型值赋给 d2coutvoid main()coutvoid main()int num1,num2,max;coutnum1num2;max=num1;if(maxvoid main()int num1,num2;coutnum1num2;if(num1num2)coutvoid main() float score;coutscore;if(score=85)cout=75)cout=60)coutvoid main()int num
5、;cinnum;switch(num)case 1:coutvoid main()int sum=0,i;i=1;while(ivoid main()int m,n;for(m=1;mvoid main()int m,n;coutm;for(n=2;nm/2)coutvoid main()int m,n;coutvoid main()int x,y,z,t;coutxyz;if (xy)t=x;x=y;y=t; /交换 x,y 的值if(xz)t=z;z=x;x=t; /交换 x,z 的值if(yz)t=y;y=z;z=t; /交换 z,y 的值coutvoid main()int day,m
6、onth,year,sum,leap;coutyearmonthday;switch(month) /先计算某月以前月份的总天数case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:
7、sum=334;break;default: cout2) /如果是闰年且月份大于 2,总天数应该加一天sum+;coutvoid main()int a,b,m,n,t,r;coutmn;if(mvoid main()int i,j,k;for(i=1;i#include /使用控制符 setw,要包含头文件 iomanip.hvoid main()int i,a7;coutai;for(i=0;ivoid main() int x10,i,j;j=0; /计数器初值赋 0coutxi;for(i=0;i0) j+;coutvoid main()int a11; int i,n,t; /i
8、表示数组下标,n 表示遍历次数,t 用来交换数据 coutai;coutai+1) t=ai;ai=ai+1;ai+1=t;coutvoid main()int a23=1,5,9,2,6,8;int b32,i,j;coutvoid main()float score63,sv6,x;int i,j;coutx;scoreij=x; /输入每个学生的每门课成绩 for(i=0;i#include void main()char str20;int i,j;cout=0;i-)cout.put(stri);/e4_8.cpp#include #include void main()char s
9、tr120,str220,str350;cin.getline(str1,20);cin.getline(str2,20);if (strcmp(str1,str2)void main()float a33,sum=0,x;int i,j;coutx;aij=x;for(i=0;i#include void main()char str60;int i,j;cin.get(str,60);i=0;j=strlen(str)-1;while(stri=) i+; /寻找前面第一个不是空格的字符 while(strj=) j-; /寻找后面第一个不是空格的字符 while(ivoid main()
10、int i,j,a10,t,k;coutai;for(i=0;ivoid main()int x,y,z;cinxy;z=x+y;coutint sum(int a,int b) /被调用函数 sum 的定义int s;s=a+b;return s;void main()int x,y,z;int sum(int,int); /函数声明cinxy;z=sum(x,y); /函数调用coutvoid swap(int x,int y); /函数声明void main()int a=50,b=60;coutint add(int x, int n) /定义函数return(x+n);void ma
11、in()int add(int x, int n); /函数声明int i;int a10=9,8,7,6,5,4,3,2,1,0;for(i=0;ivoid swap(int *x,int *y); /函数声明,形参 x 和 y 为指针变量void main()int a=50,b=60;coutfloat average(float array10); /函数声明void main()float score10,aver;int i;coutscorei;coutint max_element(int array44); /函数声明void main()int p44=1,22,3,4,3
12、,4,6,18,6,5,2,9,0,6,1,34;coutmax) max=arrayij;return(max);/e5_7.cpp#include void print(int p); /函数声明void main()int score34=87,81,65,74,93,84,72,88,76,85,92,99;int num;coutnum;print(scorenum); /函数调用,二维数组列地址作为参数void print(int p) /函数定义int i;for(i=0;ivoid main()int i=2,j=3,k;k=i+j;int k=14;if(i=2) couti
13、nt s1,s2,s3;int vs( int a,int b,int c)int v;v=a*b*c;s1=a*b;s2=b*c;s3=a*c;return v;void main()int v,l,w,h;coutlwh;v=vs(l,w,h);coutint fun(int x,int y,int z) /函数定义int v;v=x*y*z;return v;void main()extern int w,h; /外部变量说明,以扩展变量 w、h 的作用范围int l=5;coutvoid main()auto int a,s=10,p=10;couta;if(a0)auto int s
14、,p;s=a+a;p=a*a;coutvoid main()int i;void f(); /函数声明 for(i=1;ifloat sum(float x,float y,float z);float average(float a,float b,float c);void main()float num1,num2,num3;coutnum1num2num3;coutlong fac(int n) long t; if(n=1)|(n=0)return 1; else t=n*fac(n-1); return t; void main() long fac(int n);int m;lo
15、ng y;coutm;if(m#include /使用控制符 setw,要包含头文件 iomanip.h#define MAX 100int fun( int m, int primeMAX) int i,j=0,k;for( k=2;k=k) primej+=k;return j;void main() int m,i,sum;int primeMAX;coutm;sum=fun(m,prime);cout#include void f(char s,char t) int i,s1;s1=strlen(s);for(i=0;iint max(int x,int y)int z;z = (x
16、y?x:y);return z;float max(float x,float y)float z;z = (xy?x:y);return z;void main()int num1,num2;float num3,num4;cinnum1num2num3num4;coutvoid main()int max(int a,int b,int c=0);int a,b,c;cinabc;couta) a=b;if (ca) a=c;return a;/e5_20.cpp#include template T max(T x,T y)return xy?x:y;void main()int num
17、1,num2;float num3,num4;cinnum1num2num3num4;couttemplate T2 max(T1 x,T2 y)return xy?x:y;void main()int num1,num2;float num3,num4;cinnum1num2num3num4;cout#define R 1void main()double c,r,s;coutc;#if Rr=3.14159*c*c;cout#define S(a,b) (ab)?(a):(b) /定义带参数的宏名 Svoid main() int x,y;cinxy;couty)?(x):(y)第 6 章
18、 指针/e6_1.cpp#include void main() int *p1,*p2,*p,a,b;cinab;p1=p2=if(avoid main()int *p1,*p2,n;n=3;p1= p2=coutswap(int *p1,int *p2)int *p;p=p1;p1=p2;p2=p;void main() int a,b;int *pointer_1,*pointer_2;coutab;pointer_1=pointer_2=if(avoid main()int *p,i,a10;p=a;for(i=0;iint sub_max(int b,int n)int temp,i
19、;temp=b0;for(i=1;iai;max=sub_max(pt,10);coutvoid main()int a34=0,1,2,3,4,5,6,7,8,9,10,11;int (*p)4;int i,j;p=a;for(i=0;ivoid main()int a33=1,2,3,4,5,6,7,8,9;int * pa3;int * p=a0;int i;pa0=a0;pa1=a1;pa2=a2;for(i=0;i#include void main()void sort(char *name,int n);void print(char *name,int n);char *nam
20、e=“CHINA“,“AMERICA“,“AUSTRALIA“,“FRANCE“,“GERMAN“;int n=5;sort(name,n);print(name,n);void sort(char *name,int n)char *pt;int i,j,k;for(i=0;i0) k=j;if(k!=i)pt=namei;namei=namek;namek=pt;void print(char *name,int n)int i;for(i=0;ivoid main()int i;char *day_name(int n); couti;if(i“7)?name0:namen);/e6_1
21、9.cpp#include int max(int a,int b)if(ab) return a;else return b;void main()int max(int a,int b);int (*pmax)(int ,int);int x,y,z;pmax=max;coutxy;z=pmax(x,y);coutvoid main()int num = 500;int ref = ref + 100;coutvoid swap(int x,int y)int t;t = x;x = y;y = t;void main()int a = 1,b = 2;coutvoid swap(int
22、* x,int * y)int t;t = *x;*x = *y;*y = t;void main()int a = 1,b = 2;coutvoid swap(int t = x;x = y;y = t;void main()int a = 1,b = 2;coutint *f=1;for(k=2;kvoid inverse(int a,int m,int n)int i,j,t;for(i=m-1,j=m+n-2;ivoid main()void del_ch(char *,char);char str80,*pt,ch;coutch;del_ch(pt,ch);cout#include
23、void main()char * ptr14,str420,temp20;int i,j;for(i=0;istri;cout0)strcpy(temp,ptr1j);strcpy(ptr1j,ptr1j+1);strcpy(ptr1j+1,temp);coutclass Date /定义日期类public:void SetDate(int y, int m, int d) /设置日期成员函数year = y;month = m;day = d;int IsLeapYear() /判定是否闰年成员函数return(year%4=0 void Print() /输出日期成员函数coutclas
24、s Triangle /定义三角形类public:void SetTriangle(int x, int y, int z); /设置三角形成员函数double GetArea(); /求三角形面积int GetPerimeter(); /求三角形周长void Print(); /输出三角形信息private:int a,b,c; /数据成员;/返回面积较大的三角形Triangle /Triangle.cpp#include #include “Triangle.h“void Triangle:SetTriangle(int x, int y, int z) /设置三角形成员函数a = x;b
25、 = y;c = z;double Triangle:GetArea() /求三角形面积double s;s = (a + b + c)/2.0;return sqrt( s*(s-a)*(s-b)*(s-c); int Triangle:GetPerimeter() /求三角形周长return (a + b + c); void Triangle:Print() /输出三角形信息coutt2.GetArea()return t1;elsereturn t2;/e7_2.cpp#include “Triangle.h“void main()Triangle t1; /定义三角形对象t1.SetTriangle(4,5,6);Triangle t2; /定义三角形对象t2.SetTriangle(7,8,9);Triangle max.Print();/e7_3.cpp#include class Date /定义日期类public:Date() /不带参数的构造函数year = 2007;month = 12;day = 31;