面向对象程序设计—第一
- 格式:pptx
- 大小:135.34 KB
- 文档页数:16
简述面向对象程序设计的三大基本特点面向对象程序设计(Object-Oriented Programming,简称OOP)是一种常用的编程范式,它具有三大基本特点:封装、继承和多态。
封装是面向对象程序设计的第一个基本特点。
它指的是将数据和对数据的操作封装在一个类中,通过类的接口来隐藏数据的实现细节。
封装使得数据和操作数据的方法被组织在一个单独的逻辑单元中,提高了代码的可维护性和可重用性。
通过封装,我们可以将数据和相关的操作绑定在一起,形成一个独立的实体,从而实现了数据的保护和安全性。
对外部用户来说,只需要通过类的公共接口来访问数据和操作,不需要关心内部的具体实现细节。
这种封装的特性可以有效地隐藏对象的内部信息,提高了代码的可读性和可理解性。
继承是面向对象程序设计的第二个基本特点。
继承是指一个类(称为子类或派生类)可以继承另一个类(称为父类或基类)的属性和方法。
通过继承,子类可以拥有父类的属性和方法,同时可以在此基础上进行扩展和定制。
继承可以有效地实现代码的重用,避免了重复编写相同的代码。
另外,继承还可以建立类之间的层次关系,使得代码的组织结构更加清晰和直观。
继承的特性使得面向对象程序设计具有了更强的灵活性,可以在不破坏原有代码的基础上进行功能扩展和修改。
多态是面向对象程序设计的第三个基本特点。
多态是指同一个方法在不同的对象上可以有不同的行为。
具体来说,是通过父类的引用指向子类的对象,以实现同一个方法在不同对象上表现出不同的行为。
多态可以提高代码的灵活性和可拓展性,使得程序更加易于扩展和维护。
通过多态,可以处理同一类对象的不同实现方式,提高了代码的可读性和可维护性。
多态还可以实现接口的统一,使得不同的对象可以以相同的方式调用方法。
面向对象程序设计的三大基本特点相互依赖、相互联系,共同构成了面向对象编程的核心思想和基本原则。
封装保护了对象的内部数据和操作,提供了对外的公共接口;继承实现了代码的重用和扩展,建立了类之间的层次关系;多态实现了同一方法在不同对象上的不同行为,提高了代码的灵活性和可拓展性。
第九章面向对象的程序设计面向对象的程序设计(Object Oriented Programming,简称OOP)与编程技术不同于标准的结构化程序设计。
在进行面向对象程序设计时,首先要考虑为实现某种目标而创建的具有某种功能且操作使用便捷的控件、对象和控件的使用参数及外观,以及为实现具体功能应选用的事件及数据环境并设计好相应的方法程序模块。
9.1 VFP中的对象9.1.1 面向对象的基本概念对象是由数据及可以施加在这些数据上的可执行操作所构成的统一体,是代码和数据的组合,它可以作为一个完整的、独立的单位模块来处理。
面向对象程序设计方法是一种以数据和信息为主线,将数据和处理相结合的方法。
那么什么是对象呢?对象就是客观世界中事物的抽象,是反映客观事物属性及行为特征的可运作实体。
在OOP中,将对象作为一个变量来处理,对象包括数据和用来处理这些数据的方法和工具。
对象是构成程序的基本单位和运行实体,是应用程序的组装模块。
一般来说,对象=属性+控件+事件+数据环境+方法程序,是一种模块的组合体。
控件是显示数据和执行操作的基本工具对象;属性是对象所具有的物理性质及其特性标识符;事件是对象所能识别和响应的某些行为和操作;数据环境是对象运行生存所依据的数据信息范围;方法程序是对象在事件触发时的行为和动作。
9.1.2 对象的属性、事件和方法对象(Object)在现实生活中是很常见的(如:一个人是一个对象,一台Pc即是一个对象。
从可视化编程的角度来看,对象是一个具有属性(数据)和方法(行为方式)的实体。
一个对象建立之后,其操作就通过与该对象有关的属性、事件和方法来描述。
1. 对象的属性属性(Property)是指对象的一项描述内容,用于描述对象的一个特性。
不同的对象具有不同的属性,而每个对象又都由若干属性来描述。
属性是对象的特征,是对象某一方面的行为参数,它描述了一个对象,描述了对象的状态或某一方面的行为功能,说明了对象可以完成的工作,但是还没有说明如何去完成任务。
第一章1-5、#include <iostream> using namespace std;int main(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program.";return 0;}1-6、#include <iostream> using namespace std;int main(){int a,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return 0;}1-7#include <iostream> using namespace std;int main(){int a,b,c;int f(int x,int y,int z); cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return 0;}int f(int x,int y,int z){int m;if (x<y) m=x;else m=y;if (z<m) m=z;return(m);} 1-8、#include <iostream>using namespace std;int main(){int a,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return 0;}1-9、#include <iostream>using namespace std;int main(){int a,b,c;int add(int x,int y);cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return 0;}int add(int x,int y){int z;z=x+y;return(z);}第二章2-3、#include <iostream>using namespace std;int main(){char c1='a',c2='b',c3='c',c4='\101',c5='\116'; cout<<c1<<c2<<c3<<'\n';cout<<"\t\b"<<c4<<'\t'<<c5<<'\n';return 0;}2-4、#include <iostream>using namespace std;int main(){char c1='C',c2='+',c3='+';cout<<"I say: \""<<c1<<c2<<c3<<'\"';cout<<"\t\t"<<"He says: \"C++ is veryinteresting!\""<< '\n';return 0;}2-7、#include <iostream>using namespace std;int main(){int i,j,m,n;i=8;j=10;m=++i+j++;n=(++i)+(++j)+m;cout<<i<<'\t'<<j<<'\t'<<m<<'\t'<<n<<endl; return 0;}2-8、#include <iostream>using namespace std;int main(){char c1='C', c2='h', c3='i', c4='n', c5='a';c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout<<"passwordis:"<<c1<<c2<<c3<<c4<<c5<<endl;return 0;}第三章3-2、#include <iostream>#include <iomanip>using namespace std;int main ( ){float h,r,l,s,sq,vq,vz;const float pi=3.1415926;cout<<"please enter r,h:";cin>>r>>h;l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout<<setiosflags(ios::fixed)<<setiosflags(ios:: right)<<setprecision(2);cout<<"l= "<<setw(10)<<l<<endl;cout<<"s= "<<setw(10)<<s<<endl;cout<<"sq="<<setw(10)<<sq<<endl;cout<<"vq="<<setw(10)<<vq<<endl;cout<<"vz="<<setw(10)<<vz<<endl;return 0;}3-3、#include <iostream>using namespace std;int main (){float c,f;cout<<"请输入一个华氏温度:";cin>>f;c=(5.0/9.0)*(f-32); //注意5和9要用实型表示,否则5/9值为0cout<<"摄氏温度为:"<<c<<endl;return 0;};3-4、#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<c2<<endl;return 0;}3-4-1、#include <iostream>using namespace std;int main ( ){char c1,c2;cout<<"请输入两个字符c1,c2:";c1=getchar(); //将输入的第一个字符赋给c1c2=getchar(); //将输入的第二个字符赋给c2cout<<"用putchar函数输出结果为:"; putchar(c1);putchar(44);putchar(c2);cout<<endl;cout<<"用cout语句输出结果为:";cout<<c1<<","<<c2<<endl;return 0;}3-5、#include <iostream>using namespace std;int main ( ){char c1,c2;int i1,i2; //定义为整型cout<<"请输入两个整数i1,i2:";cin>>i1>>i2;c1=i1;c2=i2;cout<<"按字符输出结果为:"<<c1<<" , "<<c2<<endl;return 0;}3-8、#include <iostream>using namespace std;int main ( ){ int a=3,b=4,c=5,x,y;cout<<(a+b>c && b==c)<<endl;cout<<(a||b+c && b-c)<<endl;cout<<(!(a>b) && !c||1)<<endl;cout<<(!(x=a) && (y=b) && 0)<<endl;cout<<(!(a+b)+c-1 && b+c/2)<<endl; return 0;}3-9-1、#include <iostream>using namespace std;int main ( ){int a,b,c;cout<<"please enter three integer numbers:";cin>>a>>b>>c;if(a<b)if(b<c)cout<<"max="<<c;elsecout<<"max="<<b;else if (a<c)cout<<"max="<<c;elsecout<<"max="<<a;cout<<endl;return 0;}3-9-2、#include <iostream>using namespace std;int main ( ){int a,b,c,temp,max ;cout<<"please enter three integer numbers:";cin>>a>>b>>c;temp=(a>b)?a:b; /* 将a和b中的大者存入temp中*/max=(temp>c)?temp:c; /* 将a和b中的大者与c比较,最大者存入max */cout<<"max="<<max<<endl;return 0;}3-10、#include <iostream>using namespace std;int main ( ){int x,y;cout<<"enter x:";cin>>x;if (x<1){y=x;cout<<"x="<<x<<", y=x="<<y;}else if (x<10) // 1≤x<10{y=2*x-1;cout<<"x="<<x<<", y=2*x-1="<<y;}else// x≥10{y=3*x-11;cout<<"x="<<x<<",y=3*x-11="<<y;}cout<<endl;return 0;}3-11、#include <iostream>using namespace std;int main (){float score;char grade;cout<<"please enter score of student:"; cin>>score;while (score>100||score<0){cout<<"data error,enter data again.";cin>>score;}switch(int(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;default:grade='E';}cout<<"score is "<<score<<", grade is "<<grade<<endl;return 0;}3-12、#include <iostream>using namespace std;int main (){long int num;intindiv,ten,hundred,thousand,ten_thousand,pla ce;/*分别代表个位,十位,百位,千位,万位和位数*/cout<<"enter an integer(0~99999):"; cin>>num;if (num>9999)place=5;else if (num>999)place=4;else if (num>99)place=3;else if (num>9)place=2;else place=1;cout<<"place="<<place<<endl;//计算各位数字ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1 000;hundred=(int)(num-ten_thousand*10000-tho usand*1000)/100;ten=(int)(num-ten_thousand*10000-thousan d*1000-hundred*100)/10;indiv=(int)(num-ten_thousand*10000-thousa nd*1000-hundred*100-ten*10);cout<<"original order:";switch(place){case5:cout<<ten_thousand<<","<<thousand<<","< <hundred<<","<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<ten _thousand<<endl;break;case4:cout<<thousand<<","<<hundred<<","<<ten <<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<thousand<<en dl;break;case3:cout<<hundred<<","<<ten<<","<<indiv<<en dl;cout<<"reverse order:";cout<<indiv<<ten<<hundred<<endl;break;case 2:cout<<ten<<","<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<ten<<endl;break;case 1:cout<<indiv<<endl;cout<<"reverse order:";cout<<indiv<<endl;break;}return 0;}3-13-1、#include <iostream>using namespace std;int main (){ long i; //i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; //利润为10万元时的奖金bon2=bon1+100000*0.075; //利润为20万元时的奖金bon4=bon2+100000*0.05; //利润为40万元时的奖金bon6=bon4+100000*0.03; //利润为60万元时的奖金bon10=bon6+400000*0.015; //利润为100万元时的奖金cout<<"enter i:";cin>>i;if (i<=100000)bonus=i*0.1;//利润在10万元以内按10%提成奖金else if (i<=200000)bonus=bon1+(i-100000)*0.075; //利润在10万元至20万时的奖金else if (i<=400000)bonus=bon2+(i-200000)*0.05; //利润在20万元至40万时的奖金else if (i<=600000)bonus=bon4+(i-400000)*0.03; //利润在40万元至60万时的奖金else if (i<=1000000)bonus=bon6+(i-600000)*0.015; //利润在60万元至100万时的奖金elsebonus=bon10+(i-1000000)*0.01; //利润在100万元以上时的奖金cout<<"bonus="<<bonus<<endl;return 0;}3-13-2、#include <iostream>using namespace std;int main (){long i;float bonus,bon1,bon2,bon4,bon6,bon10; int c;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;cout<<"enter i:";cin>>i;c=i/100000;if (c>10) c=10;switch(c){case 0: bonus=i*0.1; break;case 1: bonus=bon1+(i-100000)*0.075; break;case 2:case 3: bonus=bon2+(i-200000)*0.05;break;case 4:case 5: bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015; break;case 10: bonus=bon10+(i-1000000)*0.01;}cout<<"bonus="<<bonus<<endl;return 0;}3-14、#include <iostream>using namespace std;int main (){int t,a,b,c,d;cout<<"enter four numbers:";cin>>a>>b>>c>>d;cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl;if (a>b){t=a;a=b;b=t;}if (a>c){t=a; a=c; c=t;}if (a>d){t=a; a=d; d=t;}if (b>c){t=b; b=c; c=t;}if (b>d){t=b; b=d; d=t;}if (c>d){t=c; c=d; d=t;}cout<<"the sorted sequence:"<<endl;cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl; return 0;}3-15、#include <iostream>using namespace std;int main (){int p,r,n,m,temp;cout<<"please enter two positive integer numbers n,m:";cin>>n>>m;if (n<m){temp=n;n=m;m=temp; //把大数放在n中, 小数放在m中}p=n*m; //先将n和m的乘积保存在p中, 以便求最小公倍数时用while (m!=0) //求n和m 的最大公约数{r=n%m;n=m;m=r;}cout<<"HCF="<<n<<endl;cout<<"LCD="<<p/n<<endl; // p是原来两个整数的乘积return 0;}3-16、#include <iostream>using namespace std;int main (){char c;int letters=0,space=0,digit=0,other=0;cout<<"enter one line::"<<endl;while((c=getchar())!='\n'){if (c>='a' && c<='z'||c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<", other:"<<other<<endl;return 0;}3-17、#include <iostream>using namespace std;int main (){int a,n,i=1,sn=0,tn=0;cout<<"a,n=:";cin>>a>>n;while (i<=n){tn=tn+a; //赋值后的tn为i个a 组成数的值sn=sn+tn; //赋值后的sn为多项式前i项之和a=a*10;++i;}cout<<"a+aa+aaa+...="<<sn<<endl;return 0;}3-18、#include <iostream>using namespace std;int main (){float s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n; // 求n!s=s+t; // 将各项累加}cout<<"1!+2!+...+20!="<<s<<endl;return 0;}3-19、#include <iostream>using namespace std;int main (){int i,j,k,n;cout<<"narcissus numbers are:"<<endl;for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n == i*i*i + j*j*j + k*k*k)cout<<n<<" ";}cout<<endl;return 0;}3-20-1、#include <iostream>using namespace std;int main(){const int m=1000; // 定义寻找范围int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=m;a++) // a是2~1000之间的整数,检查它是否为完数{n=0; // n用来累计a的因子的个数s=a; // s用来存放尚未求出的因子之和,开始时等于afor (i=1;i<a;i++) // 检查i是否为a 的因子if (a%i==0) // 如果i是a的因子{n++; // n加1,表示新找到一个因子s=s-i; // s减去已找到的因子,s的新值是尚未求出的因子之和switch(n) // 将找到的因子赋给k1,...,k10{case 1:k1=i; break; // 找出的笫1个因子赋给k1case 2:k2=i; break; // 找出的笫2个因子赋给k2case 3:k3=i; break; // 找出的笫3个因子赋给k3case 4:k4=i; break; // 找出的笫4个因子赋给k4case 5:k5=i; break; // 找出的笫5个因子赋给k5case 6:k6=i; break; // 找出的笫6个因子赋给k6case 7:k7=i; break; // 找出的笫7个因子赋给k7case 8:k8=i; break; // 找出的笫8个因子赋给k8case 9:k9=i; break; // 找出的笫9个因子赋给k9case 10:k10=i; break; // 找出的笫10个因子赋给k10}}if (s==0) // s=0表示全部因子都已找到了{cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";if (n>1) cout<<k1<<","<<k2; // n>1表示a至少有2个因子if (n>2) cout<<","<<k3; // n>2表示至少有3个因子,故应再输出一个因子if (n>3) cout<<","<<k4; // n>3表示至少有4个因子,故应再输出一个因子if (n>4) cout<<","<<k5; // 以下类似if (n>5) cout<<","<<k6;if (n>6) cout<<","<<k7;if (n>7) cout<<","<<k8;if (n>8) cout<<","<<k9;if (n>9) cout<<","<<k10;cout<<endl<<endl;}}return 0;}3-20-2、#include <iostream>using namespace std;int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){cout<<m<<" is a完数"<<endl;cout<<"its factors are:";for (i=1;i<m;i++)if (m%i==0) cout<<i<<" ";cout<<endl;}}return 0;}3-20-3、#include <iostream>using namespace std;int main(){int k[11];int i,a,n,s;for (a=2;a<=1000;a++){n=0;s=a;for (i=1;i<a;i++)if ((a%i)==0){n++;s=s-i;k[n]=i; // 将找到的因子赋给k[1]┅k[10]}if (s==0){cout<<a<<" is a 完数"<<endl;cout<<"its factors are:";for (i=1;i<n;i++)cout<<k[i]<<" ";cout<<k[n]<<endl;}}return 0;}3-21、#include <iostream>using namespace std;int main(){int i,t,n=20;double a=2,b=1,s=0;for (i=1;i<=n;i++){s=s+a/b;t=a;a=a+b; // 将前一项分子与分母之和作为下一项的分子b=t; // 将前一项的分子作为下一项的分母}cout<<"sum="<<s<<endl;return 0;}3-22、#include <iostream>using namespace std;int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; // 第1天的桃子数是第2天桃子数加1后的2倍x2=x1;day--;}cout<<"total="<<x1<<endl;return 0;}3-23、#include <iostream>#include <cmath>using namespace std;int main(){float a,x0,x1;cout<<"enter a positive number:"; cin>>a; // 输入a的值x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);cout<<"The square root of "<<a<<" is "<<x1<<endl;return 0;}3-24、#include <iostream>using namespace std;int main(){int i,k;for (i=0;i<=3;i++) // 输出上面4行*号{for (k=0;k<=2*i;k++)cout<<"*"; // 输出*号cout<<endl; //输出完一行*号后换行}for (i=0;i<=2;i++) // 输出下面3行*号{for (k=0;k<=4-2*i;k++)cout<<"*"; // 输出*号cout<<endl; // 输出完一行*号后换行}return 0;}3-25、#include <iostream>using namespace std;int main(){char i,j,k; /* i是a的对手;j是b的对手;k是c的对手*/for (i='X';i<='Z';i++)for (j='X';j<='Z';j++)if (i!=j)for (k='X';k<='Z';k++)if (i!=k && j!=k)if (i!='X' && k!='X' && k!='Z')cout<<"A--"<<i<<"B--"<<j<<" C--"<<k<<endl;return 0;}第四章4-1、#include <iostream>using namespace std;int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;cin>>u>>v;h=hcf(u,v);cout<<"H.C.F="<<h<<endl;l=lcd(u,v,h);cout<<"L.C.D="<<l<<endl;return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}4-2、#include <iostream>#include <math.h>using namespace std;float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;disc=b*b-4*a*c;cout<<"root:"<<endl;if (disc>0){greater_than_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else if (disc==0){equal_to_zero(a,b);cout<<"x1="<<x1<<",x2="<<x2<<endl;}else{smaller_than_zero(a,b);cout<<"x1="<<p<<"+"<<q<<"i"<<endl;cout<<"x2="<<p<<"-"<<q<<"i"<<endl;}return 0;}void greater_than_zero(float a,float b) /* 定义一个函数,用来求disc>0时方程的根*/ {x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b) /* 定义一个函数,用来求disc=0时方程的根*/ {x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b) /* 定义一个函数,用来求disc<0时方程的根*/ {p=-b/(2*a);q=sqrt(-disc)/(2*a);}4-3、#include <iostream>using namespace std;int main(){int prime(int); /* 函数原型声明*/int n;cout<<"input an integer:";cin>>n;if (prime(n))cout<<n<<" is a prime."<<endl;elsecout<<n<<" is not a prime."<<endl; return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}4-4、#include <iostream>using namespace std;int main(){int fac(int);int a,b,c,sum=0;cout<<"enter a,b,c:";cin>>a>>b>>c;sum=sum+fac(a)+fac(b)+fac(c);cout<<a<<"!+"<<b<<"!+"<<c<<"!="<<sum<<e ndl;return 0;}int fac(int n){int f=1;for (int i=1;i<=n;i++)f=f*i;return f;}4-5、#include <iostream>#include <cmath>using namespace std;int main(){double e(double);double x,sinh;cout<<"enter x:";cin>>x;sinh=(e(x)+e(-x))/2;cout<<"sinh("<<x<<")="<<sinh<<endl; return 0;}double e(double x){return exp(x);}4-6、#include <iostream>#include <cmath>using namespace std;int main(){doublesolut(double ,double ,double ,double ); double a,b,c,d;cout<<"input a,b,c,d:";cin>>a>>b>>c>>d;cout<<"x="<<solut(a,b,c,d)<<endl;return 0;}double solut(double a,double b,double c,double d){double x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}4-7、#include <iostream>#include <cmath>using namespace std;int main(){void godbaha(int);int n;cout<<"input n:";cin>>n;godbaha(n);return 0;}void godbaha(int n) {int prime(int);int a,b;for(a=3;a<=n/2;a=a+2){if(prime(a)){b=n-a;if (prime(b))cout<<n<<"="<<a<<"+"<<b<<endl;}}}int prime(int m){int i,k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0) break;if (i>k) return 1;else return 0;}4-8、#include <iostream>using namespace std;int main(){int x,n;float p(int,int);cout<<"input n & x:";cin>>n>>x;cout<<"n="<<n<<",x="<<x<<endl;;cout<<"P"<<n<<"(x)="<<p(n,x)<<endl; return 0;}float p(int n,int x){if (n==0)return(1);else if (n==1)return(x);elsereturn(((2*n-1)*x*p((n-1),x)-(n-1)*p((n-2),x))/ n);}4-9、#include <iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"input the number of diskes:"; cin>>m;cout<<"The steps of moving "<<m<<" disks:"<<endl;hanoi(m,'A','B','C');return 0;}void hanoi(int n,char one,char two,char three) //将n个盘从one座借助two座,移到three座{void move(char x,char y);if(n==1) move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);}}void move(char x,char y){cout<<x<<"-->"<<y<<endl;}4-10、#include <iostream>using namespace std;int main(){void convert(int n);int number;cout<<"input an integer:";cin>>number;cout<<"output:"<<endl;if (number<0){cout<<"-";number=-number;}convert(number);cout<<endl;return 0;}void convert(int n){int i;char c;if ((i=n/10)!=0)convert(i);c=n%10+'0';cout<<" "<<c;}4-11、#include <iostream>using namespace std;int main(){int f(int);int n,s;cout<<"input the number n:";cin>>n;s=f(n);cout<<"The result is "<<s<<endl;return 0;}int f(int n){;if (n==1)return 1;elsereturn (n*n+f(n-1));}4-12、#include <iostream>#include <cmath>using namespace std;#define S(a,b,c) (a+b+c)/2#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c) -c))int main(){float a,b,c;cout<<"input a,b,c:";cin>>a>>b>>c;if (a+b>c && a+c>b && b+c>a)cout<<"area="<<AREA(a,b,c)<<endl; elsecout<<"It is not a triangle!"<<endl; return 0;}4-14、#include <iostream>using namespace std;//#define LETTER 1int main(){char c;cin>>c;#if LETTERif(c>='a' && c<='z')c=c-32;#elseif(c>='A' && c<='Z')c=c+32;#endifcout<<c<<endl;return 0;}4-15、#include <iostream>using namespace std;#define CHANGE 1int main(){char ch[40];cout<<"input text:"<<endl;;gets(ch);#if (CHANGE){for (int i=0;i<40;i++){if (ch[i]!='\0')if (ch[i]>='a'&& ch[i]<'z'||ch[i]>'A'&& ch[i]<'Z')ch[i]+=1;else if (ch[i]=='z'||ch[i]=='Z')ch[i]-=25;}}#endifcout<<"output:"<<endl<<ch<<endl;return 0;}4-16-1、#include <iostream>using namespace std;int a;int main(){extern int power(int);int b=3,c,d,m;cout<<"enter an integer a and its power m:"<<endl;cin>>a>>m;c=a*b;cout<<a<<"*"<<b<<"="<<c<<endl;d=power(m);cout<<a<<"**"<<m<<"="<<d<<endl; return 0;}4-16-2、extern int a;int power(int n){int i,y=1;for(i=1;i<=n;i++)y*=a;return y;}第五章5-1、#include <iostream>#include <iomanip>using namespace std;#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; }cout<<endl;for (i=1,n=0;i<=100;i++){if (a[i]!=0){cout<<setw(5)<<a[i]<<" ";n++;}if(n==10){cout<<endl;n=0;}}cout<<endl;return 0;}5-2、#include <iostream>using namespace std;//#include <math.h>int main(){int i,j,min,temp,a[11];cout<<"enter data:"<<endl;for (i=1;i<=10;i++){cout<<"a["<<i<<"]=";cin>>a[i]; //输入10个数}cout<<endl<<"The original numbers:"<<endl;;for (i=1;i<=10;i++)cout<<a[i]<<" "; // 输出这10个数cout<<endl;;for (i=1;i<=9;i++) //以下8行是对10个数排序{min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i]; //以下3行将a[i+1]~a[10]中最小者与a[i] 对换a[i]=a[min];a[min]=temp;}cout<<endl<<"The sorted numbers:"<<endl;for (i=1;i<=10;i++) // 输出已排好序的10个数cout<<a[i]<<" ";cout<<endl;return 0;}5-3、#include <iostream>using namespace std;int main(){int a[3][3],sum=0;int i,j;cout<<"enter data:"<<endl;;for (i=0;i<3;i++)for (j=0;j<3;j++)cin>>a[i][j];for (i=0;i<3;i++)sum=sum+a[i][i];cout<<"sum="<<sum<<endl;return 0;}5-4、#include <iostream>using namespace std;int main(){int a[11]={1,4,6,9,13,16,19,28,40,100};int num,i,j;cout<<"array a:"<<endl;for (i=0;i<10;i++)cout<<a[i]<<" ";cout<<endl;;cout<<"insert data:";cin>>num;if (num>a[9])a[10]=num;else{for (i=0;i<10;i++){if (a[i]>num){for (j=9;j>=i;j--)a[j+1]=a[j];a[i]=num;break;}}}cout<<"Now, array a:"<<endl;for (i=0;i<11;i++)cout<<a[i]<<" ";cout<<endl;return 0;}5-5、#include <iostream>using namespace std;int main(){ const int n=5;int a[n],i,temp;cout<<"enter array a:"<<endl;for (i=0;i<n;i++)cin>>a[i];cout<<"array a:"<<endl;for (i=0;i<n;i++)cout<<a[i]<<" ";for (i=0;i<n/2;i++) //循环的作用是将对称的元素的值互换{ temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}cout<<endl<<"Now,array a:"<<endl;for (i=0;i<n;i++)。
面向对象程序设计教程(C++语言描述)题解与课程设计指导第1章面向对象程序设计概论一、名词解释抽象封装消息【问题解答】面向对象方法中的抽象是指对具体问题(对象)进行概括,抽出一类对象的公共性质并加以描述的过程。
面向对象方法中的封装就是把抽象出来的对象的属性和行为结合成一个独立的单位,并尽可能隐蔽对象的内部细节.消息是面向对象程序设计用来描述对象之间通信的机制。
一个消息就是一个对象要求另一个对象实施某种操作的一个请求。
二、填空题(1)目前有面向过程的结构化程序设计方法和面向对象的程序设计方法两种重要的程序设计方法。
(2) 结构化程序设计方法中的模块由顺序、选择和循环3种基本结构组成。
(3)在结构化程序设计方法中,程序可表示为程序=数据结构+算法;而面向对象的程序设计方法,程序可表示为程序=对象+消息.(4)结构化程序设计方法中的基本模块是过程;而面向对象程序设计方法中的基本模块是类。
(5)面向对象程序设计方法具有抽象性、封装性、继承性和多态性等特点。
三、选择题(至少选一个,可以多选)(1) 面向对象程序设计着重于( B )的设计。
A。
对象B. 类C。
算法D. 数据(2)面向对象程序设计中,把对象的属性和行为组织在同一个模块内的机制叫做( C )。
A. 抽象B. 继承C. 封装D. 多态(3) 在面向对象程序设计中,类通过( D )与外界发生关系。
A. 对象B. 类 C。
消息 D. 接口(4)面向对象程序设计中,对象与对象之间的通信机制是( C )。
A。
对象 B. 类 C。
消息 D. 接口(5)关于C++与C语言的关系的描述中,( D )是错误的。
A. C语言是C++的一个子集 B。
C语言与C++是兼容的C。
C++对C语言进行了一些改进 D。
C++和C语言都是面向对象的【结果分析】C语言是面向过程的。
C++语言是一种经过改进的更为优化的C语言,是一种混合型语言,既面向过程也面向对象。
(6)面向对象的程序设计将数据结构与( A )放在一起,作为一个相互依存、不可分割的整体来处理。
C++面向对象程序设计课后答案(1-4章)第一章:面向对象程序设计概述[1_1]什么是面向对象程序设计?面向对象程序设计是一种新型的程序设计范型。
这种范型的主要特征是:程序=对象+消息。
面向对象程序的基本元素是对象,面向对象程序的主要结构特点是:第一:程序一般由类的定义和类的使用两部分组成,在主程序中定义各对象并规定它们之间传递消息的规律。
第二:程序中的一切操作都是通过向对象发送消息来实现的,对象接受到消息后,启动有关方法完成相应的操作。
面向对象程序设计方法模拟人类习惯的解题方法,代表了计算机程序设计新颖的思维方式。
这种方法的提出是软件开发方法的一场革命,是目前解决软件开发面临困难的最有希望、最有前途的方法之一。
[1_2]什么是类?什么是对象?对象与类的关系是什么?在面向对象程序设计中,对象是描述其属性的数据以及对这些数据施加的一组操作封装在一起构成的统一体。
对象可以认为是:数据+操作在面向对象程序设计中,类就是具有相同的数据和相同的操作的一组对象的集合,也就是说,类是对具有相同数据结构和相同操作的一类对象的描述。
类和对象之间的关系是抽象和具体的关系。
类是多个对象进行综合抽象的结果,一个对象是类的一个实例。
在面向对象程序设计中,总是先声明类,再由类生成对象。
类是建立对象的“摸板”,按照这个摸板所建立的一个个具体的对象,就是类的实际例子,通常称为实例。
[1_3]现实世界中的对象有哪些特征?请举例说明。
对象是现实世界中的一个实体,其具有以下一些特征:(1)每一个对象必须有一个名字以区别于其他对象。
(2)需要用属性来描述它的某些特性。
(3)有一组操作,每一个操作决定了对象的一种行为。
(4)对象的操作可以分为两类:一类是自身所承受的操作,一类是施加于其他对象的操作。
例如:雇员刘名是一个对象对象名:刘名对象的属性:年龄:36 生日:1966.10.1 工资:2000 部门:人事部对象的操作:吃饭开车[1_4]什么是消息?消息具有什么性质?在面向对象程序设计中,一个对象向另一个对象发出的请求被称为“消息”。
面向对象程序设计C++ 第一套试卷一、选择题(共32分,每小题2分)1.按照C++标识符的规则,下面( B )是c++的标识符。
A.8_afrB.a_ideC.t-axyD._3?d2.C++中声明常量的关键字是( A )A.constB.externC.publicD.enum3.建立( C )的作用是为变量另起一个名字。
A.指针B.枚举C.引用D.结构4.在( D )情况下适宜采用内联函数。
A.函数体含有循环语句B.函数代码多,频繁调用C.函数体含有递归语句D. 函数代码少,频繁调用5.关于new运算符的下列描述中,错误的是( D )A.它可以用来动态创建对象和对象数组B.使用它创建的对象或对象数组可以使用运算符delete删除C.使用它创建对象时要调用构造函数D.使用它创建对象数组时必须指定初始值6.类成员的访问权限中,( C )只能被本类的成员函数和其友元函数访问。
A.shareB.publicC.privateD.protected7.在下面有关构造函数的描述中,正确的是( B )A.构造函数可以带有返回值B.构造函数名字与类名完全相同C.构造函数必须带参数D.构造函数必须定义,不能默认8.有如下类声明:class A{ private: int x;public: A(int n){ x=n;} };class B: public A{ private: int y;public: B(int a,int b); };在构造函数B的下列定义中,正确的是( C )。
A. B::B(int a,int b): x(a),y(b){ }B. B::B(int a,int b): x(a),B(b){ }C. B::B(int a,int b): A(a),y(b){ }D. B::B(int a,int b): A(a),B(b){ }9.设置虚基类的目的是( B )A.简化程序B.消除二义性C.提高运行效率D.减少目标代码10.当一个派生类私有继承一个基类时,基类中的所有公有成员和保护成员成为派生类的( C )A.public成员B. protected成员C.private成员D.友元11. 应在下列程序划线处填入的正确语句是( D )#include<iostream>using namespace std;class Base{ public: void fun(){cout<<"Base::fun"<<endl;}};class Derived:public Base{ public: void fun() {cout<<"Derived::fun"<<endl;}};void main(){ Derived obj;_____________ //调用基类的fun函数}A.obj.fun(); B. Base.fun(); C. Base->fun(); D. obj.Base::fun();12. 假定一个类的构造函数为B(int x,int y){a=x;b=a*y;},则执行B obj(3,5); 语句后,obj.a和obj.b的值分别为( C )A.3和5 B.5和3 C.3和15 D.20和513.模板的使用是为了( A )A.提高代码的可重用性B.提高代码的运行效率C.加强类的封装性D.实现多态性14.在下列选项中( A )不是ostream类的对象。