How can i present printing for my class fred?, C/C++ Programming

A: Use operator overloading to present a friend left-shift operator, operator<<.

#include class Fred {

public:

friend std::ostream& operator<< (std::ostream& o, const Fred& fred);

... private:

int i_; // only for illustration

};

std::ostream& operator<< (std::ostream& o, const Fred& fred)

{

return o << fred.i_;

}

int main()

{

Fred f;

std::cout << "My Fred object: " << f << "\n";

...

}

We employ a non-member function (a friend in this case) as the Fred object is the right-hand operand of the << operator. If the Fred object was imagined to be on the left hand side of the << (i.e., myFred << std::cout instead of std::cout << myFred), we could have utilized a member function named operator<<.

Note down that operator<< returns the stream. It is so the output operations can be cascaded.

 

Posted Date: 3/20/2013 8:14:22 AM | Location : United States







Related Discussions:- How can i present printing for my class fred?, Assignment Help, Ask Question on How can i present printing for my class fred?, Get Answer, Expert's Help, How can i present printing for my class fred? Discussions

Write discussion on How can i present printing for my class fred?
Your posts are moderated
Related Questions
Write a program that does the following: Calculates the Velocity and Momentum of an object. The formula for the velocity is V=d/t and the formula Momentum is m=mass*velocity. Your

c program to count the number of occurances of the string in padovan''s string

Write a function to determine whether a number is prime: it will return true if the input is prime and false otherwise. Use it to see whether -7 is prime.

Problems With Multiple Inheritance The following example presents a problem with multiple inheritance. class Aclass  {   public :  void put()

C Program for FUNCTION OF COUNT THE CHARACTERS IN EACH WORD void count(char c[]); void main() {           char a[50];           int i=0;           clrscr();

C Program for 5 FUNCTION OF VOWELS, CNT_WORDS, REVERSE void input(char a[]); void output(char a[]); void reverse(char a[], char b[]); char poli(char a[], char b[]);

color representation 0,1,2,3,4,5,6,7...

w.a.p to find outWrite 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.

Implement (a part of) electronic election Scenario where following political parties are participating in election: PPP, PML and PTI.  Create a class PollingStation where it stor

Use the above problem and have the program print the biggest and smallest sales for each employee for everyday of the month. Program should also find the largest and