This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View 11602105 C Lecture 12 Friends Exception Handling as PDF for free.
Friend relationship Friend function Friend method Friend class Exception Handling Exception Exception Handler try, throw and catch block Computer Programming II
2
Access Privileges - Review (private, Public, protected) 1. Public: methods of class itself, methods of derived class, and other functions like main can access it. 2. Protected: methods of class itself and methods of derived class can access it. 3. Private: methods of class itself can access it.
Computer Programming II
3 3
Will you be my friend? • Friend-ship allows a class to selectively grant access to its private internals to other constructs. • C++ friends come in three flavors: • friend functions • friend classes • friend methods (member functions) • Friend-ship is • A friend function can access both the private and protected members of the class. Computer Programming II
4
Friends function/method/class
• Friend-ship allows a class to selectively grant access to its private and protected internals to other function, method, and class. • C++ friends come in three flavors: • friend functions • friend classes • friend methods (member functions) - A friend function is a function that can access private and protected data members of a class, even though the function itself is not a member of the class. - A friend class is a class whose functions can all access private and protected data members of another class - A friend method is a method that can access private and protected data members of a class, even though the method itself is not a member of the class.
Computer Programming II
5
Example: Friend Function / Method class Chong {… public: Chong(); // constructor friend void perform_sthg( ); ... }; … void perform_sthg() { // can access private&Protected data of //Chong ... }
class Yin {… public: Yin(); // constructor int Yin_f1(); ... // can access private&Protected }; //data of Lim class Lim { public: Lim(); // constructor friend int Yin::Yin_f1(); ... };
The functions perform_sthg has access to private&Protected data of Chong
Only the member method Yin_f1 has access to private&Protected data of Lim
Computer Programming II
6
Example: Friend Class class Raj {… public: Raj(); // constructor int Raj_f1(); int Raj_f2(); int Raj_f3(); }; class Singh {… public: Singh(); // constructor friend class Raj; ... };
Computer Programming II
// can access private&Protected data of Singh
All member methods of Raj (f1, f2, f3) have access to Private&protected data of Singh
7
Friend Function
Computer Programming II
8
Friend Method
Computer Programming II
9
Friend Class Friendship is not inherited, meaning that classes derived from YourOtherClass cannot access YourClass's private members. Friendship is not transitive, so classes that are friends of YourOtherClass cannot access YourClass's private members.
Computer Programming II
10
Computer Programming II
11
Exception Handling
Computer Programming II
12
Dealing with Errors When a program is executed, unexpected situation may occur. Such a situation is called an exception In other word: Exception is a run-time error caused by some abnormal condition
Example: • indexing outside the limits in an array • division by zero • failure of new to obtain a requested amount of memory
Exception handler • it’s a code to deal with these exceptions (run-time errors) when they occured
Computer Programming II
13
Exception Example
Division by zero
Index out of range
float x, y; …. y = 0.0; float result = x/y
int array [10]; for (int i=0; i<=10; i++) array [i] = something;
How to deal with these exceptions? Computer Programming II
14
Dealing with Errors Three aspects of dealing with an error: 1. Identifying that an error has occurred 2. Handling (resolving) an identified error in order to either continue execution of the program OR gracefully terminate it. 3. If the error cannot be resolved at the current level of execution, communicate its existence to the next highest level of execution int main() { int x,y; cout << “Enter for two numbers;” cin >> x >> y; cout << “The first num divided by the second is: “<< MyDivide(x, y) << endl; } double MyDivide(double numerator, double denominator) { return numerator / denominator; Error if denominator=0!!! }
Computer Programming II
15
Introduction to Exceptions • An exception is an object (int, char, text..etc) that contains information on an error and is used to communicate that information to a higher level of execution. • The process of transferring an exception from its current execution context to the context above it is knows as throwing an exception • The point in the function code at which the exception is thrown is referred to as the throw point • An execution context that receives an exception and accesses it is said to catch the exception
Computer Programming II
16
The Syntax of Exceptions C++ implements exceptions with three keywords try, throw and catch Try Block - The code which might generate a runtime error is written within the try block. Format: try { //Code that can generate exceptions } Computer Programming II
17
The Syntax of Exceptions throw - An exception is generated by using keyword throw. That is, to signal that abnormal condition has occurred. Format: throw