面向对象技术题型归类示例-3|Zam9036博客

编程语言

C++

运算符的重载

21、定义复数类,并使用运算符重载来实现其功能
问题描述

定义一个复数类Complex,重载运算符+,使之能用于复数的加法。运算符的重载函数作为Complex类的成员函数,求两个复数的和。

输出样例21

参考代码
#include <iostream>
using namespace std;

class Complex
{
private:
    double real;
    double imag;

public:
    Complex()
    {
    }
    Complex(Complex &c1)
    {
        real = c1.real;
        imag = c1.imag;
    }
    Complex(double x, double y)
    {
        real = x;
        imag = y;
    }

    friend Complex operator+(Complex &c1, Complex &c2)
    {
        Complex a(c1.real + c2.real, c1.imag + c2.imag);
        return a;
    }
    friend Complex operator-(const Complex &c1, const Complex &c2)
    {
        Complex a(c1.real - c2.real, c1.imag - c2.imag);
        return a;
    }
    friend Complex operator*(const Complex &c1, const Complex &c2)
    {
        Complex a(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);
        return a;
    }
    friend Complex operator/(const Complex &c1, const Complex &c2)
    {
        if (c2.real == 0 && c2.imag == 0)
        {
            Complex a(0, 0);
            return a;
        }
        else
        {
            Complex a(((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag)), ((c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag)));
            return a;
        }
    }
    friend void operator+=(Complex &c1, Complex &c2)
    {
        c1.real = c1.real + c2.real;
        c1.imag = c1.imag + c2.imag;
    }
    friend ostream &operator<<(ostream &output, const Complex &c1)
    {
        if (c1.imag >= 0)
            output << c1.real << "+" << c1.imag << "i" << endl;
        else
            output << c1.real << c1.imag << "i" << endl;
        return output;
    }
};

int main()
{
    double a, b, c, d;
    cin >> a >> b >> c >> d;
    Complex c1(a, b), c2(c, d);
    Complex c3;
    c3 = c1 + c2;
    cout << c3;
}
22、重载运算符使之能进行复数的运算
问题描述

下面是一个用来表达复数的类Complex的部分代码.该类包含两个私有double型成员变量,分别代表复数的实部和虚部,默认为0。

class Complex {
private:
    double real;
    double img;
public:
//..
Complex operator + (const Complex & other) //….
};

请按要求补全该类其他代码。

该类需有完备的类构造函数,至少支持Complex a; Complex b(a); Complex c(4, 5); 这三种定义Complex类对象的方式。重载操作符+, +=, , 分别完成两个复数的加法和乘法,其中操作符的重载必须用友元函数的形式完成。

补全该类后,对于如下的main函数,要求其能通过编译,并正常运行。

int main () {
    Complex c2(2., 3.), c3 = c2 + Complex(1., 2.),c1(c2);
    Complex c4;
    c4 += c2 * c3 + c2;
    cout << c1 << c2 << c3 << c4;
    return 0;
}

输出样例22

参考代码
#include <iostream>
using namespace std;
class Complex
{
private:
    double real;
    double img;

public:
    Complex(double real = 0, double img = 0);
    Complex(const Complex &c2)
    {
        real = c2.real;
        img = c2.img;
    }
    Complex operator+(const Complex &other)
    {
        Complex c;
        c.real = real + other.real;
        c.img = img + other.img;
        return c;
    }
    Complex operator+=(const Complex &c1)
    {
        *this = *this + c1;
        return *this;
    }
    friend Complex operator*(Complex &c1, Complex &c2)
    {
        return Complex((c1.real * c2.real - c1.img * c2.img), (c1.img * c2.real + c1.real * c2.img));
    }
    friend ostream &operator<<(ostream &output, Complex &c)
    {
        if (c.img >= 0)
            output << c.real << "+" << c.img << "i" << endl;
        else
            output << c.real << c.img << "i" << endl;
        return output;
    }
};
Complex::Complex(double a, double b)
{
    real = a;
    img = b;
}
int main()
{
    Complex c2(2., 3.), c3 = c2 + Complex(1., 2.), c1(c2);
    Complex c4;
    c4 += c2 * c3 + c2;
    cout << c1 << c2 << c3 << c4;
    return 0;
}

类和对象的特性

23、将普通函数声明为友元并使用它输出日期与时间
问题描述

顺序定义时间类Time与日期类Date。其中,Time拥有私有数据hour, minute, second,以及友元函数display;Date拥有私有数据day, month, year,以及友元函数display。在类外定义display函数并实现它,display函数接收一个Time类和一个Date类的引用,并将其按照格式输出。 运行时依次从键盘输入day, month, year,hour, minute, second,最后调用display函数将其全部按格式输出。

