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?

C, minimum total number of shelves.

minimum total number of shelves.

Integration, Write a program to find the area under the curve y = f(x) betw...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Describe what is a listener in java, Problem: (a) Show the Java approac...

Problem: (a) Show the Java approach to event processing by explaining how event handling works in Java. Make use of a suitable example to support your answer. (b) Describe

C program for even & odd no in any matrices , C Program for EVEN & ODD NO I...

C Program for EVEN & ODD NO IN ANY MATRICES #include stdio.h> #include conio.h> void main() {           int a[100][100];           int i=0,j=0,r,c,even=0,odd=0;

Define global variable in c++ program, Define global variable in c++ progra...

Define global variable in c++ program: How to define a global variable and need of global variable in c++ program. int main() {    int m=20;    clrscr();    for

Function, give an example of function

give an example of function

Inheritance, example of program to add two number

example of program to add two number

Program with various inputs-set associative cache , 1.1 A Few Notes: 1....

1.1 A Few Notes: 1. Please test your program with various inputs prior to submission. 2. All group members must understand the entire project for interactive grading. Equal

Draw a picture using line - c program, Draw a picture using line: void...

Draw a picture using line: void main(void) {     int   driver = DETECT,mode;     int   x[10],y[10];     int   x_center = 360, y_center = 180, rad = 100;     int

Explain the goto statement, The goto statement This statement can be us...

The goto statement This statement can be used to branch to another statement of the program. This is rarely used as it violates the principle of structured programming. Though

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