How can i provide printing for whole hierarchy of classes?, C/C++ Programming

Assignment Help:

A: Provide a friend operator<< which calls a protected virtual function

class Base {

public:

friend std::ostream& operator<< (std::ostream& o, const Base& b);

... protected:

virtual void printOn(std::ostream& o) const;

};

inline std::ostream& operator<< (std::ostream& o, const Base& b)

{ b.printOn(o); return o;

}

class Derived : public Base {

protected:

virtual void printOn(std::ostream& o) const;

};

The ending result is that operator<< acts as if it were bound dynamically, even though it's a friend function. It is called the Virtual Friend Function Idiom.

Note down that derived classes override printOn(std::ostream&) const. Particularly, they do not provide their own operator<<.

Of course if Base is an ABC, Base::printOn(std::ostream&) const may be declared pure virtual using the "= 0" syntax.

!    

 


Related Discussions:- How can i provide printing for whole hierarchy of classes?

Why do c++ compilers require name mangling?, A: Name mangling is the rule a...

A: Name mangling is the rule according to which C++ modify function's name into function signature before passing that function to a linker. It is how the linker differentiates amo

Help, Deliverables: you are required to upload your c code in the assignmen...

Deliverables: you are required to upload your c code in the assignment dropbox set in Moodle. You are supposed to work with Linux gcc compiler and pico editor for compiling via the

Boolean functions and simple logic design, The digital signal is one which ...

The digital signal is one which only consists of two states i.e. logic '1'   (+5 volts) and logic '0' (0 volts). Various electronic blocks use logic and these form the basis of a m

Windows object code copy minder defeat, Project Description: We own prop...

Project Description: We own proprietary software which long ago had Copyminder protection added. We no longer have the source code or a relationship with the original coder and

Explain call by reference, Call by Reference Passing variables(paramete...

Call by Reference Passing variables(parameters) to a function in C can be done in two ways - pass by value, also called as call by value and pass by address or also known as ca

201 it, overloadstream insertion opertator to display the data of object on...

overloadstream insertion opertator to display the data of object on the console

Can you overload a function depend only on whether parameter, Can you overl...

Can you overload a function depend only on whether a parameter is a value or a reference? A: No. Passing by value and reference looks identical to the caller.

Write a program that finds the minimum total number of shelv, Write a progr...

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.

Verifone pos offline mod vx670, Verifone pos offline mod vx670 Project D...

Verifone pos offline mod vx670 Project Description: I want a programmer who will build custom application for Offline VX670 to collect and save info+pin 1) Press the am

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd