SlideShare a Scribd company logo
1 of 25
www.techmentor.co.in

www.triumphsys.com
Object Oriented Programming

12/14/13

2
Static Members of a Class
Making a member variable “static”
For a static Member variable, individual copies of each member
are not made for each object.
No matter, how many objects of class are instantiated, only one
copy of static data member exists
All objects of that class will share same variable.
When we declare a static data member within a class, we are not
defining it. i.e. not allocating any storage for it.
Ideally, We need to provide a global definition outside the class. This
is done by re-declaring static variable using ::
Object Oriented Programming

12/14/13

3
Static Members of a Class
Static member is initialized to zero when first object is created.
Static members are typically created to maintain values common to
entire class.
Like – to maintain a counter that counts the occurences of all the
objects.
Just like a static member, we can also have static member function
Which can have access to only other static members.
Which is invoked by using class name rather than object

Object Oriented Programming

12/14/13

4
Static Members of a Class
class item
{
static int count;
int price;
public :
item()
{
price=100;
}
Object Oriented Programming

12/14/13

void incr()
{
price++;
count++;
}
void show()
{
cout<<" nPrice "<<price;
cout<<" nCount = "<<count;
}
};
int item :: count ;
5
Static Members of a Class
void main()
{
clrscr();
item a,b,c;
cout<<" nn A's Show";
a.show();
cout<<" nn B's Show";
b.show();
cout<<" nn C's Show";
c.show();
price

101
100

void incr()

a.incr();
b.incr();
c.incr();

{
price++;
count++;
}

cout<<" nn A's Show after INCR";
a.show();
cout<<" nn B's Show after INCR";
b.show();
cout<<" nn C's Show after INCR";
c.show();
getch(); }
price

101
100

price

3
2
1
0
Count

101
100

a
b
Object Oriented Programming

12/14/13

c
6
Object Oriented Programming

12/14/13

7
Influential Reuse Mechanisms in C++
Basic approach to accomplish Code Reuse here is
Do not recreate the existing class from a scratch
Use or Extend existing class which is proven

You simply create objects of your existing class inside new class
New class is composed of objects of existing class.
COMPOSITION

You create a new class as a type of existing class.
Take the basic form of existing class and simply add new code to it and
that too, without modifying existing class.
INHERITANCE
Object Oriented Programming

12/14/13

8
Composition
Actually we have already been composing the classes
Primarily with Built-In Types.

It turns out to be almost as easy to use composition
With user defined types, typically with classes.
class date
{
int dd, mm, yy ;
};

Object Oriented Programming

12/14/13

class person
{
char name[20];
date DOB
};

9
Inheritance
The syntax of Composition was quite obvious.
Now --------- A new approach to perform Inheritance
Just say ------ “ This New Class is like that Old Class “
New Class : Existing Class
New Class is said to be derived from Existing Class .
Where as Existing Class works as Base Class for New Class.

When we explore like this ----------------- we involuntarily make
specific part of base class accessible to derived class.
Of course ---------- without impacting the protection mechanism of
encapsulation.
Object Oriented Programming

12/14/13

10
Inheritance
class date
{

class person
{
char name[20];
date DOB

int dd, mm, yy ;

};

};
class emp : public person
{

class analyst : public emp
{
float incentives;

int Empid;
};
};
Object Oriented Programming

12/14/13

11
Class Hierarchy and Relationships
Person

Has - a

Date

Is - a
Employee

Is - a
Analyst
Object Oriented Programming

12/14/13

12
Constructors & Parameter List in Inheritance
Obviously, we should not construct a derived class without calling the
base class constructor.
It either happens implicitly, if it is a Non – Argument Constructor
Mechanism.
The Base Class Constructors are called implicitly in the opening stage of
Derived Class Constructor.
Virtually, the first line of derived class constructor would be the call to the
Base Class Constructor.

For the Parameterized Constructor, it’s other way round. We need to
follow separate mechanism known as “Constructor Chaining”. Here
we specify Constructor’s Initialization List.
Object Oriented Programming

12/14/13

13
Base Class Access Specifier Gimmicks
Can not be inherited
• Not accessible for derived class directly
• Not accessible for outside environment

Private Members of
Base Class

•

Public Members of
Base Class

• Directly available for derived class
• Accessible for outside environment also.

Protected Members
of Base Class

