Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Formatting and outputting your own classes in C++

When you output an object in C++, you pass it to an output stream such as std::cout or cout. If the thing you're outputting is a string, or an integer, float, some other primitive or a standard system object, that's all you need to do and the formatting is taken care of for you. But what if you're outputting your own object?

The first option you have is to call methods that will convery your object's values into primitives which you ca output usig standard techniques. The alternative is for you to overload the operator<< method for the ostream class to provide an extra output method to handle your output. Here's an example of such a method, together with a method in the class we're handling to do the actual formatting:

  void Mtbs::print(ostream *os)
    { *os << "Attention every " << value << " cups [" << descriptor << "]" ;
    }
  
  ostream &operator<<(ostream &os, Mtbs &mt) {
    mt.print(&os);
    return os;
    }


Complete example [here]
(written 2015-10-30)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C235 - C and C based languages - I/O in C++
  [4562] Left shift operator on an output stream object - C++ - (2015-10-30)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3807] Reading (and writing) files in C++ - (2012-07-18)
  [3390] Printing objects in C++ - (2011-08-13)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [1675] Comparing Objects in C++ - (2008-06-13)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)


Back to
Left shift operator on an output stream object - C++
Previous and next
or
Horse's mouth home
Forward to
Perl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015
Some other Articles
Japanese and Malaysian food in Melksham
C - why is slow to write and debug) but fast to run?
Allocation of memory for objects in C++ - Stack v Heap
Perl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015
Formatting and outputting your own classes in C++
Hello World in C++ - a first program, with the process explained
Variables, Pointers and References - C and C++
When do I use the this keyword in C++?
Well House Consultants - Python courses / what's special.
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4563_For ... in-C-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat