奥鹏作业答案-谋学网-专业的奥鹏在线作业答案辅导网【官网】

 找回密码
 会员注册

微信登录,扫一扫

手机号码,快捷登录

VIP会员,3年作业免费下 !奥鹏作业,奥鹏毕业论文检测新手作业下载教程,充值问题没有找到答案,请在此处留言!
2022年5月最新全国统考资料投诉建议,加盟合作!点击这里给我发消息 点击这里给我发消息
奥鹏课程积分软件(2021年最新)
查看: 1464|回复: 0

[谋学网首发] 18春浙江大学远程教育学院《面向对象程序设计》课程作业

[复制链接]
发表于 2018-5-26 13:34:34 | 显示全部楼层 |阅读模式
谋学网
浙江大学远程教育学院
《面向对象程序设计》课程作业

姓名:                学    号:       
年级:                学习中心:       
—————————————————————————————
第2章
【2.3】 测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是否有效。
//this is a strange /*way to do a comment*/
【2.4】 以下这个简短的C++程序不可能编译通过,为什么?
        #include<iostream>
using namespace std;
int main()
{int a,b,c;
         cout<<"Enter two numbers:";
         cin>>a>>b;
         c=sum(a,b);
         cout<<"sum is:"<<c;
         return 0;
}
sum(int a,int b)
{ return a+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】 下列语句中错误的是(  )。
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";”下面的语句中正确的是(   )。
A. name[3]='a';               B. name="lin";
C. name=new char[5];         D. cout<<name[3];
【2.8】 假设已经有定义“char * const name="chen";”下面的语句中正确的是(   )。
A. name[3]='q';              B. name="lin";
C. name=new char[5];         D. name=new char('q');
【2.9】 假设已经有定义“const char * name="chen";”下面的语句中错误的是(   )。
A. name[3]='q';              B. name="lin";
C. name=new char[5];         D. name=new char('q');
【2.10】重载函数在调用时选择的依据中,(   )是错误的。
        A.函数名字                 B.函数的返回类型
        C.参数个数                 D.参数的类型
【2.11】 在(   )情况下适宜采用内联函数。
        A.函数代码小,频繁调用     B.函数代码多,频繁调用
        C.函数体含有递归语句       D. 函数体含有循环语句
【2.12】 下列描述中,(   )是错误的。
        A. 内联函数主要解决程序的运行效率问题
        B. 内联函数的定义必须出现在内联函数第一次被调用之前
        C. 内联函数中可以包括各种语句
        D. 对内联函数不可以进行异常接口声明
【2.13】 在C++中,关于下列设置默认参数值的描述中,(   )是正确的。
        A.不允许设置默认参数值
        B.在指定了默认值的参数右边,不能出现没有指定默认值的参数
        C.只能在函数的定义性声明中指定参数的默认值
D. 设置默认参数值时,必须全部都设置   

【2.14】 下面的类型声明中正确是(  )。
          A. int &a[4];           B. int &*p;
          C.int &&q;              D. int i,*p=&i;
【2.15】 下面有关重载函数的说法中正确的是(   )。
          A.重载函数必须具有不同的返回值类型            
          B.重载函数形参个数必须不同               
          C. 重载函数必须有不同的形参列表               
          D.重载函数名可以不同   
【2.16】 关于new运算符的下列描述中,(   )是错误的。
          A,它可以用来动态创建对象和对象数组           
          B. 使用它创建的对象或对象数组可以使用运算符delete删除
          C. 使用它创建对象时要调用构造函数              
          D. 使用它创建对象数组时必须指定初始值
【2.17】 关于delete运算符的下列描述中,(   )是错误的。
          A.它必须用于new返回的指针                 
          B. 使用它删除对象时要调用析构函数              
          C. 对一个指针可以使用多次该运算符              
D.指针名前只有一对方括号符号,不管所删除数组的维数
【2.18】 写出下列程序的运行结果。
         #include<iostream>
using namespace std;
int i=15;
int main()
{ int i;
           i=100;
           ::i=i+1;
cout<<::i<<endl;
           return 0;
}
【2.19】 写出下列程序的运行结果。
#include<iostream>
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<<a<<" "<<b<<endl;
  return 0;
}
【2.20】 分析下面程序的输出结果。
#include<iostream>
using namespace std;
int &f(int &i)
{ i+=10;
  return i;
}
int main()
{ int k=0;
int &m=f(k);
cout<<k<<endl;
m=20;
cout<<k<<endl;
return 0;
}
【2.21】 举例说明可以使用const替代#define以消除#define的不安全性。
【2.22】 编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci 数列的前20项,并存储到动态分配的空间中。
【2.23】 编写一个C++风格的程序,建立一个被称为sroot()的函数,返回其参数的二次方根。重载sroot()3次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt())。
【2.24】 编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、5分的硬币,有多少种换法?
【2.25】 编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序输出。要求使用变量的引用。

