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

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
CS Nov 2006 C-strings.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading in C++
Basic Elements of C++ Chapter 2.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Input & Output: Console
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
February 14, 2005 Characters, Strings and the String Class.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
A Sample Program #include using namespace std; int main(void) { cout
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ ReviewEECE 3521 EECS 352 Data Structures and Algorithms José M. Vidal Office: 3A47.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Bill Tucker Austin Community College COSC 1315
Operator Overloading Introduction
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
C Characters and Strings
Overloading Operator MySting Example
Basic Elements of C++.
TMF1414 Introduction to Programming
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Lecture 13 Input/Output Files.
Operator Overloading.
Operator Overloading; String and Array Objects
Chapter 3: Input/Output
C++ Programming Lecture 3 C++ Basics – Part I
Fundamental Programming
Strings …again.
C Characters and Strings
The Fundamentals of C++
Presentation transcript:

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

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 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 &); };

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

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

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( " " ); i = atoi( " " ); i = atoi( " " ); i = atoi( " 512 bottles of beer on the wall" );

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

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

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

More Specifier specifierOutputExample c Character a d or i Signed decimal integer 392 e Scientific notation (mantise/exponent) using e character e+2 E Scientific notation (mantise/exponent) using E character E+2 f Decimal floating point g Use the shorter of %e or %f G Use the shorter of %E or %f 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

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