为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

面向对象离线离线作业

2017-09-20 50页 doc 125KB 34阅读

用户头像

is_589748

暂无简介

举报
面向对象离线离线作业面向对象离线离线作业 浙江大学远程教育学院 《面向对象程序设计》课程作业 713005012007 姓名: 曹明敏 学 号: 年级: 2013 春 学习中心: 杭州 ————————————————————————————— 第2章 【2.3】 测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是 否有效。 //this is a strange /*way to do a comment*/ 此注释有效,单行注释中可以嵌套/*…….*/方式的注释。 【2.4】 以下这个简短的C++程序不可能编译...
面向对象离线离线作业
面向对象离线离线作业 浙江大学远程教育学院 《面向对象程序》课程作业 713005012007 姓名: 曹明敏 学 号: : 2013 春 学习中心: 杭州 ————————————————————————————— 第2章 【2.3】 测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是 否有效。 //this is a strange /*way to do a comment*/ 此注释有效,单行注释中可以嵌套/*…….*/方式的注释。 【2.4】 以下这个简短的C++程序不可能编译通过,为什么, #include using namespace std; int main() {int a,b,c; cout<<"Enter two numbers:"; cin>>a>>b; c=sum(a,b); cout<<"sum is:"<函数
的定义在后,而对它的调用在前时,必须将该函数的原型写在调用语句之前,而在本程序中缺少函数原型语句。在语句“using namespace std;”后加上函数原型语句“sum(int a,int b );” 就可通过编译。 【2.5】 回答问题。 (1) 以下两个函数原型是否等价: float fun(int a,float b,char *c); float fun(int,float,char * ); 这两个函数原型是等价的,因为函数原型中的参数名可以缺省。 (2) 以下两个函数的第一行是否等价: float fun(int a,float b,char * c); float fun(int,float,char * ); 两个函数的第一行是不等价的,因为这个函数的第一行中必须包 含参数名。 【2.6】 下列语句中错误的是( D )。 A(int *p=new int(10); B(int *p=new int[10]; C(int *p=new int; D(int *p=new int[40](0); 【2.7】 假设已经有定义“const char * const name="chen";”下面的语句中 正确的是( D )。 A. name[3]='a'; B. name="lin"; C. name=new char[5]; D. cout< using namespace std; int i=15; int main() { int i; i=100; ::i=i+1; cout<<::i< using namespace std; void f(int &m,int n) { int temp; temp=m; m=n; n=temp; } int main() { int a=5,b=10; f(a,b); cout< using namespace std; int &f(int &i) { i+=10; return i; } int main() { int k=0; int &m=f(k); cout< using namespace std; # define A 2+4 # define B A*3 int main() { cout< using namespace std; const A=2+4; const B=A*3; int main() { cout< using namespace std; int main() { int *p=new int[20]; *p=1; *(p+1)=1; cout<<*p<<"\t"<<*(p+1)<<"\t"; p=p+2; for (int i=3;i<=20;i++) { *p=*(p-1) + *(p-2); cout <<*p<<"\t"; if(i%5==0) cout<标准
库函数sqrt())。 # include # include using namespace std; double sroot(int i) { return sqrt(i); } double sroot(long l) { return sqrt(l); } double sroot(double d) { return sqrt(d); } int main() { int i=12; long l=1234; double d=12.34; cout<<"i 的二次方根是:"< using namespace std; int main() { int i,j,sum=0; for(i=0;i<=20;i++) for(j=0;j<=50;j++) if(100-5*i-2*j>=0) { sum++; cout<<100-5*i-2*j<<"\t"< using namespace std; int main() { void change (int &,int &); int a,b; cin>>a>>b; if(a>b)change(a,b); cout< using namespace std; class B{ public: B(){} B(int i,int j) { x=i; y=j; } void printb() {cout< using namespace std; class A{ public: void set(int i,int j) { x=i; y=j; } int get_y() {return y; } private: int x,y; }; class box{ public: void set(int l,int w,int s,int p) { length=1; width=w; label.set(s,p); } int get_area() {return length*width; } private: int length,width; A label; }; int main() { box b; b.set(4,6,1,20); cout< using namespace std; class Sample{ public: Sample(int i,int j) { x=i; y=j; } void disp() {cout<<"disp1"< using namespace std; class R{ public: R(int r1,int r2) {R1=r1; R2=r2; } void print(); void print() const; private: int R1,R2; }; void R::print() {cout< using namespace std; class toy { public: toy(int q,int p) {quan=q; price=p; } int get_quan() {return quan; } int get_price() { return price; } private: int quan,price; }; int main() { toy op[3][2]={ toy(10,20),toy(30,48), toy(50,68),toy(70,80), toy(90,16),toy(11,120), }; for(int i=0;i<3;i++) {cout< using namespace std; class example { public: example(int n) { i=n; cout<<"Constructing\n"; } ~example() { cout<<"Destructing\n"; } int get_i() { return i; } private: int i; }; int sqr_it(example o) {return o.get_i()*o.get_i(); } int main() { example x(10); cout< using namespace std; class aClass { public: aClass() {total++; } ~aClass() {total--; } int gettotal() { return total; } private: static int total; }; int aClass::total=0; int main() {aClass o1,o2,o3; cout< using namespace std; class test { public: test(); ~test(){}; private: int i; }; test::test() { i=25; for(int ctr=0;ctr<10;ctr++) { cout<<"Counting at"< using namespace std; class A{ int a,b; public: A() {a=0; b=0; cout<<"Default constructor called.\n"; } A(int i,int j) {a=i; b=j; cout<<"Constructor: a="< using namespace std; class Test{ private: int val; public: Test() { cout<<"default."< using namespace std; class N{ private: int A; static int B; public: N(int a) {A=a; B+=a; } static void f1(N m); }; void N::f1(N m) {cout<<"A="<x; y=m->y; } void fun(M m1,M*m2) {m1.setxy(12,15); m2->setxy(22,25); } int main() {M p(5,7),q; q.copy(&p); fun(p,&q); p.print(); q.print(); return 0; } 运行结果: 5,7 22,25 【3.26】 写出下面程序的运行结果。 #include using namespace std; class M{ int A; static int B; public: M(int a) {A=a; B+=a; cout<<"Constructing"< using namespace std; class Point{ public: int x,y; private: Piont() {x=1;y=2; } }; int main() {Point cpoint; cpoint.x=2; return 0; } 构造函数Point是私有的,语句“Point cpoint;”执行时出现错误。错 误的原因是:类Point的构造函数是私有函数,创建对象cpoint时不能 调用它。 【3.30】 下面是一个计算器类的定义,请完成该类成员函数的实现。 class counter{ public: counter(int number); void increment(); //给原值加1 void decrement(); //给原值减1 int getvalue(); //取得计数器值 int print(); //显示计数 private: int value; }; 程序如下: class counter { public: counter(int number); void inctement(); void decrement(); int getvalue(); int print(); private: int value; }; counter::counter(int number) { value=number; } void counter::inctement() { value++; } void counter::decrement() { value--; } int counter::getvalue() { return value; } int counter::print() { cout<<"value is"< using namespace std; class Date{ public: void printDate(); //显示日期 void setDay(int d); //设置日的值 void setMonth(int m); //设置月的值 void setYear(int y); //设置年的值 private: int day,month,year; }; int main() { Date testDay; testDay.setDay(5); testDay.setMonth(10); testDay.setYear(2003); testDay.printDate(); return 0; } 成员函数程序如下: void Date::printDate() { cout<<"\nDate is"< using namespace std; class cylinder { public: cylinder(double a,double b); void vol(); private: double r,h; double volume; }; cylinder::cylinder(double a,double b) { r=a;h=b; volume=3.141592*r*r*h; } void cylinder::vol() { cout<<"volume is:"< using namespace std; class book { public: book(int a,int b) { qu=a; price=b; } void show_money() { cout< using namespace std; class book { public: book(int a,int b) { qu=a; price=b; } void show_money() { cout<show_money();p--; } return 0; } 运行结果: 250 160 90 40 10 【3.35】 构建一个类 Stock,含字符数组stockcode[]及整型数据成员quan、 双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、 p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成 员stockcode,第2和第3个参数分别赋给quan、price。未设置第2 和第3个参数时,quan的值为1000,price的值为8.98。成员函数 print没有形参,需使用this指针,显示对象数据成员的内容。假设 类Stock第1个对象的三个参数分别为:“600001”,3000和5.67,第 2个对象的第1个数据成员的值是“600001”,第2和第3个数据成员 的值取默认值。要求编写程序分别显示这两个对象数据成员的值。 #include using namespace std; const int SIZE=80; class Stock { public: Stock() { strcpy(stockcode," "); } Stock(char code[], int q=1000,double p=8.98) { strcpy(stockcode,code); quan=q; price=p; } void print(void) { cout<stockcode; cout<<" "<quan<<" "<price< # include using namespace std; class Student { public: Student(int n,string na, double d) { no=n; deg=d; name=na; sum+=d; num++; } static double avg() { return sum/num; } static int total() { return num; } void disp() { cout< using namespace std; class Main{ protected: char *mainfood; public: Main(char *name) { mainfood=name; } }; class Sub{ protected: char *subfood; public: Sub(char *name) { subfood=name; } }; class Menu:public Main,public Sub{ public: Menu(char *m,char *s):Main(m),Sub(s) {} void show(); }; void Menu::show() { cout<<"主食="< using namespace std; class A{ private: int a; public: A() { a=0; } A(int i) { a=i; } void Print() { cout< using namespace std; class B1{ int b1; public: B1(int i) {b1=i; cout<<"constructor B1."< using namespace std; class A{ public: A(int i,int j) {x=i; y=j; } int sum() { return x+y; } private: int x,y; }; class B:public A{ public: B(int i,int j,int k,int l); int sum() { return w+h; } private: int w,h; }; B::B(int i,int j,int k,int l):A(i,j) { w=k; h=l; } void f(A& s) { cout< using namespace std; class A{ int a,b; public: A(int i,int j) {a=i;b=j; } void Move(int x,int y) { a+=x; b+=y; } void Show() { cout<<"("< using namespace std; class base1{ public: base1() { cout<<"class base1"< using namespace std; class Time { public: Time(int h,int m,int s) { hours=h; minutes=m; seconds=s; } void display() { cout<<"出生时间:"< using namespace std; class person { public: void input() { cout<<"编号:";cin>>no; cout<<"姓名:";cin>>name; } void disp() { cout<<"编号:"<>depart; cout<<"成绩:"; cin>>degree; } void disp() { person::disp(); cout<<"班号:"<>prof; cout<<"部门:"; cin>>depart; } void disp() { person::disp(); cout<<"职称:"< 【5.9】 关于虚函数,正确的描述是( A )。 A(构造函数不能是虚函数 B(析构函数不能是虚函数 C(虚函数可以是友元函数 D(虚函数可以是静态成员函数 【5.10】 派生类中虚函数原型的( D )。 A(函数类型可以与基类中虚函数的原型不同 B(参数个数可以与基类中虚函数的原型不同 C(参数类型可以与基类中虚函数的原型不同 D(以上都不对 【5.11】 如果在基类中将show声明为不带返回值的纯虚函数,正确的写法是 ( C )。 A. virtual show()=0; B. virtual void show(); C. virtual void show()=0; D. void show()=0 virtual; 【5.12】下列关于纯虚函数与抽象类的描述中,错误的是( C )。 A. 纯虚函数是一种特殊的函数,它允许没有具体的实现 B. 抽象类是指具有纯虚函数的类 C. 一个基类的说明中有纯虚函数,该基类的派生类一定不再是抽象类 D(抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出 【5.13】 下面的程序段中虚函数被重新定义的方法正确吗,为什么, class base{ public: virtual int f(int a)=0; . . . }; class derived: public base{ public: int f(int a,int b) { return a*b; } . . . }; 不正确,因为虚函数在派生类中重新定义是,其函数原型,包括函数类型、函数名、参数个数与参数类型的顺序,都必须与基类中的原型完全相同,而本程序段中虚函数在派生类中重新定义时,参数个数与基类中的原型不相同。 【5.14】 写出下列程序的运行结果。 #include using namespace std; class A{ public: A(int i): x(i) {} A() { x=0; } friend A operator ++(A a); friend A operator --(A &a); void print(); private: int x; }; A operator++(A a) { ++a.x; return a; } A operator--(A &a) { --a.x; return a; } void A::print() { cout< using namespace std; class Words{ public: Words(char *s) { str=new char[strlen(s)+1]; strcpy(str,s); len=strlen(s); } void disp(); char operator[](int n); //定义下标运算符"[]"重载函数 private: int len; char *str; }; char Words::operator [](int n) { if(n<0||n>len-1) //数组的边界检查 { cout<<"数组下标超界!\n"; return ' '; } else return *(str+n); } void Words::disp() { cout< using namespace std; class Length{ int meter; public: Length(int m) { meter=m; } operator double() { return (1.0*meter/1000); } }; int main() { Length a(1500); double m=float(a); cout<<"m="< using namespace std; #include const int row=2; const int col=3; class array { public: array(); array (int a,int b,int c,int d, int e, int f); void get_array(); void display(); array operator+(array &X); array operator-(array &X); private: int var[row][col]; }; array::array() { for(int i=0;i>var[i][j]; } void array::display() { for(int i=0;i #include using namespace std; const int row=2; const int col=3; class array { public: array(); array (int a,int b,int c,int d, int e, int f); void get_array(); void display(); friend array operator+(array &X,array &Y); friend array operator-(array &X,array &Y); private: int var[row][col]; }; array::array() { for(int i=0;i>var[i][j]; } void array::display() { for(int i=0;i格式
“(real,imag)” 输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2 和6.5,则调用print函数输出格式为:(4.2,6.5); (4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输 出。 #include using namespace std; class complex { public: complex(double r=0,double i=0); friend complex operator+(const complex c1,const complex c2); void print(); privat: double real,imag; }; complex::complex(double r,double i) { real=r; imag=i; } complex operator+(const complex c1,const complex c2) { complex temp; temp.real=c1,real+c2.real; temp.imag=c1,imag+c2.imag; return temp; } void complex::print() { complex c1(2.5,3.7),c2(4.2,6.5); complex c; c=c1+c2; c.p-rint(); return 0; } 运行结果: (6.7,10.2)
/
本文档为【面向对象离线离线作业】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
相关资料
热门搜索
你可能还喜欢
最新资料 资料动态 专题动态

历史搜索

    清空历史搜索