Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:51:12+00:00 2026-06-01T10:51:12+00:00

Class DVD inherits class Media, has one more variable than the base class does.

  • 0

Class DVD inherits class Media, has one more variable than the base class does.

I declare a pointer:

Media* ptr = new DVD(...);

I want to print out the content of the DVD, so the following code works as expected:

ptr->print(cout);

But using the overloaded << operator only invokes the base class print() function:

cout << *ptr << endl;

So it prints out only the ID, not with the name of the director.

One way to solve this problem is to modify the overload << operator a bit to make it accept pointers, so:

cout << ptr << endl;

should work, but somehow I must find the way to make cout << *ptr << endl; work as expected.

Any advice?

The thing is I can’t make the base class (Media) abstract because I need to call an instance of it in the overloading ostream operator, so a pointer of the base class can not invoke the overloading function of the derived class, which it’s pointing to.

Code:

#include <iostream>
using namespace std;

class Media{
    private:
        int id;
    public:
        Media(int _id) : id(_id) {}
        virtual ~Media();
        virtual void print(ostream &out);
        friend ostream& operator << (ostream& out, Media aMedia);
};
Media::~Media(){}

class DVD : public Media {
    private:
        string director;
    public:
        DVD(int _id, string _director = "unknown") : Media(_id), director(_director) {}
        ~DVD();
        void print(ostream &out);
};
DVD::~DVD(){}

void Media::print(ostream& out){
    out << "ID " << id;
}
void DVD::print(ostream& out){
    out << "DVD: ";
    Media::print(out);
    out << " Directed by " << director;
}
ostream& operator << (ostream& out, Media aMedia){
    aMedia.print(out);
    return out;
}

int main() { 
    Media *ptr = new DVD(352, "Stephen Spielberg"); 
    ptr->print(cout); // Prints out: "DVD: ID 352 Directed by Stephen Spielberg". Correct!
    cout << endl; 
    cout << *ptr << endl; //Prints out: "ID 352" Incorrect!
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:51:13+00:00Added an answer on June 1, 2026 at 10:51 am

    Problem is in this declaration ostream& operator << (ostream& out, Media aMedia). You are accepting the parameter aMedia by copy which causes object slicing, accept it by using a reference by changing the signature to ostream& operator << (ostream& out, const Media& aMedia) .

    Because of the slice when you do cout << *ptr , a copy of the DVD is created of type Media (i.e. DVD is sliced to a Media), now when you call print since the type of the object is Media call goes to the Media::print. You can read more about object slicing here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class Media and several derived classes, namely DVD , Book
Does anyone know of a class/component FreeWare/ShareWare/BuyWare that can investigate a DVD structure (and
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end In the above, what
I have two Java interfaces and one implementing class. (I have used Eclipse to
class ABC is an abstract base class. class X is its subclass. There's some
class Object { }; Class Derived : public Object { }; boost::shared_ptr<Object> mObject(new Derived);
class Client < ActiveRecord::Base has_many :hosts, :dependent => :destroy end class Host < ActiveRecord::Base
I have 4 models: transac, transac_data, item, dvd_details class Transac < ActiveRecord::Base has_many :transac_datas

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.