Académique Documents
Professionnel Documents
Culture Documents
.
Public:
// visible to all functions in the program.
};
Inheritance
When a protected member is inherited in the public mode, it becomes
protected in the derived class too and therefore is accessible by the member
functions of the derived class.
It is also ready for further inheritance.
A protected member derived in the private mode becomes private in the derived
class.
It is available for member functions of the derived class it is not available for
further inheritance.
The keywords private, public and protected may appear in any order and any
number of times in the declaration of the class.
It is possible to inherit a base class in protected mode.
Both the public and protected members of the base class will be protected
members of the derived class.
Inheritance
Effect of inheritance on the visibility of members
Inheritance
Visibility of inherited members
Inheritance
Multi level inheritance
Inheritance
//muti level inheritance
#include <iostream>
using namespace std;
class student {
protected:
int roll_no;
public:
void get_no(int);
void put_no(void);
};
void student :: get_no(int a){
roll_no =a;
}
void student :: put_no(){
cout << "Roll Number " << roll_no << "\n";
}
Inheritance
class test : public student{ // first level derivation
protected :
float sub1;
float sub2;
public:
void get_marks(float,float);
void put_marks(void);
};
void test :: get_marks(float x, float y){
sub1= x;
sub2= y;
}
void test :: put_marks(){
cout << "Mark in sub1 : " << sub1 << "\n";
cout << "Mark in sub2 : " << sub2 << "\n";
}
Inheritance
class result : public test {
//second level derivation
float total;
public:
void display (void);
};
void result :: display(void){
total= sub1+sub2;
put_no();
put_marks();
cout << "Total : " << total << "\n";
}
int main(){
result student1;
student1.get_no(111);
student1.get_marks(75.5, 80.5);
student1.display();
system("pause");
return 0;
}
Inheritance
Multiple inheritance
A class can inherit two or more classes.
Allows us to combine the features of
several existing classes as a starting point
for new classes.
The syntax of a derived class with multiple base
class
class : visibility B-1, visibility B-2 .
{
. // Function body
}
Visibility may be either private or public. The base classes may be separated by commas.
Inheritance
#include <iostream>
using namespace std;
class M {
protected:
int m;
public:
void get_m(int);
};
class N {
protected:
int n;
public:
void get_n(int);
};
class P : public M , public N{
public:
void display (void);
};
Inheritance
void M :: get_m(int x){
m=x;
}
void N :: get_n(int y){
n=y;
}
void P :: display (void){
cout << " m = " << m << "\n";
cout << " n = " << n << "\n";
cout << " m*n = " << m*n << "\n";
}
int main(){
P p;
p.get_m(10);
p.get_n(20);
p.display();
system("pause");
return 0;
}
Polymorphism
Polymorphism means one name multiple forms.
Concept of Polymorphism is implemented using the overloaded functions and
operators.
The overloaded member functions are selected for invoking by matching
arguments ,both type and number.
This information is known to the compiler at the compile time and compiler is
able to select the appropriate function for a particular call at the compile
time
itself.
This is called early binding or static binding also known as compile time
polymorphism.
Let us consider a situation where the function name and prototype is the same in
both the base and derived classes.
We may use the class resolution operator to specify the class while invoking the
functions with the derived class objects.
It would be nice if the appropriate member function could be selected while the
program is running. This is known as run time polymorphism.
C++ supports a mechanism known as virtual function to achieve run time
polymorphism.
Polymorphism
Achieving polymorphism
Virtual functions
When we use the same function name both in the base and derived classes, the
function in base class is declared as virtual using the keyword virtual
preceding its normal declaration .
When a function is made virtual, C++ determines which function to be used at
run time based on the type of object pointed to by the base pointer,
rather
than by the type of the pointer.
Thus by making the base pointer to point to different objects we can execute
different versions of virtual function.
class Shape {
protected:
int width, height;
public: Shape( int a=0, int b=0) { width = a; height = b; }
// pure virtual function
virtual int area() = 0;
};
The = 0 tells the compiler that the function has no body and above virtual
function will be called pure virtual function.
outfile.close();
ifstream infile (salary);
.
infile.close();
ofstream outfile (salary);
Exception handling
It is very rare that a program works for the first time. It will have bugs
Two common bugs are logic errors and syntactic errors.
We come across some errors other than logic and syntax. They are known as
exceptions(run time anomalies or unusual conditions).
Conditions such as 1. divide by zero 2.access to an array outside its bounds
3.Running out of memory or disk space.
Basics of exception handling - two types of Exceptions
1.Synchronous out of range index ,over flow
2.Asynchronous events beyond the control of the program(keyboard interrupt)
Exception handling mechanism is to provide means to detect and report an
exceptional circumstance
The mechanism suggests a separate error handing code that performs the
following tasks.
1.Find the problem (hit the exception)
2.Inform that an error had occurred (throw an exception)
3.Receive the error information (catch the exception)
4.Take corrective actions (handle exception)
Exception handling
Exception handling mechanism
Basically built upon 3 keywords
Try, throw, catch
Keyword try is used to preface a block of
Statements Which may generate exceptions.
(try block)
Exception handling
Exception handling mechanism
When try block throws an exception, the program control leaves the try block and
enters the catch statement of the catch block.
Exceptions are objects used to transmit information about a problem.
If the type of object thrown matches the arg type in the catch statement, then
catch block is executed for handling the exception.
If they do not match, the program is aborted with the help of abort() function
which is invoked by default.
When no exception is detected and thrown, control goes to the statement
immediately after the catch block.
Exception handling
// TRY BLOCK THROWING AN EXCEPTION
#include <iostream>
using namespace std;
int main(){
int a,b;
cout << "Enter the values of a & b :" << "\n";
cin >> a ;
cin >> b;
int x = a-b;
try {
if (x !=0){
cout << "Result a/x :" << a/x;}
else
{
throw(x);
}
}
Exception handling
catch (int i){
cout << "Exception caught : x = " << x << "\n";
}
cout << "end";
system ("pause");
return 0;
}
Exceptions are thrown by functions which are invoked from within the try block.
The point at which throw is executed is called throw point.
Once the exception is thrown to the catch block, control cannot return to the throw
point.
Exception handling
Exception handling
Throwing mechanism
when an exception that is desired to be handled is detected , it is thrown using
the throw statement.
throw (exception);
throw exception;
throw; // used for rethrowing an exception
The operand object exception may be of any type, including constants. It is also
possible to throw objects not intended for error handling.
Catching mechanism
Code for exception is handled in catch block.
catch (type arg) {
// statements for managing exception
}
The type indicates the type of exception that catch block handles.
The parameter arg is an optional parameter name.
The catch statement catches an exception whose type matches with the type of
catch argument .
Due to mismatch, if an exception is not caught, abnormal program termination
Exception handling
Multiple catch statement
Program segment has more than one condition to throw exception.
We can associate more than one catch statement with a try.
try {
// try block
When an exception is thrown, exception handlers are
}
searched in an order for an appropriate match.
catch (type1 arg){
// catch block1
The first handler that yields a match is executed.
}
After executing the handler the control goes to the first
catch (type2 arg){
Statement after the last catch block for that try.
// catch block2
}
.
.
catch (typeN arg){
// catch block N
}