输出样例23

参考代码
#include <iostream>
using namespace std;
class Date;
class Time
{
private:
    int hour, minute, second;

public:
    Time(int a, int b, int c)
    {
        hour = a;
        minute = b;
        second = c;
    }
    friend void display(Date &d, Time &t);
};
class Date
{
private:
    int day, month, year;

public:
    Date(int a, int b, int c)
    {
        day = a;
        month = b;
        year = c;
    }
    friend void display(Date &d, Time &t);
};

void display(Date &d, Time &t)
{
    cout << d.day << "/" << d.month << "/" << d.year << "" << t.hour << ":" << t.minute << ":" << t.second << endl;
}

int main()
{
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    Time t(d, e, f);
    Date dd(a, b, c);
    display(dd, t);
    return 0;
}
24、使用友元成员函数输出日期与时间
问题描述

顺序声明时间类Time与日期类Date。其中Time拥有私有数据hour, minute, second,以及公有函数display(接收一个Date的引用作为参数);Date拥有私有数据day, month, year,以及友元函数display(要求声明Time中的display函数为Date的友元成员函数)。运行时依次从键盘输入day, month, year,hour, minute, second,最后调用Time的display函数将其全部按格式输出。

输出样例24

参考代码
#include <iostream>
using namespace std;
class Date;
class Time
{
private:
    int hour, minute, second;

public:
    Time(int a, int b, int c)
    {
        hour = a;
        minute = b;
        second = c;
    }
    void display(Date &t);
};
class Date
{
private:
    int day, month, year;

public:
    Date(int a, int b, int c)
    {
        day = a;
        month = b;
        year = c;
    }
    friend void Time::display(Date &);
};

void Time::display(Date &t)
{
    cout << t.day << "/" << t.month << "/" << t.year << "" << hour << ":" << minute << ":" << second << endl;
}

int main()
{
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    Time t(d, e, f);
    Date dd(a, b, c);
    t.display(dd);
    return 0;
}
25、使用友元函数输出时间
问题描述

现有一时间类Time,拥有私有成员hour, minute, second,以及一友元函数display。display以引用的方式接收一个Time对象,并以hour:minute:second的格式来输出时间。

输出样例25

参考代码
#include <iostream>
using namespace std;

class Time
{
private:
    int hour, minute, second;

public:
    Time(int a, int b, int c)
    {
        hour = a;
        minute = b;
        second = c;
    }
    friend void display(Time &t)
    {
        cout << t.hour << ":" << t.minute << ":" << t.second;
    }
};

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    Time t(a, b, c);
    display(t);
    return 0;
}
26、使用指针传参找到成绩最高的学生
问题描述

现有一批学生(共5人,其类名为Student)的数据需要存放,包括私有数据成绩(名为score),数据从键盘输入。要求使用名为arr的对象静态数组存放这5个学生的数据,并使用名为sp的对象指针指向其对象数组的首地址(要求使用取地址符),最后将该指针传递给max函数(指针在max中的形参名为p),找出5个学生中成绩最高者并输出其成绩。

输出样例26

参考代码
#include <iostream>
using namespace std;

class Student
{
private:
    int score;

public:
    Student(int a)
    {
        score = a;
    }
    Student()
    {
        score = 0;
    }
    friend Student max(Student *p);
    void display()
    {
        cout << score;
    }
};

Student max(Student *p)
{
    Student s;
    for (int i; i < 5; i++, p++)
        if (s.score < p->score)
        {
            s = *p;
        }
    return s;
}

int main()
{
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    Student s1(a), s2(b), s3(c), s4(d), s5(e);
    Student arr[5];
    arr[0] = s1;
    arr[1] = s2;
    arr[2] = s3;
    arr[3] = s4;
    arr[4] = s5;
    Student *sp;
    sp = &arr[0];
    Student aa = max(sp);
    aa.display();
    return 0;
}
27、用指针输出学生信息
问题描述

现有一批学生(共5人,其类名为Student)的数据需要存放,包括私有数据成绩(名为score),数据从键盘输入。要求使用名为arr的对象数组(要求动态分配)存放这5个学生的数据,并使用名为sp的对象指针指向其对象数组的首地址(要求使用取地址符),随后有序输出第1、3、5个学生的数据。

输出样例27

参考代码
#include <iostream>
using namespace std;

class Student
{
private:
    int score;

public:
    Student(int a)
    {
        score = a;
    }
    Student()
    {
        score = 0;
    }
    void display()
    {
        cout << score << " ";
    }
};

