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

Assignment Help:

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.

 


Related Discussions:- How can i present printing for my class fred?

Create a c project, The project will include Arena models (only .doe files)...

The project will include Arena models (only .doe files) and MSWord document for the report. Thereport must contain the following sections: 1. Introduction: (a)    Statemen

Luminous Jewels - Polishing Necklace, Byteland county is very famous for lu...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Sentinel controlled loop, develop an algorithm using pseudocode for computi...

develop an algorithm using pseudocode for computing cos(x) and sin(x). use a sentinel controlled while loop. use the series definition of e^+-jx

Destructor, Destructor: The purpose of destructor is to free the memory...

Destructor: The purpose of destructor is to free the memory when the compiler memory is reduced or not enough to execute certain program. Sometimes there may several objects op

First line starts with T, Input Format: First line starts with T, which is...

Input Format: First line starts with T, which is the number of test cases. Each test case contains a string (S). Output Format: For each test case print the minimum number of ch

Program of swapping in c++, Program of swapping two varibales: void sw...

Program of swapping two varibales: void swap(int *, int *);   // This is swap's prototype int main() {                 int x = 5, y = 7;                 swap(&x, &

Explain friend for overloading operators, Friend for Overloading Operators ...

Friend for Overloading Operators Sometimes friend functions cannot be avoided. For example with the operator overloading. Consider the following class that have data members to

C program to swap two numbers using reference arguments, Aim: To implement...

Aim: To implement a program to swap two numbers using reference arguments. Code: void swap(int *a, int *b) {             int temp;             temp=*a;

Write a program in c++ to read n numbers in an array, Problem Write a p...

Problem Write a program in C++ to read N numbers in an array, the user should be able to search a particular number in the array using sequential search algorithm. Writing a

Nested if else, input marks of c and c++ if c grater than equal to 50 grate...

input marks of c and c++ if c grater than equal to 50 grater than 50 you are pass if c greater than equal to 50 c++ less than 50 than supplementry c++ if c less then 50 and c++ gra

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