寒假作业2月4号

第三章  类与构造函数

一.选择题

   1、下列不能作为类的成员的是(B

A. 自身类对象的指针

B. 自身类对象

C. 自身类对象的引用

D. 另一个类的对象

   2、假定AA为一个类,a()为该类公有的函数成员,x为该类的一个对象,则访问x对象中函数成员a()的格式为(B

A. x.a

B. x.a()

C. x->a

D. (*x).a()

3、已知:p是一个指向类A数据成员m的指针,A1是类A的一个对象。如果要给m赋值为5,正确的是(D

A. A1.p=5;

B. A1->p=5;

C. A1.*p=5;

D. *A1.p=5;

 4、下列不具有访问权限属性的是(A

A. 非类成员

B. 类成员

C. 数据成员

D. 函数成员

 5、 下面的叙述中那个是不正确的是___C____

     A 类必须提供至少一个构造函数

     B 默认构造函数的形参列表中没有形参

     C 如果一个类没有有意义的默认值,则该类不应该提供默认构造函数

     D 如果一个类没有定义默认构造函数,则编译器会自动生成一个,同时将每个数据成员初始化为相关类型的默认值

二.填空题

1、给出下面程序输出结果。

#include <iostream>

using namespace std;

class Test

{   int x,y;

public:

Test(int i,int j=0)

{x=i;y=j;}

int get(int i,int j)

{return i+j;}

};

int main()

{ Test t1(2),t2(4,6);

int (Test::*p)(int,int);

p=&Test::get;

cout<<(t1.*p)(5,10)<<endl;    15

Test *p1=&t2;

cout<<(p1->*p)(7,20)<<endl;    27

}

2、下面程序运行的结果是:5+10=15。

#include <iostream.h>

class Test

{ private:

int x,y;

public:

Test() {x=y=0;}

void Setxy(int x,int y) {this->x=x;  this->y=y;}

void show(){cout << x <<+<<y<<=<<x+y<<endl;}

};

int main()

{  Test ptr;

ptr.Setxy(5,10);

ptr.show();

}

3、请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

11,   10

13,   12

#include <iostream.h>

class A

{int a;

public:

A(int i=0){a=i;}

int Geta(){return a;}

void show(){cout<<a<<endl;}

};

class B

{

A a;

int b;

public:

B(int i,int j):_a(i)___,_b(j)_____

{   }

void show(){cout<<a.Geta()<<","<<b<<endl;}

};

void main()

{   B   b[2]={B(10,11),B(12,13)};

for(int i=0;i<2;i++)

__b[i].show;________

}

三、改错题

 1. #include <iostream.h>

class Test

{ private:

int x,y=20;

public:

Test(int i,int j){ x=i,y=j; }

int getx(){return x;}

int gety(){return y;}

};

void main()

{  Test mt(10,20);     Test mt;   mt.x = 10; mt.y = 20;

cout<<mt.getx()<<endl;

cout<<mt.gety()<<endl;

}

2. #include <iostream.h>

class Test

{int x,y;

public:

void fun(int i,int j)

{x=i;y=j;}

void show()

{

cout<<"x="<<x;

if(y)

cout<<",y="<<y<<endl;

cout<<endl;

}

};

int main()

{  Test a;

   a.fun( )

a.fun(1);   错了,没有对应函数

a.show();

a.fun(2,4);

a.show();

}

3. #include <iostream.h>

class X

{  public:

int x;

public:

X(int x)

{cout<< this->x=x <<endl;}

X(X&t)

{x=t.x;

cout<<t.x<<endl;

}

void fun(X);

};

void fun(X t)

{ cout<<t.x<<endl;}

void main()

{ fun(X(10));}      没有这个构造函数成员类型

4. #include <iostream.h>

#include <string.h>

class Bas

{  public:

Bas(char *s="\0"){strcpy(name,s);}

void show();

protected:

char name[20];

};

Bas b;

void show()

{   cout<<"name:"<<b.name<<endl;  }  不能在类外访问protected,放到类内即可

void main()

{

Bas d2("hello");

show();        

}

四、编程题

4.1   a: 编写一个类Person,表示一个人的名字和地址,使用string来保存每个元素

   b:为Person提供一个接受两个string参数的构造函数

  c:提供返回名字和地址的操作

  d:指明Person的那个成员应声明为public,那个成员应声明为private

  • 24
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值