An example of object-oriented technology topic classification-2 blog of zam9036

Programing language

C++

Inheritance and Derive

11. Constructor of multiple inheritance derived class

Problem description

Declare a teacher class and a student class, and declare a graduate derived class with public multiple inheritance, including private member waves. The teacher class contains the protection data members name (name), age (age), title (title). The student class includes the protection data members name1 (name), sex, score. Give the initialized data when defining the derived class object, and then output the data.

It is required that the data members of the Teacher and Student classes are initialized in their own constructors. When creating a new grade object, grade should call the constructors of Teacher and Student and others in turn through its own constructors.

Some codes have been given below. Please complete the existing codes. It is required that the existing codes cannot be changed.

int main() {
    Graduate grad1("Johnson", 21, "male", "assistant", 90, 7800);
    grad1.show();
    return 0;
}

Reference code
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
    Teacher(string nam, int a, string tit) : name(nam), age(a), title(tit) {}

protected:
    string name;
    int age;
    string title;
};
class Student
{
public:
    Student(string nam, string s, int sco) : name1(nam), sex(s), score(sco) {}

protected:
    string name1;
    string sex;
    int score;
};
class Graduate : public Teacher, public Student
{
public:
    Graduate(string nam, int a, string s, string tit, int sco, int w) : Teacher(nam, a, tit), Student(nam, s, sco) { wages = w; }
    void show()
    {
        cout << "name:" << name << "age:" << age << "sex:" << sex << "score:" << score << "title:" << title << "wages:" << wages;
    }

private:
    int wages;
};
int main()
{
    Graduate grad1("Johnson", 21, "male", "assistant", 90, 7800);
    grad1.show();
    return 0;
}

12. Constructor of a derived class with child objects

Problem description

There is an existing Student class, which contains two protection members, num and name, representing Student number and name respectively; there is another Student1 class, which inherits from the Student class publicly, and contains a monitor of private Student type and a private age.

It is required to construct a stu object of Student1 type. The monitor object must be initialized in the constructor of Student1 by calling the constructor of Student. Finally, show method is called by stu object to output stu Student number, name and age.

Some codes have been given below. Please complete the existing codes. It is required that the existing codes cannot be changed.

int main(){
    Student1 stu(20120107, "Johnson", 20);
    stu.show();
    return 0;
}

Reference code
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
    Student(int n, string nam) : num(n), name(nam) {}

protected:
    int num;
    string name;
};
class Student1 : public Student
{
public:
    Student1(int n, string nam, int a) : Student(n, nam), monitor(n, nam) { age = a; }
    void show()
    {
        cout << num << " " << name << " " << age << endl;
    }

private:
    Student monitor;
    int age;
};
int main()
{
    Student1 stu(20120107, "Johnson", 20);
    stu.show();
    return 0;
}

operator overloading

13. Using the transformation constructor to implement the transformation between two objects

Problem description

Define a Teacher class, including private attribute number (num), name (name), gender (sex), title (title);

A Student class, including public attribute number (num), name (name), gender (sex), grade,

Some of the data members are the same: num, name, sex.

Write a program, use the conversion constructor to convert a Student object to a Teacher object, and only migrate the number (num), name (name), sex (sex) of the Student object to the Teacher object.

Some codes have been given below. Please complete the existing codes. It is required that the existing codes cannot be changed.

int main()
{
       Student s(20160103, "Johnson", "male", 100);
       Teacher t = s;
       t.display();
       return 0;
}

Reference code
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
    Student(int a, string b, string c, int d) : num(a), name(b), sex(c), grade(d) {}
    int num;
    string name;
    string sex;
    int grade;
};

class Teacher
{
public:
    Teacher(int a, string b, string c, string d) : num(a), name(b), sex(c), title(d) {}
    Teacher(Student &s)
    {
        num = s.num;
        name = s.name;
        sex = s.sex;
    }
    void display()
    {
        cout << num << " " << name << " " << sex << endl;
    }

private:
    int num;
    string name;
    string sex;
    string title;
};

int main()
{
    Student s(20160103, "Johnson", "male", 100);
    Teacher t = s;
    t.display();
    return 0;
}

14. Overloaded + operator and input / output operators > > and < < for matrix operation

Problem description

A matrix class matrix is established, which contains a private data member named mtx, an integer two-dimensional array, which can store three rows and three columns. Overloaded I / O operators > > and < < enable the program to read in 2 matrices of 3 rows and 3 columns from the keyboard. Overload + operator, so that two matrices can be added to return a result matrix, and finally output the result matrix.

