Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.

Similar presentations


Presentation on theme: "CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC."— Presentation transcript:

1 CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC

2 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

3 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 Glenn Stevenson CSIS 113A MSJC friend ostream &operator<<(ostream&, Rectangle &);

4 What is this ostream thing? The iostream library is derived from istream and ostream! Glenn Stevenson CSIS 113A MSJC

5 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

6 Rectangle class definition Glenn Stevenson CSIS 113A MSJC 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 >(istream&, Rectangle &); };

7 The operator overloads Glenn Stevenson CSIS 113A MSJC ostream &operator >(istream &input, Rectangle &r) { cout > r.length; cout > r.width; return input; }

8 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

9 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: Glenn Stevenson CSIS 113A MSJC –ostream &operator<<(ostream &output, Rectangle &r) Rectangle r; cin >> r; cout << r;

10 atoi, atol, atof Ascii to integer, long, float –Use these to convert cstrings to numbers Glenn Stevenson CSIS 113A MSJC 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" );

11 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 Glenn Stevenson CSIS 113A MSJC int i = atoi( " does not work: 512" ); // results in i == 0

12 sprintf Used to format print numbers and cstrings into a buffer (cstring); Uses a specifier to put data in buffer: Glenn Stevenson CSIS 113A MSJC 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; }

13 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! Glenn Stevenson CSIS 113A MSJC sprintf (buffer, "%d plus %d is %d", a, b, a+b);

14 More Specifier specifierOutputExample c Character a d or i Signed decimal integer 392 e Scientific notation (mantise/exponent) using e character 3.9265e+2 E Scientific notation (mantise/exponent) using E character 3.9265E+2 f Decimal floating point 392.65 g Use the shorter of %e or %f392.65 G Use the shorter of %E or %f392.65 o Signed octal 610 s String of characters sample u Unsigned decimal integer 7235 x Unsigned hexadecimal integer 7fa X Unsigned hexadecimal integer (capital letters) 7FA p Pointer address B800:0000 Glenn Stevenson CSIS 113A MSJC

15 Most common specifiers %d – int %s – string (cstring) %f – floating point number %c - character Glenn Stevenson CSIS 113A MSJC


Download ppt "CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC."

Similar presentations


Ads by Google