Beruflich Dokumente
Kultur Dokumente
Inheritance
Inheritance
Objectives
Explaining the inheritance Explaining how to derive inherited classes Using constructors in inheritance Using protected access specifier contd on the next slide..
2 Programming in C++ 06/09/11 2006 IBM Corporation
Explaining public inheritance Explaining private inheritance Using method overriding Using multiple inheritance Explaining virtual function
Programming in C++
06/09/11
Introduction
Inheritance is a powerful feature of OOP and its most important use is in software reusability. It is the process of creating new classes from already existing classes. The new class is called derived class and the already existing class is called the base class. The derived class inherits all the capabilities of the base class and can have some additional capabilities.
Programming in C++
06/09/11
Single Inheritance
The derivation of a class from only one base class
is called single inheritance and it is the ability of derived class to inherit member functions and member variables of the existing base class.
Programming in C++
06/09/11
Programming in C++
06/09/11
Single Inheritance
Notice that in both employee and worker classes
the fields empno, empname, desig and methods getinfo, printinfo() are the same.
Programming in C++
06/09/11
Single Inheritance
A Derived class is defined by specifying its relationship with the
base class in addition to its own details.
Programming in C++
06/09/11
Programming in C++
06/09/11
Derived class
The colon (:) indicates that we are inheriting
something.
10
Programming in C++
06/09/11
11
Programming in C++
06/09/11
12
Programming in C++
06/09/11
For a class: private members (both data and methods) are accessible within that class only. protected members are accessible within that class and its derived sub_classes as well. public members are accessible everywhere and anywhere in the program using that class.
13
Programming in C++
06/09/11
Table summarizing different accessibility relationships: access specifier accessible within accessible from the class outside the class private protected public yes yes yes No No yes
14
Programming in C++
06/09/11
Public Inheritance
In public inheritance base class properties are inherited in the derived class using the public keyword. all the derived class member functions can be access protected and public members of the base class. the derived class functions cannot access the private class members of the base class.
15
Programming in C++
06/09/11
In public inheritance the protected and public class members of the base class are derived as protected and public members respectively in the derived class. the derived class definition begins as shown below: class derived_class : public base_class { ----------------------------} This accessibility relationship is shown diagrammatically in the next figure.
16 Programming in C++ 06/09/11 2006 IBM Corporation
Summary of accessibility relationships in public inheritance. base class accessible accessible accessible access from ow n from from outside specifier class derived the class class private yes no no protected yes yes no public yes yes yes Table:Accessibility relationships in public inheritance
18
Programming in C++
06/09/11
The following table shows an accessibility relation in public base class member visibility private protected
06/09/11 2006 IBM Corporation
19
Programming in C++
Private Inheritance
In private inheritance base class properties are inherited in the derived class using the private keyword. the derived class member functions can access only the protected and public class members but not the private members of the base class. the protected and public members of the base class are derived as private members in the derived class.
20
Programming in C++
06/09/11
In private inheritance objects of the derived class cannot access private, protected and even public members of the base class. the derived class definition begins using the private keyword as shown below: class derived_class_name : private base_class_name { --------------------} This accessibility relationship is shown diagrammatically in the following figure.
21 Programming in C++ 06/09/11 2006 IBM Corporation
The table below summarises the accessibility relationship in private inheritance a tabular form. in access accessible fromaccessible from accessible from specifier own class derived class outside Private yes no no protected yes yes no public yes yes no Table: Accessibility relationship in private inheritance
23
Programming in C++
06/09/11
24
Programming in C++
Constructors In Inheritance
In inheritance the base-class default constructor is called every time when we invoke a derived class constructor in order to create an object of the derived class. As a result, the derived class constructors may be redoing some of the things done by the base-class default constructor. In order to avoid this repetition of the same job,the derived class constructors should invoke corresponding base class constructors.
25
Programming in C++
06/09/11
In principle, a derived class inherits every member of a base class except: its constructor and its destructor its operator=() members its friends
Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.
26
Programming in C++
06/09/11
Derived-class default constructor should invoke base-class default constructor. For derived-class default constructor, the function definition heading should become derived_class :: derived_class(): base_class() { --------------------}
27
Programming in C++
06/09/11
Derived constructor with a constructor with the same relevant parameters into i
28
Programming in C++
class mother { public: mother () { cout << "mother: no parameters\n"; } mother (int a) { cout << "mother: int parameter\n"; } };
int main () { daughter cynthia (0); son daniel(0); return 0; } Output : mother: no parameters daughter: int parameter
class daughter : public mother { public: daughter (int a) { cout << "daughter: int parameter\n\n"; } };
29 Programming in C++
Destructors In Inheritance
It is not necessary to define a destructor in the derived class unless there is some special task to be performed. When a derived-class object goes out of scope, the derived class destructor is called first and then the base class destructor.
30
Programming in C++
06/09/11
Me
31 Programming in C++ 06/09/11 2006 IBM Corporation
32
Programming in C++
06/09/11
Multiple Inheritance
In multiple inheritance a new class is derived from more than one base classes. the derived class definition begins like class employ : public person, public array { --------} The base classes are listed after the colon (:) separated by comas.
33 Programming in C++ 06/09/11 2006 IBM Corporation
int area () { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area () { return (width * height / 2); } }; void main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5);
cout<<"The area of the rectangle is : "<<endl; rect.output (rect.area()); cout<<"The area of the triangle is : "<<endl; trgl.output (trgl.area());
06/09/11 2006 IBM Corporation
Langmarks
totmarks
35
Programming in C++
06/09/11
Now the totmarks has two base classes and these two base
classes have a common base class stdinfo
36
Programming in C++
06/09/11
37
Programming in C++
06/09/11
38
Programming in C++
06/09/11
The declaration of a member with an ambiguous name in a derived For example, suppose that two classes named A and B both have a
member named x, and a class named C inherits from both A and B. An attempt to access x from class C would be ambiguous. name using the scope resolution (::) operator.
39
Programming in C++
06/09/11
Polymorphism
Polymorphism
Polymorphism triggers different reactions in
objects in response to the same message.
42
Programming in C++
06/09/11
Binding
Binding : It is the process of resolving a function call during
the execution time or compile time.
OR
43
Programming in C++
06/09/11
Types of Binding
Static or Early Binding Dynamic or late Binding
44
Programming in C++
06/09/11
Static Binding
Binding that happens during compile time is called Static
Binding.
Eg :
main() { Person p1; P1.acceptdetails(); //binding happens during compile time }
45
Programming in C++
06/09/11
Eg :
main() { Person *p1; P1=new person; P1->acceptdetails(); //binding happens during run time }
46
Programming in C++
06/09/11
void main() { Base *baseptr=new Base; Derived *derivedptr=new Derived; baseptr->Base_Message(); derivedptr->Derived_Message(); }
47
Programming in C++
06/09/11
Special Case
When the Base and derived class use the same
name to one of their member functions, and a pointer to the Base class is used to invoke the member functions of both the base and derived classes
48
Programming in C++
06/09/11
Example :
class Base { public: void show_Message() { cout<<"This is Base Class \n"; } };//end of Base class class Derived : public Base { public: void show_Message() { cout<<"This is Derived Class \n"; } };
49
void main() { Base *baseptr=new Base; baseptr->show_Message(); //make the baseptr point to derived class object baseptr=new Derived; baseptr->show_Message(); }
50
Programming in C++
06/09/11
51
Programming in C++
06/09/11
Virtual Functions
When the Base class and the Derived class use
the same name to their member functions to resolve the identity of these functions (To ensure Late Binding) these functions are to be declared (with in the classes) preceded by the keyword 'Virtual' and they are called 'Virtual Functions'
52
Programming in C++
06/09/11
Example :
class Base { public: virtual void show_Message() { cout<<"This is Base Class \n"; } };//end of Base class class Derived : public Base { public: virtual void show_Message() { cout<<"This is Derived Class \n"; } };
53
void main() { Base *baseptr=new Base; baseptr->show_Message(); //make the baseptr point to derived class object baseptr=new Derived; baseptr->show_Message(); }
1. Virtual functions must have identical declarations in the Base and Derived
classes
2. Virtual functions cannot be used as Friend Functions 3. Virtual functions cannot be static 4. A Virtual function in a Base class must be defined, even though it is not
used.
54
Programming in C++
06/09/11
Polymorphism
Static
( Compile Time )
Dynamic
( Run-time )
Operator Overloading
Function Overloading
55
Method Overriding
Virtual Functions
Programming in C++
06/09/11
56
Programming in C++
06/09/11
Polymorphic class
The class which contains atleast one of more
virtual functions / pure virtual functions is called 'polymorphic class'.
57
Programming in C++
06/09/11
58
Programming in C++
06/09/11
Vehicle
Flyer
Streams
Stream Classes
Ios is the base class Istream and ostream class derived from ios class and are
dedicated for input and output
62
Programming in C++
06/09/11
63
Programming in C++
06/09/11
#include <stdafx.h> #include <iostream.h> #include <fstream.h> //Write to the file int main() { const int MAX=80; char buffer[MAX]; ifstream infile("c:\\test2.txt"); while(infile) { infile.getline(buffer,MAX); cout<<buffer<<endl; } }
64
Programming in C++
06/09/11
#include <iostream.h> #include <fstream.h> #include <string.h> int main() { ofstream outfile("c:\\test3.txt"); //reading from a file char ch; ifstream infile("c:\\test3.txt"); cout<<"The contents of the file are :"<<endl; while(infile) { infile.get(ch); cout<<ch; } return 0; }
65
Programming in C++
06/09/11
Writing to an object
#include "stdafx.h" #include <fstream.h> class person { protected : char name[40]; int age; public: void getdata() { cout<<"Enter name : "; cin>>name; cout<<"enter age : "; cin >>age; } }; int main() { person p1; p1.getdata (); ofstream outfile("c:\\person.dat"); outfile.write ((char *)&p1,sizeof(p1)); }
66
Programming in C++
06/09/11
67
Programming in C++
06/09/11
char ch; do { cout<<"Enter person's data : "<<endl; p1.getdata (); file.write ((char *)&p1,sizeof(p1)); cout<<"Enter another person(Y/N)"; cin>>ch; //to get the no. of records file.seekg(0,ios::end); int pos=file.tellg (); int reccount=pos/sizeof(p1); cout<<"The total no. of records are : "<<reccount; file.seekg (0); //reset to start the file file.read((char *)&p1,sizeof(p1)); while(!file.eof()) { cout<<"\n person:"; p1.showdata (); file.read((char *)&p1,sizeof(p1)); } }while(ch=='y');
68
Programming in C++
06/09/11
Operator Overloading
Operator Overloading
Operator Overloading refers to giving additional
meaning to the normal C++ operators when they are applied to the user-defined datatypes
70
Programming in C++
06/09/11
Return value
Class name
Keyword
Operator to be overloaded
71
Programming in C++
06/09/11
72
Programming in C++
06/09/11
Function overloading
C++ allows you to use the same name for different
functions. As long as they have different parameter type lists, the compiler will regards them as different functions.
73
Programming in C++
06/09/11
74
Programming in C++
06/09/11
Operator Overloading
Operator overloading enable us to apply standard
operators such as +, -, *, < and many more, to objects of our own data types.
75
Programming in C++
06/09/11
() new delete
76
Programming in C++
06/09/11
77
Programming in C++
06/09/11
78
Programming in C++
06/09/11
fps::fps(int f=0,float i=0.0) { feet=f; inch=i; } void fps::print() { cout<<"Feet="<<feet; cout<<"Inch="<<inch; } void main() { fps obj1(11,11); fps obj2(10,10); fps obj3; obj3=obj1 + obj2; obj3.print(); }
fps::fps(int f=0,float i=0.0) { feet=f; inch=i; } void fps::print() { cout<<"Feet="<<feet; cout<<"Inch="<<inch; } void main() { fps obj1(11,11); ++Obj1; obj3.print(); }
while overloading the unary operators ++ and -- to distinguish between prefixing and postfixing a dummy parameter is used in case of ostfixing
81 Programming in C++ 06/09/11 2006 IBM Corporation
References
Programming With C++ - Schaum Series John
Hubbar
82
Programming in C++
06/09/11