int main()
{
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    Student s1(a), s2(b), s3(c), s4(d), s5(e);
    Student *arr = new Student[5];
    if (!arr)
        cout << "内存分配失败";
    else
    {
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;
        arr[3] = s4;
        arr[4] = s5;
        Student *sp;
        sp = &arr[0];
        sp->display();
        (sp + 2)->display();
        (sp + 4)->display();
        delete[] sp;
        sp = NULL;
    }
    return 0;
}
28、统计学生平均成绩,要求使用静态成员函数
问题描述

现有一批学生(其类名为Student)成绩(均为整数)需要统计,要求使用类内静态公有函数(函数名为average)求出这批学生的平均成绩(学生人数并不确定)。在输入成绩时,先输入一个整数,表示学生总人数,然后再输入相应个数学生成绩,并将这些数据都存入一个名为arr的数组中,该数组必须使用new动态开辟,最后输出学生的平均成绩。(结果要求保留1位小数,检测内存分配是否成功需使用NULL)

输出样例28

参考代码
#include <iostream>
#include <iomanip>
using namespace std;

class Student
{
private:
    int score;

public:
    Student(int a)
    {
        score = a;
    }
    Student()
    {
        score = 0;
    }
    int get()
    {
        return score;
    }
    static float average(int n, Student arr[])
    {
        int a;
        for (int i = 0; i < n; i++)
        {
            a += arr[i].get();
        }
        return (float)a / n;
    }
};

int main()
{
    int a, b;
    cin >> a;
    Student *arr = new Student[a];
    for (int i; i < a; i++)
    {
        cin >> b;
        arr[i] = Student(b);
    }
    if (arr == NULL)
        cout << "xx";
    cout << setiosflags(ios::fixed) << setprecision(1);
    cout << Student::average(a, arr);

    delete arr;
    return 0;
}
29、利用面向对象的思想求解长方体体积
问题描述

有一个长方体类,其类名为box, 其私有数据成员包括length(长)、width(宽)、height(高)以及一个名为volume的公有成员函数,volume函数用来计算长方体的体积。现有3个长方体,存储在一个名为arr的静态对象数组中,3个长方体的长、宽、高(三者均为整数)从键盘分别输入,要求使用名为bp的指针指向这个数组的首地址(要求使用数组名),并有序输出三个长方体的体积,体积的计算须使用volume函数。

输出样例29

参考代码
#include <iostream>
#include <iomanip>
using namespace std;

class box
{
private:
    int length, width, height;

public:
    box(int a, int b, int c) : length(a), width(b), height(c)
    {
    }
    box() : length(0), width(0), height(0)
    {
    }
    int volume()
    {
        return height * length * width;
    }
};
int main()
{
    int a, b, c, d, e, f, g, h, i;
    cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
    box a1(a, b, c), a2(d, e, f), a3(g, h, i);
    box arr[3];
    arr[0] = a1;
    arr[1] = a2;
    arr[2] = a3;
    box *bp = arr;
    cout << bp[0].volume() << " ";
    cout << bp[1].volume() << " ";
    cout << bp[2].volume() << " ";
    bp = NULL;
    return 0;
}
30、利用面向对象的思想求解长方体体积
问题描述

有一个长方体类,其类名为box, 其私有数据成员包括length(长)、width(宽)、height(高)以及一个名为volume的公有成员函数,volume函数用来计算长方体的体积。现有3个长方体,存储在一个名为arr的静态对象数组中,3个长方体的长、宽、高(三者均为整数)从键盘分别输入,要求有序输出三个长方体的体积。

输出样例30

参考代码
#include <iostream>
#include <iomanip>
using namespace std;

class box
{
private:
    int length, width, height;

public:
    box(int a, int b, int c) : length(a), width(b), height(c)
    {
    }
    box() : length(0), width(0), height(0)
    {
    }
    int volume()
    {
        return height * length * width;
    }
};
int main()
{
    int a, b, c, d, e, f, g, h, i;
    cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
    box a1(a, b, c), a2(d, e, f), a3(g, h, i);
    box arr[3];
    arr[0] = a1;
    arr[1] = a2;
    arr[2] = a3;
    box *bp;
    bp = &arr[0];
    cout << bp[0].volume() << " ";
    cout << bp[1].volume() << " ";
    cout << bp[2].volume() << " ";
    bp = NULL;
    return 0;
}

原创声明

​ 文章作者:Zam9036

​ 文章链接:https://zam9036.gitee.io/2019/11/20/17-Object-oriented-technical-problem-classification-example-3

​ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自Zam9036的博客

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值