C++ programming chapter 10 after-school exercises answers - operator overloading exercises

Table of contents

1. The method of operator overloading

1. Operator overloading functions as class member functions

2. Operator overloading functions as friend functions

2. Overloading the stream insertion operator "<<" and the stream extraction operator ">>"

1. Overloaded stream insertion operator "<<"

2. Overloaded stream extraction operator ">>"

3. Type conversion between different data types

1. Define a complex class Complex and overload the operator "+" so that it can be used for the addition of complex numbers. Overload operator functions as non-member, non-friend ordinary functions. Write a program to find the sum of two complex numbers.

2. Define a complex number class Complex, overload operators "+", "-", "*", "/", so that it can be used for addition, subtraction, multiplication, and division of complex numbers. Operator overloading functions as member functions of the Complex class. Write a program to calculate the sum, difference, product and quotient of two complex numbers respectively.

3. Define a complex class Complex and overload the operator "+" so that it can be used for complex wig operations. The two operands participating in the operation can both be class objects, or one of them can be an integer, and the order is arbitrary. Write a program to find the sum of two complex numbers and the sum of an integer and a complex number respectively.

4. There are two matrices a and b, both of which are 2 rows and 3 columns. Find the sum of two matrices. Operator "+" is overloaded so that it can be used for matrix addition.

5. There are two matrices a and b, both of which are 2 rows and 3 columns. Find the sum of two matrices. Overload the operator "+" so that it can be used for matrix addition, such as c=a+b. Overload the stream insertion operator "<<" and the stream extraction operator ">>" to work with the input and output of this matrix.

6. Please write a program to process the operation of adding a complex number and a double number, store the result in a double variable d1, output the value of d1, and then output this value in the form of a complex number. Define the Complex class and include overloaded type conversion operators in member functions: operator double(){return real;}

7. Define a teacher (teacher) class and a student (student) class, both of which have the same data members, such as num, name, sex. Write a program to convert a student object (student) into a teacher (teacher) class, and only transplant the above three same data members. It can be imagined as follows: a student has graduated from university and stays as a teacher at the school. Part of his original data is still useful for the current teacher status, and should be retained and become part of his teacher's data.


1. The method of operator overloading

1. Operator overloading functions as class member functions

  • Define class operator+(Define class &);
  • When c1+c2 is interpreted by the compiler as c1.operator+(c2)
  • If operator overloading is used as a member function, it can freely access the data members of this class through the address of this pointer, so one less function parameter can be written.

2. Operator overloading functions as friend functions

  • friend definition class operator+(definition class &, definition class &);
  • When c1+c2 is interpreted by the compiler as operator+(c1,c2)
  • When the binocular operator is overloaded as a friend function, since the friend function is not a member function of this class, there must be two parameters in the formal parameter list of the function, which cannot be omitted.

2. Overloading the stream insertion operator "<<" and the stream extraction operator ">>"

1. Overloaded stream insertion operator "<<"

  • friend istream& operator>>(istream&,Arr&);

2. Overloaded stream extraction operator ">>"

  • friend ostream& operator<<(ostream&,Arr&);

3. Type conversion between different data types

1. Conversion constructor

  • Complex(double r){real=r;imag=0;}
  • A converting constructor can only have one parameter. If it takes more than one parameter, it's not a converting constructor.

2. Type conversion function

  • operatorTypeName() {statement to implement the conversion}
  • The type conversion function can only be used as a member function, because the subject of the conversion is the object of this class. Cannot be used as a friend function or a normal function.

1. Define a complex class Complex and overload the operator "+" so that it can be used for the addition of complex numbers. Overload operator functions as non-member, non-friend ordinary functions. Write a program to find the sum of two complex numbers.

#include <iostream>
using namespace std;
//习题 1
class Complex{
public:
    Complex(){real=0;imag=0;}
    Complex(double r,double i){real=r;imag=i;}
    double get_real(){return real;}
    double get_imag(){return imag;}
    void display();
private:
    double real;
    double imag;
};


