150 likes | 316 Views
CSIS 123A Lecture 5. Friend Functions. Friend introduction. Friend functions go against object oriented principals because they allow a function to have access of classes private data. Done by declaring it a friend of the class.
E N D
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC
Friend introduction • Friend functions go against object oriented principals because they allow a function to have access of classes private data. • Done by declaring it a friend of the class. • You will find that most Object Oriented philosophy is against friend functions. • I find that there is one real good reason for using friend functions. • adding cin and cout to your classes Glenn Stevenson CSIS 113A MSJC
Friend declaration • Uses keyword friend to define that the operator << operator is a friend funtion that returns a reference to an ostream object. • Goes in class definition • Do not use friend keyword in function declaration • friend ostream &operator<<(ostream&, Rectangle &); Glenn Stevenson CSIS 113A MSJC
What is this ostream thing? • The iostream library is derived from istream and ostream! Glenn Stevenson CSIS 113A MSJC
What is cin & cout • cin and cout are part of the iostream library • This gets created as a combination of the istream and ostream libraries • Since iostream is derived from istream & ostream we can use these classes almost like cin and cout • May help to see an example Glenn Stevenson CSIS 113A MSJC
Rectangle class definition class Rectangle{private: int length; int width;public: Rectangle(int len, int wd); Rectangle(){length=0; width = 0;} int area(); Rectangle &operator = (Rectangle r); Rectangle &operator +=(Rectangle r); bool operator ==(Rectangle r); bool operator ==(int Ar);friend ostream &operator<<(ostream&, Rectangle &); friend istream &operator>>(istream&, Rectangle &); }; Glenn Stevenson CSIS 113A MSJC
The operator overloads ostream &operator<<(ostream &output, Rectangle &r){output << "The length is: " << r.length << " The width is " << r.width<< "The area is " << r.area() << endl;return output;}istream &operator>>(istream &input, Rectangle &r){ cout << "Enter the length: " ; input >> r.length; cout << "Enter the width: "; input >> r.width; return input; } Glenn Stevenson CSIS 113A MSJC
functionality • Notice the function header • ostream &operator<<(ostream &output, Rectangle &r) • The function returns an ostream object and takes one as its first argument. • It now acts like cout • Remember, cout is derived from ostream and istream! • You can define the functionality any way that you would like. • But you should make sure that the >> operator functions like cin and the << operator functions like cout Glenn Stevenson CSIS 113A MSJC
Breaking it down • Think of it this way • First argument is left hand side of operator • Second argument is right hand side of operator • Can now do this: • ostream &operator<<(ostream &output, Rectangle &r) Rectangle r; cin >> r; cout << r; Glenn Stevenson CSIS 113A MSJC
atoi, atol, atof • Ascii to integer, long, float • Use these to convert cstrings to numbers int i; i = atoi( "512" );i = atoi( "512.035" ); i = atoi( " 512.035" ); i = atoi( " 512+34" ); i = atoi( " 512 bottles of beer on the wall" ); Glenn Stevenson CSIS 113A MSJC
Some Caveats • Converts up to the point it cannot convert anymore • All the previous set i to 512 • atof returns a double not a float • If it cannot convert the number, it returns 0 int i = atoi( " does not work: 512" ); // results in i == 0 Glenn Stevenson CSIS 113A MSJC
sprintf • Used to format print numbers and cstrings into a buffer (cstring); • Uses a specifier to put data in buffer: int main () { char buffer [50]; int a=5, b=3; sprintf (buffer, "%d plus %d is %d", a, b, a+b); cout << buffer << endl; return 0; } Glenn Stevenson CSIS 113A MSJC
Specifier • In the above %d is an int specifier. • Says replace int in this location. • Substitution begins after comma following the format string ( the variable a in this case) • All substitutions are placed in the order they are listed. • Make sure that you specify the right type! sprintf (buffer, "%d plus %d is %d", a, b, a+b); Glenn Stevenson CSIS 113A MSJC
More Specifier Glenn Stevenson CSIS 113A MSJC
Most common specifiers • %d – int • %s – string (cstring) • %f – floating point number • %c - character Glenn Stevenson CSIS 113A MSJC