第三章:
【3.7】 在下面有关对构造函数的描述中,正确的是(   )。
      A.构造函数可以带有返回值
      B.构造函数的名字与类名完全相同
      C.构造函数必须带有参数
      D.构造函数必须定义,不能默认
【3.8】 在声明类时,下面的说法正确的是(   )。
      A.可以在类的声明中给数据成员赋初值
      B.数据成员的数据类型可以是register
      C.private、public、protected可以按任意顺序出现
      D.没有用private、public、protected定义的数据成员是公有成员
【3.9】 在下面有关析构函数特征的描述中,正确的是(   )。
      A.一个类中可以定义多个析构函数
      B. 析构函数名与类名完全相同
      C. 析构函数不能指定返回类型
      D. 析构函数可以有一个或多个参数
【3.10】 构造函数是在(   )时被执行的。
         A.程序编译               B. 创建对象
         C. 创建类                 D.程序装人内存
【3.11】 在下面有关静态成员函数的描述中,正确的是(   )。
        A.在静态成员函数中可以使用this指针
        B. 在建立对象前,就可以为静态数据成员赋值
        C. 静态成员函数在类外定义时,要用static前缀
        D. 静态成员函数只能在类外定义
【3.12】 在下面有关友元函数的描述中,正确的说法是〔   )。
        A.友元函数是独立于当前类的外部函数
        B. 一个友元函数不能同时定义为两个类的友元函数
        C. 友元函数必须在类的外部定义
        D. 在外部定义友元函数时,必须加关键字friend
【3.13】 友元的作用之一是()。
        A.提高程序的运行效率      B.加强类的封装性
        C. 实现数据的隐藏性        D. 增加成员函数的种类
【3.14】 以下程序运行的结果是(   )。
#include<iostream>
using namespace std;
class B{
public:
        B(){}
        B(int i,int j)
        { x=i;
          y=j;
        }
        void printb()
        {cout<<x<<","<<y<<endl;
        }
private:
        int x,y;
};
class A{
   public:
           A()
           {}
           A(int I,int j);
           void printa();
   private:
           B c;
};
A::A(int i,int j):c(i,j)
{}
void A::printa()
{c.printb();
}
int main()
{ A a(7,8);
        a.printa();
        return 0;
}
A.8,9         B. 7,8          C. 5,6           D. 9,10
【3.15】 以下程序的运行结果是(   )。
         #include<iostream>
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<<b.get_area()<<endl;
return 0;
}
A.24          B. 4          C. 20          D. 6
【3.16】 以下程序的运行结果是(   )。
         #include<iostream>
using namespace std;
class Sample{
public:
         Sample(int i,int j)
         { x=i;
           y=j;
         }
         void disp()
         {cout<<"disp1"<<endl;
         }
         void disp() const
         {cout<<"disp2"<<endl;
         }
         private:
         int x,y;
};
int main()
{const Sample a(1,2);
a.disp();
return 0;
}
A. disp1   B.disp2    C. disp1 disp2    D. 程序编译出错
【3.17】 以下程序的运行结果是(   )。
         #include<iostream>
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<<R1<<","<<R2<<endl;
}
void R::print() const
{cout<<R1<<","<<R2<<endl;
}
int main()
{ R a(6,8);
const R b(56,88);
b.print();
return 0;
}
          A. 6,8    B. 56,88    C. 0,0    D. 8,6
