2cc1 Printing objects in C++
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Printing objects in C++

Lunch at Well House ManorOverloading operators with methods is a great way of providing a shorthand using the operator syntax for common things you want to do with objects. In other words - it's much easier to write:
  c = a + b
than
  c = a.addition(b)
ans it's also much easier for the maintainance programmer later too - a short and sweet "+" is pretty obvious.

In C++ (which I have been teaching for the last couple of days), you can define an operator within a class simply be defining a method with an appropriate name - a predefined name, in fact, such as operator+. There's an example of that from a course I ran earlier this year - [here].

It gets a little trickier when you want to define how an object is to be output when you pass it to an output stream. You've probably seen code like:
  cout << varname << "kgs" << endl
and had it work well for you. And if varname is the name of a standard type such as an integer or a float, it works seemlessly and without the need for much thought. But what if varname is one of your own objects?

Your initial reaction may well be to overload the << operator (actually the left shift operator), but unfortuanatley the object you want to run it on appears to the right of the <<, and so that overriding doesn't work out. What you need to do instead is to provide a method that will work on cout. Here's how ...


On a C++ coursecout is a an object of type ostream, so you define an extra function that takes an output stream parameter, and a parameter of whatever object type you have. You call the function operator<< so that it works when you call the << operator. And you have it return the incoming ostream parameter. This latter action is what allows the chaining of a whole series of << operators on a line.

Code:

  ostream &operator<<(ostream &os, Person &p) {
    p.print(&os);
    return os;
    }


You an then simply implement the print method in your class:

  void Person::print(ostream *os) {
    *os << "A Person " << weight << " " << height << endl;
    }


Demonstration source code for those methods include in a class [here]. The header file (including the function template needed) is [here]. And the new logic is called from a sample test program [here].


Illustrations - lunchtime at Well House Manor on the C++ course that was running there yesterday. We have a selection of lunches during the week, tailored to suit the delegates on each particular week. Yesterday was a "healthy option" lunch - very often that's much more popular that a traditional heavy meal which can take up a lot of valuable learning time, and may leave some of us a little too sleepy and mellow for some of the more challenging subjects.
(written 2011-08-13) 3834

 
Associated topics are indexed under
C235 - C and C based languages - I/O in C++
  [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)
  [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
Plenty to do in Melksham
Previous and next
or
Horse's mouth home
Forward to
For programmers who use Internet Explorer as their browser
Some other Articles
The difference between lists and strings - Tcl
Rodwell Trail, Weymouth
What costs 8.20 from Melksham, or 22.30 via Chippenham?
For programmers who use Internet Explorer as their browser
Printing objects in C++
Plenty to do in Melksham
Templates in C++ - defining a family pattern of methods / functions
Eating out in Melksham - where we like for lunch.
Adding the pieces together to make a complete language - C
Do university courses teach the right things for life at work later on?
4090 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 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., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3390_Pri ... in-C-.html • PAGE BUILT: Sat Feb 23 12:39:13 2013 • BUILD SYSTEM: wizard
0