C++题库

1、面向对象的三大特征分别为( 继承 )、( 多态 )和( 封装 )。

2、类中成员的访问方式有( public )、( private )和( protected ),默认的访问方式为( private )。

3、类中的析构函数有( 1 )个。

4、类的拷贝构造函数的参数为( const 类名 &名字 )。

1、C++中用( inline )替代C中带参的宏,用( const )替换不带参的宏。

4、( 引用 )是变量的别名。

1、函数重载,函数名相同,参数不同,参数不同体现在参数的(类型)、( 顺序 )或( 个数 )不同。

2、C++进行输入输出操作时,必须包含头文件(iostream)。

3、类的成员函数中隐藏着一个参数即(this )。

4、静态成员函数的主要作用是用来访问类的(静态数据成员)。

1、下列函数重载不正确的是( B )。
A、int p(int);和 void p(double);
B、int d(int t);和 char *d(int y);
C、int s(int x,char *t); 和 int s(char *t,int x);
D、int v(int x,int y);和 int v(int);

2、公有派生类中的成员函数可以访问基类中的(非私有)成员,公有派生类对象可以访问基类中的( 公有 )成员。

3、含有对象成员的派生类,析构函数调用顺序为( 派生类,对象成员, 一般基类, 虚基类 )。

4、含有对象成员的派生类,构造函数调用顺序为(虚基类,一般基类,对象成员,派生类 )。

1、C++中不能重载的运算符为( . .* sizeof :: ?: )。

2、判断下面关于友元的说法正确性。
友元类之间的友元关系具有对称性。F
友元类之间的友元关系具有传递性。F
友元函数可以访问类的私有成员。T
友元函数必须声明在类的内部。T

3、类的构造函数必须为(public )函数。填(public、private 或protected)

4、包含( 纯虚函数 )的类为抽象类。

1、写出下面程序的运行结果。
#include
using namespace std;
int s(int x,int &y)
{
x+=y;
y+=x;
return y;
}
int main()
{
int a=3,b=4;
s(a,b);
s(b,a);
cout<<a<<“,”<<b<<endl;
return 0;
} //17,11

2、f是类的一个常成员函数,无参数,无返回值,则其在类中的声明为( void f() const; )。

3、template , 则T的类型为( 任意类型 )。

4、若运算符+采用成员函数重载,则a+b的函数调用格式为( a.operator(b) )。

1、若运算符+采用友元函数重载,则a+b的函数调用格式为( operator+(a,b) )。

2、只能通过友元函数重载的运算符为( >> << )。

3、常数据成员的初始化操作应该在( 初始化表 )处进行。

4、下列语言不是面向对象程序设计语言的是( C )。
C C++ Java Phthon

1、类S中包含常数据成员a,试写成对a初始化语句( S::S():a(10){} )。

2、用new运算符创建一个包含10个元素数组的语句为( *int p=new int[10]; )。

3、引入友元的作用为( 提高程序的运行效率和灵活性 )。

4、类的析构函数带有( 0 )个参数。

1、#include
using namespace std;
class A
{
public:
A(){cout<<“AA”; }
~A(){cout<<“DA”; }
};
class B
{
A a;
public:
B(){cout<<“BB”; }
~B(){cout<<“DB”; }
};
int main()
{
B b;
cout<<“CC”;
return 0;
} //AABBCCDBDA

2、C++源程序后缀(即扩展名)为( .cpp )。

3、判断正误 符号常量定义时必须进行初始化。T

4、派生类的成员函数可以访问基类中的( 非私有 public 或protected )成员。填访问类别

已知一个复数类设计如下:

class comp
{
	double real,imag;
public:
	comp(double r=0,double i=0);
	comp operator+(const comp &t);
	friend ostream &operator<<(ostream &out,const comp &t);	
};

试实现类中的函数并在main函数中进行测试。

代码编写如下

#include<iostream>
using namespace std;
class comp {
	double real, imag;
public:
	comp(double r = 0, double i = 0);
	comp operator+(const comp& t);
	friend ostream& operator<<(ostream& out, const comp& t);
};
comp::comp(double r, double i) {
	real = r; imag = i;
}
comp comp::operator+(const comp& t) {
	comp result;
	result.real = real + t.real;
	result.imag = imag + t.imag;
	return result;
}
ostream& operator<<(ostream& out, const comp& t) {
	out << t.real << " + " << t.imag << " i";
	return out;
}
int main() {
	comp c1(10, 17328);
	comp c2(5, 2342);
	comp c3;

	cout << "c1 + c2 =" << c1 + c2;

	return 0;
}

已知一个复数类设计如下:

class comp
{
	double real,imag;
public:
	comp(double r=0,double i=0);
	bool operator>(const comp &t);//比较到原点的距离
	friend ostream &operator<<(ostream &out,const comp &t);	
};

试实现类中的函数并在main函数中进行测试。

代码编写如下

#include<iostream>
using namespace std;
class comp {
	double real, imag;
public:
	comp(double r = 0, double i = 0);
	bool  operator>(const comp& c);
	friend ostream& operator<<(ostream& out, const comp& t);
};
comp::comp(double r, double i) {
	real = r; imag = i;
}
bool comp::operator>(const comp& t) {
	if ((real*real+imag*imag>t.real*t.real+t.imag*t.imag)||((real * real + imag * imag == t.real * t.real + t.imag * t.imag)&& real>t.real)) {
		return true;
	}
	else {
		return false;
	}
}
ostream& operator<<(ostream& out, const comp& t) {
	out << t.real << " + " << t.imag <<" i";
	return out;
}
int main() {
	comp c1(10, 17328);
	comp c2(5, 2342);
	
	cout << "c1 : " << c1 << endl;
	cout << "c2 : " << c2 << endl;

	if (c1 > c2) {
		cout << "c1 > c2 " << endl;
	}
	else {
		cout << "c1 <= c2" << endl;
	}
	return 0;
}

1、定义一个学生类stu,包含私有成员 年龄和姓名,接口有:构造函数,修改年龄的函数(年龄增加一岁)。在main函数中对stu类进行测试。

代码编写如下

#include<iostream>
#include<string.h>
using namespace std;
class Student {
private:
	int age;
	string name;
public:
	Student() { name = "NoName"; age = 0; }
	Student(int a, string n) { name = n; age = a; }
	//可以简写为Student(int a = 0; string n = " NoName"){name = n ; age = a;}
	void AddAge();
	void Print();
};
void Student::AddAge() {
	age++;
}
void Student::Print() {
	cout << name << "今年" << age << "岁了" << endl;
}
int main() {
	Student s1(18, "张三");
	for (int i = 0; i < 100; i++) {
		s1.Print();
		s1.AddAge();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优降宁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值