【3.18】 写出下面程序的运行结果。
         #include<iostream>
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<<op[i][0].get_quan()<<",";
cout<<op[i][0].get_price()<<"\n";
cout<<op[i][1].get_quan()<<",";
cout<<op[i][1].get_price()<<"\n";
}
cout<<endl;
return 0;
}
【3.19】 写出下面程序的运行结果。
         #include<iostream>
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<<x.get_i()<<endl;
cout<<sqr_it(x)<<endl;
return 0;
}
【3.20】 写出下面程序的运行结果。
         #include<iostream>
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<<o1.gettotal()<<"objects in existence\n";
aClass *p;
p=new aClass;
if(!p)
{ cout<<"Allocation error\n";
   return 1;
}
cout<<o1.gettotal();
cout<<"objects in existence after allocation\n";
delete p;
cout<<o1.gettotal();
cout<<"objects in existence after deletion\n";
return 0;
}
【3.21】 写出下面程序的运行结果。
         #include<iostream>
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"<<ctr<<"\n";
   }
}
test anObject;
int main()
{ return 0;
}
【3.22】 写出下面程序的运行结果。
         #include<iostream>
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="<<a<<",b="<<b<<endl;
        }
};
int main()
{A a[3];
A b[3]={A(1,2),A(3,4),A(5,6)};
return 0;
}
【3.23】 写出下面程序的运行结果。
         #include<iostream>
using namespace std;
class Test{
private:
         int val;
public:
         Test()
         { cout<<"default."<<endl;
         }
         Test(int n)
         { val=n;
           cout<<"Con."<<endl;
         }
         Test(const Test& t)
         {val=t.val;
          cout<<"Copy con."<<endl;
         }
};
int main()
{Test t1(6);
Test t2=t1;
Test t3;
t3=t1;
return 0;
}
【3.24】 写出下面程序的运行结果。
         #include<iostream>
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="<<m.A<<endl;
cout<<"B="<<B<<endl;
}
int N::B=0;
int main()
{ N P(5),Q(9);
  N::f1(P);
  N::f1(Q);
  return 0;
}
【3.25】 写出下面程序的运行结果。
         #include<iostream>
using namespace std;
class M{
        int x,y;
public:
        M()
        { x=y=0;
        }
        M(int i,int j)
        {x=i;
         y=j;
        }
        void copy(M*m);
        void setxy(int i,int j)
        {x=i;
         y=j;
        }
        void print()
        {cout<<x<<","<<y<<endl;
        }
};
void M::copy(M*m)
{x=m->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;
}
【3.26】 写出下面程序的运行结果。
         #include<iostream>
using namespace std;
class M{
        int A;
        static int B;
public:
        M(int a)
        {A=a;
         B+=a;
         cout<<"Constructing"<<endl;
        }
        static void f1(M m);
        ~M()
        {cout<<"Destructing \n";
        }
};
void M::f1(M m)
{cout<<"A="<<m.A<<endl;
cout<<"B="<<B<<endl;
}
int M::B=0;
int main()
{M p(5),Q(10);
M::f1(p);
M::f1(Q);
return 0;
}
【3.27】 指出下列程序中的错误,并说明为什么。
         #include<iostream>
using namespace std;
class Student{
public:
        void printStu();
private:
        char name[10];
        int age;
        float aver;
};
int main()
{ Student p1,p2,p3;
p1.age=30;
.
.
.
  return 0;
}
【3.28】 指出下列程序中的错误,并说明为什么。
#include<iostream>
using namespace std;
class Student{
        int sno;
        int age;
          void printStu();
          void setSno(int d);
};
void printStu();
{ cout<<"\nSno is"<<sno<<",";
  cout<<"age is"<<age<<"."<<endl;
}
void setSno(int s)
{ sno=s;
}
void setAge(int a)
{ age=a;
}
int main()
{ Student lin;
  lin.setSno(20021);
  lin.setAge(20);
  lin.printStu();
}
【3.29】 指出下列程序中的错误,并说明为什么。
#include<iostream>
using namespace std;
class Point{
public:
        int x,y;
private:
        Piont()
        {x=1;y=2;
        }
};
int main()
{Point cpoint;
cpoint.x=2;
return 0;
}
【3.30】 下面是一个计算器类的定义,请完成该类成员函数的实现。
class counter{
public:
        counter(int number);      
        void increment();         //给原值加1
        void decrement();         //给原值减1
        int getvalue();           //取得计数器值
        int print();              //显示计数
private:
        int value;
};
【3.31】 根据注释语句的提示,实现类Date的成员函数。
#include<iostream>
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;
}
【3.32】 建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。
【3.33】 构建一个类book,其中含有两个私有数据成员qu和price,将qu初始化为1~5,将price初始化为qu的10倍,建立一个有5个元素的数组对象。显示每个对象数组元素的 qu*price值。
【3.34】 修改习题3.33,通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的 qu*price值。
【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个数据成员的值取默认值。要求编写程序分别显示这两个对象数据成员的值。
【3.36】 编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。
        

