岗位:C++/Qt中级开发工程师
第一部分:选择题
1. 一个函数功能不太复杂,但要求被频繁调用,则应把它定义为(A )
A. 内联函数
B. 重载函数
C. 递归函数
D. 嵌套函数
2.对于C++中类(class)与结构(struct)的描述正确的有(A)
A.类中成员默认是private 的,但可以声明为public,private,protected,结构中定义的成员默认的都是public;
B.结构中不允许定义成员函数,但是类中可以定义成员函数;
C.结构实例使用malloc() 动态创建,类对象使用new 操作符动态分配内存;
D.结构和类对象都必须使用new 创建;
E.结构中不可以定义虚函数,当是类中可以定义虚函数.
F.结构不可以存在继承关系,当是类可以存在继承关系.
3. 关于this指针使用说法正确的是(A)
A. 保证每个对象拥有自己的数据成员,但共享处理这些数据的代码
B. 保证基类私有成员在子类中可以被访问。
C. 保证基类保护成员在子类中可以被访问。
D. 保证基类公有成员在子类中可以被访问。
4. 下列不能作为类的成员的是(B)
A. 自身类对象的指针
B. 自身类对象
C. 自身类对象的引用
D. 另一个类的对象
第二部分:程序阅读纠错题
5. A和B是两个数组,N是数组的长度,请问下列代码返回的数组B中的元素的值将会是什么呢?(例如A[4]={1095,675,457,632},那么B中的四个元素值?)
void F(int* A, int* B, int N) {
int prod = 1;
for (int i = 0; i < N; ++i) {
B[i] = prod;
prod *= A[i];
}
prod = 1;
for (int i = N - 1; i >= 0; --i) {
B[i] *= prod;
prod *= A[i];
}
}
答案:经过程序测试
B[0]: A[1]*A[2]*A[3]
B[1]: A[0]*A[2]*A[3]
B[2]: A[0]*A[1]*A[3]
B[3]: A[0]*A[1]*A[2]
推导过程:
第一个循环得到:B[n] = (A[n-1])! (n>0), B[0] = 1
第二个循环得到:
B[3] = B`[3]* 1 = (A[2])!
B[2] = B`[2]* A[3] = (A[1])! *A[3]
B[1] = B`[1]* A[3]* A[2] = A[0]*A[3]*A[2]
B[0] = B`[0]* A[3]* A[2]* A[1] = 1 * A[3]* A[2]* A[1]
6. 下列代码的输出是什么呢?
#include <iostream>
class Base {
virtual void method() {std::cout << "from Base" << std::endl;}
public:
virtual ~Base() {method();}
void baseMethod() {method();}
};
class A : public Base {
void method() {std::cout << "from A" << std::endl;}
public:
~A() {method();}
};
int main(void) {
Base* base = new A;
base->baseMethod();
delete base;
return 0;
}
答案:
from A
from A
from Base
7. 请指出下列代码存在的问题
#include <vector.h>
void main(int argc, char** argv)
{
int n;
if (argc > 1)
n = argv[0];
int* stuff = new int[n];
vector<int> v(100000);
delete stuff;
return 0;
}
答案:
n 的类型应该为 char*
Delete stuff 应为 delete[] stuff
8.下列代码存在问题吗?如果有问题,请指出问题。
class A
{
public:
A() {}
~A() {}
};
class B: public A
{
public:
B():A(){}
~B(){}
};
int main(void)
{
A* a = new B();
delete a;
}
答案:
class A 在这里是基类,析构函数应写成虚函数
第三部分:问答题
9. 在什么情况下应该使用虚拟继承?
答案:
菱形继承,也就是多继承且继承的父类有共同父类的时候应该使用虚拟继承
10. 请解释volatile和mutable的作用
答案:
volatile每次都会从内存中读取值而不是使用寄存器中的值,有些人会用这个做线程间的资源共享,但是这是一种未定义的行为,可能会因编译器的实现不同而导致问题
mutable 表示可变动,也就是说,可以在函数尾部带const 的函数中改变
11. 局部变量和全局变量是否可以同名?
答案:
可以同名,且会根据作用域改变值的作用范围
12. 在定义一个宏的时候要注意什么?
答案:
注意隐式转化、运算符重载、宏的作用范围等,一般情况下,如非必要不适用宏,使用模板或枚举代替
13.怎么解决类之间的相互引用。
答案:
提供一个代理类或适配器类处理两个类之间的耦合问题
第四部分:编程题
14. 定义一个三角形(Triangle)类,包括三角形的三个顶点坐标,重载“<”和“>”运算符,用来实现两个三角形面积的比较。重载“==”运算符,如果两个面积相同,则返回true,否则返回false。(要求:定义类,且包含三个运算符重载函数,main()中包括简单示例,调用重载后的运算符)
答案:(已编译测试)
#include <iostream>
using namespace std;
struct Point
{
double x;
double y;
};
class Triangle
{
public:
Triangle(Point p0, Point p1, Point p2)
:m_p0(p0)
,m_p1(p1)
,m_p2(p2)
{}
double size() const;
bool operator >(const Triangle &other);
bool operator <(const Triangle &other);
bool operator ==(const Triangle &other);
private:
Point m_p0;
Point m_p1;
Point m_p2;
};
int main(int argc, char *argv[])
{
Point p1;
Point p2;
Point p3;
Point p4;
p1.x = 1.0;
p1.y = 1.0;
p2.x = 2.0;
p2.y = 2.0;
p3.x = 3.0;
p3.y = 3.0;
p4.x = 4.0;
p4.y = 4.0;
Triangle tri(p1, p2, p3);
Triangle tri2(p1, p2, p3);
cout << "size" << tri.size();
cout << "same: " << (tri == tri2);
return 0;
}
double Triangle::size() const
{
double result;
result = abs((m_p2.x * m_p0.y - m_p0.x * m_p2.y)
+(m_p2.x * m_p1.y - m_p1.x * m_p2.y)
+(m_p1.x * m_p0.y - m_p1.x * m_p2.y))/2;
return result;
}
bool Triangle::operator >(const Triangle &other)
{
bool result = false;
if (this->size() > other.size())
{
result = true;
}
return result;
}
bool Triangle::operator <(const Triangle &other)
{
bool result = false;
if (this->size() < other.size())
{
result = true;
}
return result;
}
bool Triangle::operator ==(const Triangle &other)
{
bool result = false;
if (abs(this->size() - other.size()) < 0.000001)
{
result = true;
}
return result;
}
15. Your task is to calculate the sum of some integers.
Input:
Input contains an integer N in the first line, and then N lines follow. Each line starts with an integer M, and then M integers follow in the same line.
Output:
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
答案:(仅做实现)
#include <QCoreApplication>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
typedef vector<int> vec;
vec result;
vec tmps;
int m;
int tmp;
int sum;
for (int index = 0; index != n; ++index)
{
cin >> m;
sum = 0;
tmps.clear();
for (int subIndex = 0; subIndex != m; ++subIndex)
{
cin >> tmp;
tmps.push_back(tmp);
sum += tmp;
}
result.push_back(sum);
cout << "sum: " << sum << "str: " ;
for (int i = 0; i < tmps.size(); ++i)
{
cout << tmps.at(i);
}
cout << endl;
cin.get();
}
return 0;
}
注:1.答案是自己答的,如果有不对的,可以在评论中回复,我加以修正。
2.由于最近重心放在vkt应用上,此分类暂不做其他更新。