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

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
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 examplesSunil Kumar Gunasekaran
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
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 javaTharuniDiddekunta
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 InheritanceAmrit Kaur
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaMananPasricha
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan 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

OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptxmrxyz19
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
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 modulesDurgesh Singh
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 

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

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 

Recently uploaded (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 

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