第四章:
【4.8】使用派中类的主要原因是(   )。
        A.提高代码的可重用性
        B.提高程序的运行效率
        C.加强类的封装性
        D.实现数据的隐藏
【4.9】 假设已经定义好了一个类student,现在要定义类derived,它是从student私有派生的,定义类derived的正确写法是(   )。
        A.clase derived::student private{…};
        B.clase derived::student public{…};
        C.clase derived::private student{…};
        D.clase derived::public student{…};
【4.10】 在多继承构造函数定义中,几个基类构造函数用(   )分隔。
         A. :          B. ;          C.,          D. ::
【4.11】 设置虚基类的目的是(   )。
         A.简化程序    B. 消除二义性  C. 提高运行效率  D.减少目标代码
【4.12】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class B1{
public:
        B1(int i)
        { b1=i;
          cout<<"Constructor B1."<<endl;
        }
        void Print()
        { cout<<b1<<endl;
        }
private:
        int b1;
};
class B2{
  public:
          B2(int i)
          {b2=i;
           cout<<"Constructor B2. "<<endl;
          }
          void Print()
          { cout<<b2<<endl;
          }
  private:
          int b2;
};
class A:public B2,public B1{
   public:
           A(int i,int j,int l);
           void Print();
   private:
           int a;
};
A::A(int i,int j,int l):B1(i),B2(j)
{ a=l;
  cout<<"Constructor A. "<<endl;
}
void A:rint()
{ B1:rint();
  B2:rint(); cout<<a<<endl;
}
int main()
{ A aa(3,2,1);
  aa.Print();
  return 0;
}
【4.13】 写出下面程序的运行结果。
#include<iostream>
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<<"主食="<<mainfood<<endl;
  cout<<"副食="<<subfood<<endl;
}
int main()
{ Menu m("bread","steak");
  m.show();
  return 0;
}
【4.14】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class A{
private:
        int a;
public:
        A()
        { a=0; }
        A(int i)
        { a=i; }
        void Print()
        { cout<<a<<",";
        }
};
class B:public A{
private:
        int b1,b2;
public:
        B()
        { b1=0;b2=0;
        }
        B(int i)
        { b1=i; b2=0;
        }
        B(int i,int j,int k):A(i),b1(j),b2(k)
        {}
        void Print()
        { A:rint();
          cout<<b1<<","<<b2<<endl;
        }
};
int main()
{ B ob1,ob2(1),ob3(3,6,9);
  ob1.Print();
  ob2.Print();
  ob3.Print();
  return 0;
}
【4.15】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class B1{
        int b1;
public:
        B1(int i)
        {b1=i;
         cout<<"constructor B1."<<i<<endl;
        }
        void print()
        { cout<<b1<<endl;
        }
};
class B2{
        int b2;
public:
        B2(int i)
        { b2=i;
          cout<<"constructor B2."<<i<<endl;
        }
        void print()
        { cout<<b2<<endl;
        }
};
class B3{
        int b3;
public:
        B3(int i)
        { b3=i;
          cout<<"constructor B3."<<i<<endl;
        }
        int getb3()
        { return b3;
        }
};
class A:public B2,public B1{
        int a;  B3 bb;
public:
        A(int i,int j,int k,int l):B1(i),B2(j),bb(k)
        {a=l;
         cout<<"constructor A."<<l<<endl;
        }
        void print()
        {B1::print();
         B2::print();
         cout<<a<<","<<bb.getb3()<<endl;
        }
};
int main()
{ A aa(1,2,3,4);
  aa.print();
  return 0;
}
【4.16】 写出下面程序的运行结果。
#include<iostream>
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<<s.sum()<<endl;
}
int main()
{ B ob(1,3,5,7);
  f(ob);
  return 0;
}
【4.17】 写出下面程序的运行结果。
#include<iostream>
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<<"("<<a<<","<<b<<")"<<endl;
        }
};
class B:private A{
        int x,y;
public:
        B(int i,int j,int k,int l):A(i,j)
        { x=k;y=l;
        }
        void Show()
        { cout<<x<<","<<y<<endl;
        }
        void fun()
        { Move(3,5);
        }
        void f1()
        { A::Show();
        }
};
int main()
{A e(1,2);
e.Show();
B d(3,4,5,6);
d.fun();
d.Show();
d.f1();
return 0;
}
【4.18】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class base1{
public:
        base1()
        { cout<<"class base1"<<endl;
        }
};
class base2{
public:
        base2()
        { cout<<"class base2"<<endl;
        }
};
class level1:public base2,virtual public base1{
public:
        level1()
        {cout<<"class level1"<<endl;
        }
};
class level2:public base2,virtual public base1{
public:
        level2()
        { cout<<"class level2"<<endl;
        }
};
class toplevel:public level1,virtual public level2{
public:
        toplevel()
        { cout<<"class toplevel"<<endl;
        }
};
int main()
{ toplevel obj;
  return 0;
}
【4.21】 已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time和Date,并且增加一个数据成员Childname用于表示小孩的名字,同时设计主程序显示一个小孩的出生时间和名字。
class Time{
public:
        Time(int h,int m,int s)
        { hours=h;
minutes=m;
seconds=s;
}
void display()
{ cout<<"出生时间:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;
}
protected:
        int hours,minutes,seconds;
};
class Date{
public:
        Date(int m,int d,int y)
        { month=m;
          day=d;
          year=y;
        }
        void display()
        { cout<<"出生年月:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
        }
protected:
        int month,day,year;
};
【4.22】 编写一个学生和教师数据输人和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。