Reference code
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
class matrix
{
private:
    int mtx[3][3];

public:
    friend istream &operator>>(istream &in, matrix &a)
    {
        in >> a.mtx[0][0] >> a.mtx[0][1] >> a.mtx[0][2] >> a.mtx[1][0] >> a.mtx[1][1] >> a.mtx[1][2] >> a.mtx[2][0] >> a.mtx[2][1] >> a.mtx[2][2];
        return in;
    }
    friend ostream &operator<<(ostream &ou, matrix &a)
    {
        ou << a.mtx[0][0] << " " << a.mtx[0][1] << " " << a.mtx[0][2] << "\n"
           << a.mtx[1][0] << " " << a.mtx[1][1] << " " << a.mtx[1][2] << "\n"
           << a.mtx[2][0] << " " << a.mtx[2][1] << " " << a.mtx[2][2];
        return ou;
    }
    friend matrix operator+(matrix &a, matrix &b)
    {
        matrix c;
        c.mtx[0][0] = a.mtx[0][0] + b.mtx[0][0];
        c.mtx[0][1] = a.mtx[0][1] + b.mtx[0][1];
        c.mtx[0][2] = a.mtx[0][2] + b.mtx[0][2];
        c.mtx[1][0] = a.mtx[1][0] + b.mtx[1][0];
        c.mtx[1][1] = a.mtx[1][1] + b.mtx[1][1];
        c.mtx[1][2] = a.mtx[1][2] + b.mtx[1][2];
        c.mtx[2][0] = a.mtx[2][0] + b.mtx[2][0];
        c.mtx[2][1] = a.mtx[2][1] + b.mtx[2][1];
        c.mtx[2][2] = a.mtx[2][2] + b.mtx[2][2];
        return c;
    }
};
int main()
{
    matrix a, b, c;
    cin >> a >> b;
    c = a + b;
    cout << c;
    return 0;
}

15. Add a double data and Complex data

Problem description

It is required to complete the following code so that it can run and output correct results. It is required to use the type conversion function, and pay attention not to change the existing code.

int main() {
    Complex c1(3, 4), c2(5, -10), c3;
    double d;
    d = 2.5 + c1;
    cout << d << endl;
    return 0;
}

Reference code
#include <iostream>
#include <string>
using namespace std;

class Complex
{
private:
    double real, imag;

public:
    Complex(double a, double b) : real(a), imag(b)
    {
    }
    Complex() : real(0), imag(0)
    {
    }
    operator double()
    {
        return real;
    }
};

int main()
{

    Complex c1(3, 4), c2(5, -10), c3;

    double d;

    d = 2.5 + c1;

    cout << d << endl;

    return 0;
}

16. Define complex class and use operator overloading to realize its function

Problem description

Define a Complex class Complex, overload operators -, /, so that it can be used for subtraction and division of Complex. As a friend function of Complex class, the overloaded function of operator calculates the difference sum quotient of two Complex numbers.

Some codes have been given below. Please complete the existing codes. It is required that the existing codes cannot be changed.

int main()
{
//...
    c3=c1-c2;
    cout<<c3;
//...
   c4=c1/c2;                            
    cout<<c4;
//...
}

Reference code
#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);
        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, c4;
    c3 = c1 - c2;
    cout << c3;
    c4 = c1 / c2;
    cout << c4;
}

17. Define complex class and use operator overloading to realize its function

Problem description

Define a Complex class, overload operator *, so that it can be used for Complex multiplication. As a friend function of Complex class, the overloaded function of operator finds the product of two Complex numbers.

Reference code
#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;
}

18. Define complex class and use operator overloading to realize its function

Problem description

Define a Complex class, overload operator +, so that it can be used for Complex addition. As a friend function of Complex class, the overloaded function of operator can find the sum of two Complex numbers.

Reference code
#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;
}

19. Define complex class and use operator overloading to realize its function

Problem description

Define a Complex class, overload operators -, /, so that it can be used for subtraction and division of Complex. As a member function of Complex class, the overloaded function of operator calculates the difference sum quotient of two Complex numbers.

Some codes have been given below. Please complete the existing codes. It is required that the existing codes cannot be changed.

int main()
{
//...
    c3=c1-c2;
    cout<<c3;
//...
   c4=c1/c2;                            
    cout<<c4;
//...
}

Reference code
#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, c4;
    c3 = c1 - c2;
    cout << c3;
    c4 = c1 / c2;
    cout << c4;
}

20. Define complex class and use operator overloading to realize its function

Problem description

Define a Complex class, overload operator *, so that it can be used for Complex multiplication. As a member function of Complex class, the overloaded function of operator calculates the product of two Complex numbers.

Reference code
#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;
}

Original statement

Author: Zam9036

Article link: https://zam9036.gitee.io/2019/11/06/13-Object-oriented-technical-problem-classification-example-2

Copyright notice: except for the special notice, all articles in this blog adopt CC BY-NC-SA 4.0 license agreement . Reprint please indicate from Blog of Zam9036!

Published 13 original articles, won praise 1, visited 64
Private letter follow

Keywords: Attribute

Added by voltrader on Fri, 28 Feb 2020 06:02:56 +0200