void Complex::display() {
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
Complex operator + (Complex &c1,Complex &c2)
{
    return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}

int main(){
    Complex c1(3,1),c2(2,-7),c3;
    cout<<"c1=";c1.display();
    cout<<"c2=";c2.display();
    cout<<"c1+c2=";c3.display();
}

2. Define a complex number class Complex, overload operators "+", "-", "*", "/", so that it can be used for addition, subtraction, multiplication, and division of complex numbers. Operator overloading functions as member functions of the Complex class. Write a program to calculate the sum, difference, product and quotient of two complex numbers respectively.

//习题 2
class Complex{
public:
    Complex(){real=0;imag=0;}
    Complex(double r,double i){real=r;imag=i;}
    Complex operator+(Complex &c2);
    Complex operator-(Complex &c2);
    Complex operator*(Complex &c2);
    Complex operator/(Complex &c2);
    void display();
private:
    double real;
    double imag;
};

Complex Complex::operator+(Complex &c2) {
    return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator-(Complex &c2) {
    return Complex(real-c2.real,imag-c2.imag);
}
Complex Complex::operator*(Complex &c2) {
    return Complex(real*c2.real-imag*c2.imag,imag*c2.real+real*c2.imag);
}
Complex Complex::operator/(Complex &c2) {
    return Complex((real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(imag*c2.real+real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));
}

void Complex ::display() {
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}

3. Define a complex class Complex and overload the operator "+" so that it can be used for complex wig operations. The two operands participating in the operation can both be class objects, or one of them can be an integer, and the order is arbitrary. Write a program to find the sum of two complex numbers and the sum of an integer and a complex number respectively.

//习题 3
class Complex{
public:
    Complex(){real=0;imag=0;}
    Complex(double r,double  i){real=r;imag=i;}
    friend Complex operator+(Complex &c1,Complex &c2);
    friend Complex operator+(int &i,Complex &c1);
    friend Complex operator+(Complex &c1,int &i);
    void display();
private:
    double real;
    double imag;
};

Complex operator+(Complex &c1,Complex &c2){
    return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator+(Complex &c1,int &i){
    return Complex(c1.real+i,c1.imag);
}
Complex operator+(int &i,Complex&c1){
    return Complex(i+c1.real,c1.imag);
}
void Complex::display() {
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}

int main(){
    Complex c1(2,3),c2(2,9),c3;
    int i=2;
    cout<<"c1=";c1.display();
    cout<<"c2=";c2.display();
    c3=c1+c2;
    cout<<"c1+c2=";c3.display();
    c3=i+c1;
    cout<<"i+c1=";c3.display();
    c3=c1+i;
    cout<<"c1+i=";c3.display();
}

4. There are two matrices a and b, both of which are 2 rows and 3 columns. Find the sum of two matrices. Operator "+" is overloaded so that it can be used for matrix addition.

//习题 4
#include <iomanip>
class Arr{
public:
    Arr();
    friend Arr operator+(Arr &a1,Arr &a2);
    void inp();
    void out();
private:
    int arr[2][3];
};
Arr::Arr() {
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 3; ++j)
            arr[i][j]=0;
}
Arr operator+(Arr&a1,Arr&a2){
    Arr a3;
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 3; ++j)
            a3.arr[i][j]=a1.arr[i][j]+a2.arr[i][j];
    return a3;
}
void Arr::inp() {
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 3; ++j)
            cin>>arr[i][j];
}
void Arr::out() {
    for (int i = 0; i < 2; ++i){
        for (int j = 0; j < 3; ++j)
            cout << setw(4) << arr[i][j];
        cout<<endl;
    }
}

int main(){
    Arr a1 ,a2, a3;
    a1.inp();
    a2.inp();
    a3=a1+a2;
    a3.out();

}
/*

1 2 3 4 5 6
1 2 3 4 5 6
   2   4   6
   8  10  12
*/

5. There are two matrices a and b, both of which are 2 rows and 3 columns. Find the sum of two matrices. Overload the operator "+" so that it can be used for matrix addition, such as c=a+b. Overload the stream insertion operator "<<" and the stream extraction operator ">>" to work with the input and output of this matrix.

//习题 5
#include <iomanip>
using namespace std;
class Arr {
public :
    friend ostream& operator<<(ostream&,Arr&);
    friend istream& operator>>(istream&,Arr&);
    friend Arr operator + (Arr & c1,Arr & c2);
    Arr();
private :
    int Q[2][3];
};
Arr::Arr() {
    for(int i=0; i<2; i++)
        for(int j=0; j<3; j++)
            Q[i][j]=0;
}
ostream & operator <<(ostream & output,Arr& a) {
    for (int i=0; i<2; i++) {
        for (int j=0; j<3; j++)
            output<<setw(4)<<a.Q[i][j];
        output<<endl;
    }
    return output;
}
istream & operator >>(istream & input,Arr& a) {
    for (int i=0; i<2; i++)
        for (int j=0; j<3; j++)
            input>>a.Q[i][j];
    return input;
}
Arr operator +(Arr & a1,Arr & a2) {
    Arr a3;
    for (int i=0; i<2; i++)
        for (int j=0; j<3; j++)
            a3.Q[i][j]=a1.Q[i][j]+a2.Q[i][j];
    return a3;
}
int main() {
    Arr a1,a2,a3;
    cin>>a1>>a2;
    a3=a1+a2;
    cout<<a3;
    return 0;
}

6. Please write a program to process the operation of adding a complex number and a double number, store the result in a double variable d1, output the value of d1, and then output this value in the form of a complex number. Define the Complex class and include overloaded type conversion operators in member functions: operator double(){return real;}

//习题 6
class Complex{
public:
    Complex(){real=0;imag=0;}
    Complex(double r,double i){real=r;imag=i;}
    Complex(double d1){real=d1;imag=0;}
    operator double (){
    
    return real;}
    void out(){cout<<real<<"+"<<imag<<"i";}
private:
    double real;
    double imag;
};

int main(){
    Complex c1(2,3),c2;
    double d1;
    d1=2.5+c1;
    cout<<d1<<endl;
    c2=Complex(d1);
    c2.out();
    return 0;
}

7. Define a teacher (teacher) class and a student (student) class, both of which have the same data members, such as num, name, sex. Write a program to convert a student object (student) into a teacher (teacher) class, and only transplant the above three same data members. It can be imagined as follows: a student has graduated from university and stays as a teacher at the school. Part of his original data is still useful for the current teacher status, and should be retained and become part of his teacher's data.

//习题 7
class Student{
public:
    Student(){num=10;name="王";sex='M';}
    void display(){
        cout<<num<<endl;
        cout<<name<<endl;
        cout<<sex<<endl;
    }
private:
    int num;
    string name;
    char sex;
};
class Teacher:public Student{};

int main(){
    Teacher t1;
    t1.display();
}

Guess you like

Origin blog.csdn.net/qq_21402983/article/details/127717501
Recommended