0% found this document useful (0 votes)
5 views10 pages

Object Orientation

The document discusses key concepts of object-oriented programming, including templates, inheritance, and virtual functions. It explains how to implement a generic stack class, the benefits of code reuse through inheritance, and the mechanism of dynamic method binding using virtual functions in C++. Additionally, it highlights the importance of maintaining type information for polymorphism in derived classes.

Uploaded by

amankum25725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Object Orientation

The document discusses key concepts of object-oriented programming, including templates, inheritance, and virtual functions. It explains how to implement a generic stack class, the benefits of code reuse through inheritance, and the mechanism of dynamic method binding using virtual functions in C++. Additionally, it highlights the importance of maintaining type information for polymorphism in derived classes.

Uploaded by

amankum25725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Object Orientation

Templates
• Is Class generic enough?

• For a data structure like Stack to be implemented, we can't create Stack of


Integers, Stack of floats, etc., but we can create many instances of same class.
template<class T>
class stack
{
private:
T * s;
T * top;
int size;
int is_full(){ }
public:
};
Inheritance
• Why Inheritance? public:
account(char*n, float amt) {
• Reuse of code
name = new char[strlen(n)+1];
• Adding additional facility strcpy (name, n) ;
• Creating a subtype from already existing balance = amt ;
type.
accnt_no= no_of_accnts++;
Example: }
class account ~account( ) {
{ delete [] name;}
protected: static int no_of_accnts=0;
int accnt_no; void deposit (float amt){
balance += amt;}
char *name;
float balance; void withdrawal(float amt){
balance -= amt;}
Inheritance Cont..
account

savings_account current_account

joint_savings_ account
Virtual Functions
class account{
class joint_savings_account:public
protected:
savings_account
public:
virtual void print() { } {
}; protected:
class savings_account:public account public:
{
protected: // print additional members
public: declared in this class
// print additional members virtual void print() { }
declared in this class
};
virtual void print() { }

};
Virtual Functions Cont..
We keep an additional field (a) for type of an object within each node
of the list.

S J A

•Rule: Pointer of base class can point to an object of derived class


object but not the vice versa.

•Offset of D (derived class object) will have all fields which are there in
B (base class object) followed by its own fields.
Virtual Functions Cont..
• Example:
account *pa ;
while(h!=NULL){
pa= h->object;
switch(h->type) { // type of account
Case 'A' : pa->account::print();
break;
Case 'S': pa->savings_account::print();

Case 'J': pa->joint_savings_account::print();

}
h=h->next;
}
Type Maintenance by Language
• Can type maintenance be done by Language itself?
• Then,
while(h!=NULL)
{
pa =h->object;
pa->print(); // which print() to call, language will find out the type of object
h=h->next; being pointed and then decide which function to call.
}

Solution:
•Dynamic Method Binding supported by OOP and is supported in C++ using virtual
functions.
Implementation of Dynamic Binding
• Keep the type of object along with the object. •If it encounters a base class
• Example: virtual table (vtab) pointer and print( ) is called
using it, pa->print(), then vtab
Points to code
of object of that type is used.
segment of the
account function that is to be
name called at run time. • pa might point to derived
account::print( )
balance class object.
Points to code
segment of the
account function that is to be
called at run time.
name savings_account::pri
nt( )
balance
interest_
rate
Implementation of Dynamic Binding
class bar: public foo{ bar pointer to
bar::vtab bar::k( )
protected: vtab
a
int w; foo::l( )

public: virtual void k( ) { } c bar::m( )

int m( ) { } d foo::n( )

virtual void s( ) { } w bar::s( )

virtual char t( ) { } bar::t( )

double x( ) { }
• k(), l(), m() and n() are at same index level.
} • Any function that is virtual in any of the
• m( ) becomes final, can not be base classes will be kept in vtab of derived
overriden further.
class. Ex. foo::l( ) because the indices in
vtab won't get disturbed.
• x( ) will not be in vtab • All objects of given class store the same virtual
table.

You might also like