More on Classes

More on Classes - Lecture 6b: More on Classes CS256 Fall...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 6b: More on Classes CS256 – Fall 2008
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overloading << and >> Guideline For user defined data types such as enumeration types or classes Once defined, can be used in iostream or fstream How to overload Enumeration type Operator functions Classes Define as friend function in a class
Background image of page 2
Examples Enumeration type ostream& operator<< (ostream& outs, const DaysOfWeek d) { switch (d) { SUN: outs << “Sunday “; break; MON: outs << “Monday “; break; default: outs << “Not a valid weekday “; } return outs; //note: the operator function returns a reference to a stream }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Class objects class Employee { friend istream& operator >> (istream &ins, const Employee& e); }; cout << “Please enter the Employee’s name: \n”; //optional ins >> e.name; cout << “Please enter the Employee’s id: \n”; //optional ins >> e.id; return ins; }
Background image of page 4
Forward reference of class Scenario A class car will use a class truck object as a
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

More on Classes - Lecture 6b: More on Classes CS256 Fall...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online