• Directly available for derived class
• Not Accessible for outside environment.

Object Oriented Programming

12/14/13

14
Derivation Variants - Visibility Mode
Visibility Mode – Specifies how the features of the base class are
derived.
Private Derivation - class emp : private person
When a Base Class is privately inherited by derived class, ‘Public Members ‘
of Base Class become ‘Private Members’ of Derived Class.
So Public Members of Base Class can only be accessed by member functions
of derived class. They are inaccessible to the objects of derived class

Public Derivation - class emp : public person
When a Base Class is publicly inherited by derived class, ‘Public Members ‘
of Base Class become ‘Public Members’ of Derived Class also.
They are accessible to objects of derived class.
Object Oriented Programming

12/14/13

15
Derivation Variants - Visibility Mode
Protected Derivation - ?????

Object Oriented Programming

12/14/13

16
Inheritance Variants
Simple Inheritance

Class A

Class A

Multipath
Inheritance

Hybrid Inheritance

Class B

Multilevel Inheritance

Class B

Class C
Multiple Inheritance

Class C

Object Oriented Programming

12/14/13

Class D

17
Consequence of Multipath Inheritance
Here 3 variants of Inheritance are
involved - Simple / Multiple / Multilevel
Derived Class D has 2 direct base classes
‘Class B’ & ‘Class C’
Which themselves have a common base
class ‘Class A’ --- Indirect Base Class
Class D inherits the traits of Indirect Base
Class via two separate paths.
All Public & Protected Members of
Class A are inherited into Class D twice.
Class D would have duplicate sets of
inherited members from Class A causing
ambiguity.
This can be avoided by making the
common base class as Virtual Base Class

Class A

Class B

Class C

Class D
Multipath Inheritance

Object Oriented Programming

12/14/13

18
Virtual Base Class
Class A
{
…..;
};

Class B1 : virtual public A
{
…..;
};

Class B2 : public virtual A
{
…..;
};

Class C : public B1, public B2
{
…..;
};

When a class is made a Virtual Base Class, Compiler takes
necessary care to see that only one copy of that class is
inherited, regardless of how many inheritance paths exist
Object Oriented Programming

12/14/13

19
Class Hierarchy and Relationships
Person

Empid,
Basic

Employee

SM
Target,
Commission
Passport Details,
Km, CPK
Object Oriented Programming

12/14/13

Date

Programmer
Project name,
Passport
Details, Km,
CPK

Admin
Allowance

20
SM S1 ("Sachin", 4, 6, 1968, 2001, 10000, 120000, 0.05, "S121314", 100,200);
Admin A1 ("Abhay", 12, 12, 1990, 3001, 5000, 5000);
Prog P1("Prasad", 10, 10, 1985, 4001, 25000, ”Railway Reservation System",
"P212223", 100,200);
Emp arr[3]
arr[0] = s1;

arr[1] = A1;

arr[2] = P1

Create methods to show all employee details, to get total travelling expenses
and to get total salary.
Object Oriented Programming

12/14/13

21
Object Oriented Programming

12/14/13

22
Bank maintains 2 kinds of Accounts --- Savings & Current
Saving Acct provides 4% Interest But no Cheque Book
Current Acct provides Cheque Book But no interest. Also insist for Minimum
Balance of Rs 5000. Penalty of Rs 500 can be imposed for Non-maintenance
of Minimum Balance
Create a class Account which stores Name, Acct No & Acct Type. Further
derive Curr_Acct & Sav_Acct to make them more specific to requirement.
Include Necessary Member Functions To Perform Menu Driven Activity as
Follows
Accept deposi and Update Balance
Display Customer Info with Balance
Compute & deposit the interest
Permit withdrawal and Update Balance
Check Minimum Balance & impose the penalty to update balance

1.
2.
3.
4.
5.

Object Oriented Programming

12/14/13

23
Object Oriented Programming

12/14/13

24
Constructors & Parameter List in Inheritance
Derived(formal parameter list, parameter to base class constr.) :
Base(Its Parameter) { Activity in Derived class Constructor }
In the Hierarchy of Date ------- Person --------- Emp
emp e1(2001, 25000, “Sachin”) – This is Object Creation
emp(int emp1, float emp2, char *person1) : person(person1)
{ } - This is Derived Class Constructor
person(char *person1) { } - This is Base Class Constructor

