Upload
mukul-kumar-neal
View
149
Download
0
Embed Size (px)
Citation preview
www.techmentor.co.in www.triumphsys.com
2Object Oriented Programming
04/11/23
3Object Oriented Programming
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 ::
Static Members of a Class
04/11/23
4Object Oriented Programming
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
Static Members of a Class
04/11/23
5Object Oriented Programming
class item{ static int count; int price;
public :
item() { price=100;
}
Static Members of a Class
04/11/23
void incr() { price++; count++; } void show() { cout<<" \nPrice "<<price; cout<<" \nCount = "<<count; } };int item :: count ;
6Object Oriented Programming
void main(){ clrscr(); item a,b,c; cout<<" \n\n A's Show"; a.show(); cout<<" \n\n B's Show"; b.show(); cout<<" \n\n C's Show"; c.show();
Static Members of a Class
04/11/23
a.incr(); b.incr(); c.incr(); cout<<" \n\n A's Show after INCR";a.show();cout<<" \n\n B's Show after INCR";b.show();cout<<" \n\n C's Show after INCR";c.show();getch(); }
price 100
a
price 100
b
price
c
100
0
Count
101101 101
123
void incr()
{
price++;
count++;
}
7Object Oriented Programming
04/11/23
8Object Oriented Programming
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
Influential Reuse Mechanisms in C++
04/11/23
9Object Oriented Programming
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.
Composition
04/11/23
class date {
};
class person { char name[20];
};
int dd, mm, yy ; date DOB
10Object Oriented Programming
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.
Inheritance
04/11/23
11Object Oriented Programming
Inheritance
04/11/23
class person { char name[20];
};
date DOB
class analyst : public emp {
float incentives;
};
class date {
};
int dd, mm, yy ;
class emp : public person {
int Empid;
};
12Object Oriented Programming
Class Hierarchy and Relationships
04/11/23
Person DateHas - a
Employee
Is - a
Analyst
Is - a
13Object Oriented Programming
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.
Constructors & Parameter List in Inheritance
04/11/23
14Object Oriented Programming
Base Class Access Specifier Gimmicks
04/11/23
Private Members of Base Class
• Can not be inherited• Not accessible for derived class directly• Not accessible for outside environment
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.
15Object Oriented Programming
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.
Derivation Variants - Visibility Mode
04/11/23
16Object Oriented Programming
Protected Derivation - ?????
Derivation Variants - Visibility Mode
04/11/23
17Object Oriented Programming
Inheritance Variants
04/11/23
Class A
Class B
Simple Inheritance
Class B Class C
Class A
Class DClass C
Multilevel Inheritance
Multiple Inheritance
Hybrid Inheritance
Multipath Inheritance
18Object Oriented Programming
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 ClassClass 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
Consequence of Multipath Inheritance
04/11/23
Class B Class C
Class A
Class D
Multipath Inheritance
19Object Oriented Programming
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
Virtual Base Class
04/11/23
Class A { …..; };
Class B1 : virtual public A { …..; };
Class B2 : public virtual A { …..; };
Class C : public B1, public B2 { …..; };
20Object Oriented Programming
Class Hierarchy and Relationships
04/11/23
Person Date
Employee
SM Programmer Admin
Empid, Basic
AllowanceProject name, Passport Details, Km, CPK
Target, Commission Passport Details, Km, CPK
21Object Oriented Programming
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.
04/11/23
22Object Oriented Programming
04/11/23
23Object Oriented Programming
Bank maintains 2 kinds of Accounts --- Savings & CurrentSaving Acct provides 4% Interest But no Cheque BookCurrent 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 BalanceCreate 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
1. Accept deposi and Update Balance2. Display Customer Info with Balance 3. Compute & deposit the interest4. Permit withdrawal and Update Balance5. Check Minimum Balance & impose the penalty to update balance
04/11/23
24Object Oriented Programming
04/11/23
25Object Oriented Programming
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
Constructors & Parameter List in Inheritance
04/11/23