第五章:
【5.7】 有关运算符重载正确的描述是(   )。
       A.C++语言允许在重载运算符时改变运算符的操作个数
       B.C++语言允许在重载运算符时改变运算符的优先级
       C.C++语言允许在重载运算符时改变运算符的结合性
       D.C++语言允许在重载运算符时改变运算符原来的功能
【5.8】 能用友元函数重载的运算符是(   )。
        A.+       B. =        C. []        D.->
【5.9】 关于虚函数,正确的描述是(   )。
        A.构造函数不能是虚函数
        B.析构函数不能是虚函数
        C.虚函数可以是友元函数
        D.虚函数可以是静态成员函数
【5.10】 派生类中虚函数原型的(   )。
        A.函数类型可以与基类中虚函数的原型不同
        B.参数个数可以与基类中虚函数的原型不同
        C.参数类型可以与基类中虚函数的原型不同
        D.以上都不对
【5.11】 如果在基类中将show声明为不带返回值的纯虚函数,正确的写法是()。
         A. virtual show()=0;
B. virtual void show();
C. virtual void show()=0;
D. void show()=0 virtual;
【5.12】下列关于纯虚函数与抽象类的描述中,错误的是(   )。
       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<iostream>
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<<x<<endl;
}
int main()
{ A a(7);
  ++a;
  a.print();
  --a;
  a.print();
  return 0;
}
【5.15】 写出下列程序的运行结果。
#include<iostream>
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:perator [](int n)
{ if(n<0||n>len-1)            //数组的边界检查
{ cout<<"数组下标超界!\n";
  return ' ';
}
else
   return *(str+n);
}
void Words::disp()
{ cout<<str<<endl;
}
int main()
{ Words word("This is C++book.");
  word.disp();
  cout<<"第1个字符:";
  cout<<word[0]<<endl;     //word[10]被解释为word.operator[](10)
  cout<<"第16个字符:";
  cout<<word[15]<<endl;
  cout<<"第26个字符:";
  cout<<word[25]<<endl;
  return 0;
}
【5.16】 写出下列程序的运行结果。
#include<iostream>
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="<<m<<"千米"<<endl;
  return 0;
}
【5.17】 编一个程序,用成员函数重载运算符“+”和“-”将两个二维数组相加和相减,要求第一个二维数组的值由构造函数设置,另一个二维数组的值由键盘输入。
【5.18】 修改习题5.17,用友元函数重载运算符“+”和“-”将两个二维数组相加和相减。
【5.19】 编写一个程序,要求:
        (1)声明一个类complex,定义类complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型:real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5;
        (2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时能返回两个复数对象相加操作;
        (3)定义成员函数print,调用该函数时,以格式“(real,imag)”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:(4.2,6.5);
        (4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。
                                                                                                                                                                                             
奥鹏作业答案,奥鹏在线作业答案
您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

 
 
客服一
客服二
客服三
客服四
点这里给我发消息
点这里给我发消息
谋学网奥鹏同学群2
微信客服扫一扫

QQ|关于我们|联系方式|网站特点|加入VIP|加盟合作|投诉建议|法律申明|Archiver|小黑屋|奥鹏作业答案-谋学网 ( 湘ICP备2021015247号 )

GMT+8, 2024-5-7 23:22 , Processed in 0.100899 second(s), 18 queries .

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表