前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++常见面试题

C++常见面试题

作者头像
越陌度阡
发布2020-11-26 12:26:26
5150
发布2020-11-26 12:26:26
举报

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

代码语言:javascript
复制
#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实现

复数的加法。

代码语言:javascript
复制
#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 实现雇员的姓名、年龄、性别、部门和薪水的输出。(要求编写派生类的构造函数)

代码语言:javascript
复制
#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(),分别用来计算矩形和等腰三角形的面积。

代码语言:javascript
复制
#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. 用代码实以现以图案

代码语言:javascript
复制
*  *  *  *  *  *  *  *  *

   *  *  *  *  *  *  *

      *  *  *  *  *
        
         *  *  *

            *

代码如下:

代码语言:javascript
复制
#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(三角形)。用虚函数分别计算几种图形面积、并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象。

代码语言:javascript
复制
#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;
};

未完待续。。。。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档