1 / 15

CSIS 123A Lecture 5

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.

zorina
Download Presentation

CSIS 123A Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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 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

  7. 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

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

  10. 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

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

  12. 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

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

  14. More Specifier 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

More Related