一.选择题
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()
解析:访问对象中成员函数的格式为:对象名成员函数名(参数表),本题中成员函数a()的形参表为空
3、已知:p是一个指向类A数据成员m的指针,A1是类A的一个对象。如果要给m赋值为5,正确的是( C )
A. A1.p=5;
B. A1->p=5;
C. A1.*p=5;
D. *A1.p=5;
解析:
#include <iostream>
using namespace std;
class AA
{
public:
int m;
int *p=&m;
void show()
{
cout << "m=" << m << endl;
}
};
int main()
{
AA A1;
*A1.p=5;
A1.show();
return 0;
}
4、下列不具有访问权限属性的是( A )
A. 非类成员
B. 类成员
C. 数据成员
D. 函数成员
解析:类成员、数据成员、函数成员都可以被访问,但不可以访问非类成员
5、 下面的叙述中那个是不正确的是 C
A 类必须提供至少一个构造函数
B 默认构造函数的形参列表中没有形参
C 如果一个类没有有意义的默认值,则该类不应该提供默认构造函数
D 如果一个类没有定义默认构造函数,则编译器会自动生成一个,同时将每个数据成员初始化为相关类型的默认值
解析:选项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;
Test *p1=&t2;
cout<<(p1->*p)(7,20)<<endl;
}
解析:int (Test::*p)(int,int);这句是给类Test添加一个指针成员p,指向get()有参构造函数,get()函数的返回值是两个形参值的和,所以(t1.*p)(5,10)就是访问有参构造函数,返回的结果为5+10=15。Test *p1=&t2;是实例化一个指针对象指向t2,(p1->*p)(7,20)也是访问有参构造函数,返回的结果为7+20=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();
}
解析:
#include <iostream>
using namespace std;
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();
return 0;
}
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(j),b(i)
{ }
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();
}
解析:
#include <iostream>
using namespace std;
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(j),b(i){}
void show()
{
cout << a.Geta() << "," << b << endl;
}
};
int main()
{
B b[2]={B(10,11),B(12,13)};
for(int i=0;i<2;i++)
b[i].show();
return 0;
}
三、改错题
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);
cout<<mt.getx()<<endl;
cout<<mt.gety()<<endl;
}
解析:#include <iostream.h>里面应该是iostream,不是iostream.h
缺少了要使用的命名空间,应在加上#include <iostream>的下一行添加using namespace std;
void main(){}报错,main 必须返回int改为:int main(){return 0;}
#include <iostream>
using namespace std;
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;
}
};
int main()
{
Test mt(10,20);
cout << mt.getx() << endl;
cout << mt.gety() << endl;
return 0;
}
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();
}
解析:#include <iostream.h>里面应该是iostream,不是iostream.h缺少了要使用的命名空间,应在加上#include <iostream>的下一行添加using namespace std;
a.fun( ) :句尾没加分号,由void fun(int i,int j)可知应传2个参数,但实际没有传参数
a.fun(1); :应传2个参数,只传递了一个参数
#include <iostream>
using namespace std;
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));}
解析:#include <iostream.h>里面应该是iostream,不是iostream.h缺少了要使用的命名空间,应在加上#include <iostream>的下一行添加using namespace std;
cout<< this->x=x <<endl;这句里不能有赋值,否则会报错,应为:this->x=x; cout<< x <<endl;
void main(){}错,应为:int main() { return 0;}
fun(X(10));这句错误,应先实例化一个对象再调用fun()函数应为:X t(10);fun(t);
#include <iostream>
using namespace std;
class X
{
public:
int x;
public:
X(int x)
{
this->x=x;
cout << x << endl;
}
X(X&t)
{
x=t.x;
cout << t.x << endl;
}
void fun(X);
};
void fun(X t)
{
cout << t.x << endl;
}
int main()
{
X t(10);
fun(t);
return 0;
}
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; }
void main()
{
Bas d2("hello");
show();
}
解析:#include <iostream.h>里面应该是iostream,不是iostream.h缺少了要使用的命名空间,应在加上#include <iostream>的下一行添加using namespace std;
char *s="\0"错,应为:char *s=" "
由protected: char name[20];知name是受保护的成员,只能在类内和子类被访问, 所有cout<<"name:"<<b.name<<endl;这句错,应该是void Bas::show(){cout<<"name:"<<name<<endl;}
main函数需返回一个int,所以改为int main(){return 0;}
最后show();函数不能直接调用,应加上类的对象:d2.show();
#include <iostream>
#include <string.h>
using namespace std;
class Bas
{
public:
Bas(char *s="")
{
strcpy(name,s);
}
void show();
protected:
char name[20];
};
Bas b;
void Bas::show()
{
cout << "name:" << name << endl;
}
int main()
{
Bas d2("hello");
d2.show();
return 0;
}
四、编程题
4.1
a: 编写一个类Person,表示一个人的名字和地址,使用string来保存每个元素
b:为Person提供一个接受两个string参数的构造函数
c:提供返回名字和地址的操作
d:指明Person的那个成员应声明为public,那个成员应声明为private
#include <iostream>
using namespace std;
class Person//封装一个类Person
{
private://私有属性
string name;//成员:名字
string address;//成员:地址
public://公共属性
Person()
{
cout << "无参构造函数" << endl;
}
Person(string name,string address):name(name),address(address)//初始化列表
{
cout << "有参构造函数" << endl;
}
string getName(Person &p)const//获取返回名字
{
return p.name;
}
string getAddress(Person &p)const//获取返回地址
{
return p.address;
}
void show()
{
cout << "name:" << name << ",address:" << address << endl;
}
};
int main()
{
Person p("张三","A市");//实例化一个对象p并初始化
p.show();
cout << "获取返回的名字为:" << p.getName(p) << endl;
cout << "获取返回的地址为:" << p.getAddress(p) << endl;
return 0;
}
结果