Search code examples
c++operator-overloadingoverloadingostream

Child Class Operator << overload C++


How can i overload the operator << so it can show the attribute of the parent class as well when those attribute are private withing the parent class??

Parent Class: Movie header

ifndef _Movie
#define _Movie
#include <string>
using namespace std;
Class Movie{
private:
       string title;
       int year;
       float duration;
public: 
       Movie();
       Movie(string, int, float);
       Movie(const Movie&);
      
    void Print();
};
#endif

Movie.cc

#include "Movie.h"
#include<iostream>
Movie::Movie(){
    std::cout<< "Defaut Constructor" <<std::endl;
}

Movie::Movie(string t, int a, float d){
    this->title = t;
    this->year = a;
    this->duration = d;
}

Movie::Movie(const Movie &M){
    std::cout << "copy" << std::endl;
    this->title = M.title;
    this->year = M.year;
    this->duration = M.duration;

void Movie::Print(){
    std::cout << "Info" << std::endl;
    std::cout << "------------------" << std::endl;
    std::cout << "Title: " << title <<std::endl;
    std::cout << "Year: " << year <<std::endl;
    std::cout << "Duration: " << duration <<std::endl;
    std::cout << "------------------" << std::endl;
    if (duration>=60){
        std::cout << "Long Movie" << std::endl;
    }
    else{
        std::cout << "Short Movie" << std::endl;
    }
}

Child Class:

Prize header:

#ifndef _Prize
#define _Prize
#include "Movie.h"
#include <iostream>
using namespace std;

class Prize : public Movie{
private :
    int oscar; 
    bool por;
public:
    Prize(); 
    Prize(string, int, float, int, bool);
    Prize(const Prize &);

    friend ostream& operator<<(ostream& out,const Prize&f);
};
#endif

prize cc

#include "Prize.h"
#include<iostream>


Prize::Prize()
{
cout<<"Defaut Constructor Prize"<<endl;
}

Prize::Prize(string t, int a, float d, int c, bool p):Movie(t,a,d) //inherite t,a,d from the mother class
{
    this->oscar = c;
    this->por = p;
}

Prize::Prize(const Prize &f):Movie(f)
{
    this->oscar = f.oscar;
    this->por = f.por;
}

Here i need to show the attribute of the parent class as well i can't really add Movie::Print() and i can't do f.title because it's private within the Movie class

std::ostream& operator<<(std::ostream& out,const Prize& f){
    // Movie::print;
    // out << f.title << std:endl;
    out << f.cesar <<std::endl;
    out << f.por << std::endl;
    return out;
}

Solution

  • My recommendation is that you instead make an operator<< function for the base class. This operator then calls a virtual "output" function to do the actual output.

    Each child-class then overrides this output function to output its own data, and call the base-class output function to let it print its own data.

    For your classes it could be something like this:

    class Movie
    {
    private:
        std::string title;
    
    protected:
        virtual std::ostream& output(std::ostream& out) const
        {
            return out << title;
        }
    
    public:
        // Other public members...
    
        friend std::ostream& operator<<(std::ostream& out, Movie const& movie)
        {
            return movie.output(out);
        }
    };
    
    class Prize : public Movie
    {
    protected:
        std::ostream& output(std::ostream& out) const override
        {
            return Movie::output(out) << ' ' << cesar << ' ' << por;
        }
    
        // Other public and private members...
    };