C++题库

news/2025/4/2 5:11:11/

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;
}

http://www.ppmy.cn/news/439419.html

相关文章

H5页面 部分Android 手机(魅族M721Q)滑动不流畅问题

弹窗中可滑动区域&#xff1a;overflow-y:scroll;-webkit-overflow-scrolling:touch; 问题&#xff1a;对于魅族M721Q机型&#xff1a;滑动停止之后&#xff0c;继续再划 划不动 解决方案&#xff1a;检查滑动区域的父级元素&#xff0c;发现有这个定义&#xff1a;overflow:…

魅族账号服务器返回异常,魅族确认Flyme用户数据错乱 已进行回档处理现恢复正常...

8月18日晚&#xff0c;有网友反应自己的魅族手机的通讯录中&#xff0c;出现了不少陌生人的手机号码&#xff0c;而原本属于自己的通讯录联系人均消失不见&#xff0c;甚至通讯录里出现了很多陌生人&#xff0c;一些别人的便签也同步到自己的手机中。网友据此猜测&#xff0c;自…

计算机二级Python编程题记录

文章目录 第一套第二套第三套第四套第五套第六章第七套第八套第九套第十套第十一套第十二套第十三套第十四套第十五套第十六套第十七套新增1新增2新增3 第一套 1 s input("请输入一个字符串:") print("{:*^30}".format(s))2 a, b 0, 1 while a<…

Android application 和 activity 标签详解

Application 标签 android:allowTaskReparenting android:allowTaskReparenting[“true” | “false”]   表明了这个应用在 reset task 时&#xff0c;它的所有 activity 是否可以从打开它们的 task 栈中迁移到它们声明的 taskAffinity 亲和性&#xff08;taskAffinity 属性…

Linux上

Linux linux系统介绍linux的概述linux的优势linux的应用场景linux的分类常见的发行版linux系统注意常见发行版如下 linux安装linux操作系统的安装方式常见虚拟机软件安装VMWare虚拟机安装centos7系统失败的解决方案错误原因开启操作系统bios虚拟化支持的步骤启动虚拟机常见问题…

python作业ATM(第五周)

作业需求&#xff1a; 额度 15000或自定义。 实现购物商城&#xff0c;买东西加入 购物车&#xff0c;调用信用卡接口结账。 可以提现&#xff0c;手续费5%。 支持多账户登录。 支持账户间转账。 记录每月日常消费流水。 提供还款接口。 ATM记录操作日志。 提供管理接口&#x…

android基础面试题(一)

Android基础面试题 &#xff08;⭐⭐⭐&#xff09; 1、什么是ANR 如何避免它&#xff1f; 答&#xff1a;在Android上&#xff0c;如果你的应用程序有一段时间响应不够灵敏&#xff0c;系统会向用户显示一个对话框&#xff0c;这个对话框称作应 用程序无响应&#xff08;ANR…

国内安卓统一推送联盟名单出炉,谷歌成为观察员;微软发布 Q# 量子编程语言预览...

&#xff08;点击上方蓝字&#xff0c;快速关注我们&#xff09; 参考&#xff1a;开源中国、solidot、cnBeta、腾讯科技等 0、微软发布 Q# 量子编程语言预览 微软发布了针对量子计算的新编程语言 Q#&#xff08;类似 C#&#xff0c;读作 Q sharp&#xff09;预览版。Q# 将传统…