Object Oriented Programming

12/14/13

25

More Related Content

What's hot

Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
shashank12march
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
Amrit Kaur
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 

What's hot (20)

C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
OOP C++
OOP C++OOP C++
OOP C++
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 

Similar to 2. oop with c++ get 410 day 2

Similar to 2. oop with c++ get 410 day 2 (20)

Inheritance
InheritanceInheritance
Inheritance
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Class and object
Class and objectClass and object
Class and object
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object
ObjectObject
Object
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Classes2
Classes2Classes2
Classes2
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

2. oop with c++ get 410 day 2

  • 3. Static Members of a Class Making a member variable “static” For a static Member variable, individual copies of each member are not made for each object. No matter, how many objects of class are instantiated, only one copy of static data member exists All objects of that class will share same variable. When we declare a static data member within a class, we are not defining it. i.e. not allocating any storage for it. Ideally, We need to provide a global definition outside the class. This is done by re-declaring static variable using :: Object Oriented Programming 12/14/13 3
  • 4. Static Members of a Class Static member is initialized to zero when first object is created. Static members are typically created to maintain values common to entire class. Like – to maintain a counter that counts the occurences of all the objects. Just like a static member, we can also have static member function Which can have access to only other static members. Which is invoked by using class name rather than object Object Oriented Programming 12/14/13 4
  • 5. Static Members of a Class class item { static int count; int price; public : item() { price=100; } Object Oriented Programming 12/14/13 void incr() { price++; count++; } void show() { cout<<" nPrice "<<price; cout<<" nCount = "<<count; } }; int item :: count ; 5
  • 6. Static Members of a Class void main() { clrscr(); item a,b,c; cout<<" nn A's Show"; a.show(); cout<<" nn B's Show"; b.show(); cout<<" nn C's Show"; c.show(); price 101 100 void incr() a.incr(); b.incr(); c.incr(); { price++; count++; } cout<<" nn A's Show after INCR"; a.show(); cout<<" nn B's Show after INCR"; b.show(); cout<<" nn C's Show after INCR"; c.show(); getch(); } price 101 100 price 3 2 1 0 Count 101 100 a b Object Oriented Programming 12/14/13 c 6
  • 8. Influential Reuse Mechanisms in C++ Basic approach to accomplish Code Reuse here is Do not recreate the existing class from a scratch Use or Extend existing class which is proven You simply create objects of your existing class inside new class New class is composed of objects of existing class. COMPOSITION You create a new class as a type of existing class. Take the basic form of existing class and simply add new code to it and that too, without modifying existing class. INHERITANCE Object Oriented Programming 12/14/13 8
  • 9. Composition Actually we have already been composing the classes Primarily with Built-In Types. It turns out to be almost as easy to use composition With user defined types, typically with classes. class date { int dd, mm, yy ; }; Object Oriented Programming 12/14/13 class person { char name[20]; date DOB }; 9
  • 10. Inheritance The syntax of Composition was quite obvious. Now --------- A new approach to perform Inheritance Just say ------ “ This New Class is like that Old Class “ New Class : Existing Class New Class is said to be derived from Existing Class . Where as Existing Class works as Base Class for New Class. When we explore like this ----------------- we involuntarily make specific part of base class accessible to derived class. Of course ---------- without impacting the protection mechanism of encapsulation. Object Oriented Programming 12/14/13 10
  • 11. Inheritance class date { class person { char name[20]; date DOB int dd, mm, yy ; }; }; class emp : public person { class analyst : public emp { float incentives; int Empid; }; }; Object Oriented Programming 12/14/13 11
  • 12. Class Hierarchy and Relationships Person Has - a Date Is - a Employee Is - a Analyst Object Oriented Programming 12/14/13 12
  • 13. Constructors & Parameter List in Inheritance Obviously, we should not construct a derived class without calling the base class constructor. It either happens implicitly, if it is a Non – Argument Constructor Mechanism. The Base Class Constructors are called implicitly in the opening stage of Derived Class Constructor. Virtually, the first line of derived class constructor would be the call to the Base Class Constructor. For the Parameterized Constructor, it’s other way round. We need to follow separate mechanism known as “Constructor Chaining”. Here we specify Constructor’s Initialization List. Object Oriented Programming 12/14/13 13
  • 14. Base Class Access Specifier Gimmicks Can not be inherited • Not accessible for derived class directly • Not accessible for outside environment Private Members of Base Class • Public Members of Base Class • Directly available for derived class • Accessible for outside environment also. Protected Members of Base Class • Directly available for derived class • Not Accessible for outside environment. Object Oriented Programming 12/14/13 14
  • 15. Derivation Variants - Visibility Mode Visibility Mode – Specifies how the features of the base class are derived. Private Derivation - class emp : private person When a Base Class is privately inherited by derived class, ‘Public Members ‘ of Base Class become ‘Private Members’ of Derived Class. So Public Members of Base Class can only be accessed by member functions of derived class. They are inaccessible to the objects of derived class Public Derivation - class emp : public person When a Base Class is publicly inherited by derived class, ‘Public Members ‘ of Base Class become ‘Public Members’ of Derived Class also. They are accessible to objects of derived class. Object Oriented Programming 12/14/13 15
  • 16. Derivation Variants - Visibility Mode Protected Derivation - ????? Object Oriented Programming 12/14/13 16
  • 17. Inheritance Variants Simple Inheritance Class A Class A Multipath Inheritance Hybrid Inheritance Class B Multilevel Inheritance Class B Class C Multiple Inheritance Class C Object Oriented Programming 12/14/13 Class D 17
  • 18. Consequence of Multipath Inheritance Here 3 variants of Inheritance are involved - Simple / Multiple / Multilevel Derived Class D has 2 direct base classes ‘Class B’ & ‘Class C’ Which themselves have a common base class ‘Class A’ --- Indirect Base Class Class D inherits the traits of Indirect Base Class via two separate paths. All Public & Protected Members of Class A are inherited into Class D twice. Class D would have duplicate sets of inherited members from Class A causing ambiguity. This can be avoided by making the common base class as Virtual Base Class Class A Class B Class C Class D Multipath Inheritance Object Oriented Programming 12/14/13 18
  • 19. Virtual Base Class Class A { …..; }; Class B1 : virtual public A { …..; }; Class B2 : public virtual A { …..; }; Class C : public B1, public B2 { …..; }; When a class is made a Virtual Base Class, Compiler takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist Object Oriented Programming 12/14/13 19
  • 20. Class Hierarchy and Relationships Person Empid, Basic Employee SM Target, Commission Passport Details, Km, CPK Object Oriented Programming 12/14/13 Date Programmer Project name, Passport Details, Km, CPK Admin Allowance 20
  • 21. SM S1 ("Sachin", 4, 6, 1968, 2001, 10000, 120000, 0.05, "S121314", 100,200); Admin A1 ("Abhay", 12, 12, 1990, 3001, 5000, 5000); Prog P1("Prasad", 10, 10, 1985, 4001, 25000, ”Railway Reservation System", "P212223", 100,200); Emp arr[3] arr[0] = s1; arr[1] = A1; arr[2] = P1 Create methods to show all employee details, to get total travelling expenses and to get total salary. Object Oriented Programming 12/14/13 21
  • 23. Bank maintains 2 kinds of Accounts --- Savings & Current Saving Acct provides 4% Interest But no Cheque Book Current Acct provides Cheque Book But no interest. Also insist for Minimum Balance of Rs 5000. Penalty of Rs 500 can be imposed for Non-maintenance of Minimum Balance Create a class Account which stores Name, Acct No & Acct Type. Further derive Curr_Acct & Sav_Acct to make them more specific to requirement. Include Necessary Member Functions To Perform Menu Driven Activity as Follows Accept deposi and Update Balance Display Customer Info with Balance Compute & deposit the interest Permit withdrawal and Update Balance Check Minimum Balance & impose the penalty to update balance 1. 2. 3. 4. 5. Object Oriented Programming 12/14/13 23
  • 25. Constructors & Parameter List in Inheritance Derived(formal parameter list, parameter to base class constr.) : Base(Its Parameter) { Activity in Derived class Constructor } In the Hierarchy of Date ------- Person --------- Emp emp e1(2001, 25000, “Sachin”) – This is Object Creation emp(int emp1, float emp2, char *person1) : person(person1) { } - This is Derived Class Constructor person(char *person1) { } - This is Base Class Constructor Object Oriented Programming 12/14/13 25