C++常见面试题

时间:2023-02-09 12:01:56


1. 声明一个 circle 类,有数据成员 Radius(半径,float型),成员函数 GetArea() 计算圆的面积,在main 函数中声明一个cirlce类的对象 c1,其半径为 5.6,调用 GetArea() 函数计算 c1的面积,并显示该计算结果;

#include <iostream>
using namespace std;

class circle
{
protected:
float Radius;

public:
circle(float r);
float GetArea();
};
circle::circle(float r)
{
Radius = r;
};
float circle::GetArea()
{
return Radius * Radius * 3.14;
};
int main()
{
circle c1(5.6);
cout << "圆的面积" << c1.GetArea() << endl;
return 0;
}

2. 声明复数类Complex,该类中有两个私有变量 real 和 imag,分别表示一个复数中的实部和虚部,请添加一个友元函数add实现复数的加法;

#include <iostream>
using namespace std;

class Complex
{
private:
double real, imag;

public:
// 构造函数
Complex() {}
Complex(double a, double b)
{
real = a;
imag = b;
}
void setRI(double a, double b)
{
real = a;
imag = b;
}
double getReal()
{
return real;
}
double getImag()
{
return imag;
}
void print()
{
if (imag > 0)
{
cout << "复数" << real << "+" << imag << "i" << endl;
}
if (imag < 0)
{
cout << "复数" << real << "-" << imag << "i" << endl;
}
}
// 声明一个友元函数
friend Complex add(Complex, Complex);
};

// 在类外定义友元函数
Complex add(Complex c1, Complex c2)
{
Complex c3;
c3.real = c1.real + c2.real;
c3.imag = c1.imag + c2.imag;
return c3;
};

int main()
{
Complex c1(19, 0.864), c2, c3;
c2.setRI(90, 125.012);
c3 = add(c1, c2);
cout << "复数一:";
c1.print();
cout << "复数二:";
c2.print();
cout << "相加后:";
c3.print();
return 0;
}

3. 有一个 Person 类,私有数据成员 name、age 和 Sex 分别表示人的姓名、年龄和性别。雇员类 Employee 是 Person 的派生类,新增数据成员部门 department 和薪水 salary。请用C++代码描述这两个类,并用 Employee 类的成员函数 Display 实现雇员的姓名、年龄、性别、部门和薪水的输出,要求编写派生类的构造函数;

#include <iostream>
using namespace std;

class Person
{
protected:
char name[8];
int age;
char sex[2];

public:
Person(char *s1, char *s2, int a = 0)
{
strcpy(name, s1);
age = a;
strcpy(sex, s2);
}
};

class Employee : public Person
{
private:
char department[20];
double salary;

public:
Employee(char *s1, char *s2, char *s3, int a = 0, double s = 0.0) : Person(s1, s2, 0)
{
strcpy(department, s3);
}
void Display()
{
cout << name << " " << age << " " << sex << " " << department << " " << salary << endl;
}
};

int main()
{
Employee ee("张三", "男", "计算机", 18, 3000.0);
ee.Display();
return 0;
}

4. 定义一个图类(figure),其中有保护类的数据成员:高度(height)和宽度(width), 一个公有的构造函数。由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类都包含一个函数 area(),分别用来计算矩形和等腰三角形的面积;

#include <iostream>
using namespace std;
// 定义图形基类
class figure
{
protected:
double height, width;

public:
figure(double = 0, double = 0);
};
// 类外定义构造函数
figure::figure(double h, double w)
{
height = h;
width = w;
};

// 定义三角形类
class triangle : public figure
{
public:
double area();
triangle(double = 0, double = 0);
};

// 类体外定义构造函数并初始化基类
triangle ::triangle(double h, double w) : figure(h, w)
{
height = h;
width = w;
};
// 定义三角形的计算面积的函数
double triangle ::area()
{
return 0.5 * height * width;
};

// 定义矩形
class rectangle : public figure
{
public:
double area();
rectangle(double = 0, double = 0);
};
// 类体外定义构造函数并初始化基类
rectangle::rectangle(double h, double w) : figure(h, w)
{
height = h;
width = w;
};
// 定义矩形计算面积的函数
double rectangle::area()
{
return height * width;
};

int main()
{
triangle tri(2, 3);
rectangle rec(2, 3);
cout << "the area of triangle is" << tri.area() << endl;
cout << "the area of rectangle is" << rec.area() << endl;
return 0;
};

5. 用代码实以现以图案;

*  *  *  *  *  *  *  *  *

* * * * * * *

* * * * *

* * *

*

代码如下:

#include <iostream>
using namespace std;
int main()
{
// 控制行数
for(int i = 0; i<5; i++){
// 输出空格
for(int j=1;j<i;j++){
cout<<" ";
};
// 输出星号(核心代码)
for(int n=9;n>=2*i-1;n--){
cout<<"*";
};
// 换行
cout<<endl;
}
return 0;
};

6.写一个程序,定义一个抽象类Shape,由它派生3个类:Square(正方形)、Trapezoid(梯 形) 和 Triangle(三角形)。用虚函数分别计算几种图形面积、并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象;

#include <iostream>
using namespace std;

class Shape
{
public:
virtual double area() const = 0;
};

// 定义正方形
class Square : public Shape
{
private:
double slide;

public:
Square(double s) : slide(s){};
double area() const
{
return slide * slide;
}
};

// 定义梯形
class Trapezoid : public Shape
{
private:
double a, b, h;

public:
Trapezoid(double i, double j, double k) : a(i), b(j), h(k){};
double area() const
{
return ((a + b) * h / 2);
}
};

// 定义三角形
class Triangle : public Shape
{
private:
double w, h;

public:
Triangle(double i, double j) : w(i), h(j){};
double area() const
{
return (w * h / 2);
};
};
// 主函数
int main()
{
Shape *p[5];
Square se(5);
Trapezoid td(2, 5, 4);
Triangle te(5, 8);

p[0] = &se;
p[1] = &td;
p[2] = &te;

double da = 0;
for (int i = 0; i < 3; i++)
{
da += p[i]->area();
};
cout << "总面积是:" << da << endl;

return 0;
};