lundi 20 juin 2022

using c++ overloaded operator function as member function?

From c++ how to program 10th edition by deitel


//Fig. 10.3: PhoneNumber.h
//PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <iostream>
#include <string>

class PhoneNumber
{
    friend std::ostream& operator<<(std::ostream&, const PhoneNumber&);
    friend std::istream& operator>>(std::istream&, PhoneNumber&);

private:
    std::string areaCode; //3-digit area code
    std::string exchange; //3-digit exchange
    std::string line; //4-digit line
};
#endif // PHONENUMBER_H
//Fig. 10.4: PhoneNumber.cpp
//Overloaded stream insertion and stream extraction operators
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;

//overloaded stream insertion operator; cannot be a member function
//if we would like to invoke it with cout << somePhoneNumber;

ostream& operator<<(ostream& output, const PhoneNumber& number)
{
    output << "Area code: " << number.areaCode << "\nExchange: "
        << number.exchange << "\nLine: " << number.line << "\n"
        << "(" << number.areaCode << ") " << number.exchange << "-"
        << number.line << "\n";
    return output; //enables cout << a << b << c;
}

//overloaded stream extraction operator; cannot be a member function
//if we would like to invoke it with cin >> somePhoneNumber;
istream& operator>>(istream& input, PhoneNumber& number)
{
    input.ignore(); //skip (
    input >> setw(3) >> number.areaCode; //input area code
    input.ignore(2); //skip ) and space
    input >> setw(3) >> number.exchange; //input exchange
    input.ignore(); //skip dash (-)
    input >> setw(4) >> number.line; //input line
    return input; //enables cin >> a >> b >> c
}

//Fig. 10.5: fig10_5.cpp
//Demonstrating Class PhoneNumber's overloaded stream insertion
//and stream extraction operators.

#include <iostream>
#include "PhoneNumber.h"
#include <string>

using namespace std;

int main()
{
    PhoneNumber phone; //create object phone

    cout << "Enter phone number in the form (555) 555-5555:" << endl;

    //cin >> phone invokes operator>> by implicitly issuing
    //the non-member function call operator>>(cin, phone)
    cin >> phone;

    //cout << phone invokes operator<< bby implicitly issuing
    //the non-member function call operator<<(cout, phone)
    cout << phone << endl;
}

I understand from the book that the overloaded operator function for binary operator can be member function only when the left operand is an object of the class in which the function is a member because the member function can only be accessed by left operand which is the object of the class.

however I don't understand this part

" The overloaded stream insertion operator (<<) is used in an expression in which the left operand has type ostream&, as in cout << classObject. To use the operator in this manner where the right operand is an object of a user-defined class, it must be overloaded as a non-member function. To be a member function, operator << would have to be a member of class ostream. This is not possible for user-defined classes, since we are not allowed to modify C++ Standard Library classes. "

my question is

  1. whether the stream extraction and insertion operator is part of the ostream and istream respectively?
  2. what does the part highlighted in bold mean?

My guess is that we cannot add user defined function into the C++ Standard Library class but the part in bold makes me kind of confused. do we have to add the operator << as member of class ostream to make the overloaded operator function as member function of ostream?

I always thought that stream << and >> operator were part of ostream and istream respectively.

Correct me if I am wrong. thank you in advance.

Aucun commentaire:

Enregistrer un commentaire