Object Oriented Programming Presentation
Object Oriented Programming Presentation
Programming
Knowledge Level:
Basic
About the Author
Level Basic
Prerequisites A participant must know basic understanding of
programming to do this course
Course Flow:
1 2 3 4
Programming OOPS Functions in Classes and
paradigms Concepts C++ Objects
5 6
Inheritance Polymorphism
Introduction:
1. Procedural programming
2. Structured programming
3. Modular programming
4. Functional programming
5. Object-oriented programming
Global data
Sub-programs
Modules
©Copyright 2004, Cognizant Academy, All Rights Reserved 10
Structured Programming
• Three types of control flow are used:
- sequence
- selection
Y/N
- iteration
Object B
Data variable
Object A Object C
Member Function
Data variable Data variable
• Focuses on ADT
Fetch Components
Objective:
After completing this module, you will be able to understand,
– Class and Object
– Data Abstraction
– Encapsulation
– Inheritance
– Polymorphism
– History of C++
– Attributes of Class
– Object Terminology
– Benefits of OOP
Object:
• An Object is a combination of data and behavior.
• Object behavior is referred using terms such as functions,
member functions, methods or operations.
• Data characterizes the state of an object.
Class:
• Description of common properties of objects that belong to
the class i.e., collection of objects with common
characteristics.
• A class does not exist in the memory of computer during
program execution.
• e.g.: Writing material is a class. Pen and Pencil are
objects of the class.
Account
acct_no
name
balance
withdraw()
deposit( )
Account:acct1 Account:acct2
431245 431246
Patrick Meyer
50567.78 35567.78
Objects:
Account
acct_no
name
balance
withdraw()
deposit( )
interest overdraft
Member variables:
State or properties of an object of that class.
Various variables that are defined in the class
They are attributes of an object.
class Point { … … ;};
class StraightLine {
// object’s attributes
Point &StartPoint, &Endpoint; //Reference data member
int Color; //Non-static data member
const int Thickness; //Constant data member
// class’ attributes
static int NoOfLineObjects; //Static data member
};
©Copyright 2004, Cognizant Academy, All Rights Reserved 36
Attributes of Class
Member functions:
They are actions that object can do.
Access modifiers:
Members can be declared
Private
visible only inside the class
Protected
private + visible to the immediately derived class
Public
globally visible
void main(void)
{ HelloWorldCls hwObj;
hwObj.displayHelloWorld( ); // Member function call
displayHelloWorld( ); // Global function call
}
©Copyright 2004, Cognizant Academy, All Rights Reserved 40
OOP Concepts: Summary
• In an object –oriented environment, software is a collection
of discrete objects that encapsulate their data as well as the
functionality to model real-world objects.
• References
• Function Overloading
• Default values
• Inline Functions
• Function Templates
• Friend Functions
• Static Members and Functions
• Constant Functions and Constant data members
• Call by Value:
void swap (int, int); //prototype
main( )
{
int x=4,y=5;
cout<<“x=“<<x<<“ y=“<<y; output: x=4 y=5
swap (x, y); //x, y actual args
cout<<“x=“<<x<<“ y=“<<y; output: x=4 y=5
}
void swap (int a, int b) //a, b formal args
{
int k;
k=a; a=b; b=k;
}
©Copyright 2004, Cognizant Academy, All Rights Reserved 43
Parameter passing mechanisms
• Call by Address:
void swap (int*, int*); //prototype
main( )
{
int x=4,y=5;
cout<<“x=“<<x<<“ y=“<<y; output: x=4 y=5
swap (&x, &y);
cout<<“x=“<<x<<“ y=“<<y; output: x=5 y=4
}
void swap (int* a, int* b)
{
int k;
k=*a; *a=*b; *b=k;
}
©Copyright 2004, Cognizant Academy, All Rights Reserved 45
Parameter passing mechanisms
• Call by Reference:
void swap (int&, int&); //prototype
main( )
{
int x=4,y=5;
cout<<“x=“<<x<<“ y=“<<y; output: x=4 y=5
swap (x, y);
cout<<“x=“<<x<<“ y=“<<y; output: x=5 y=4
}
void swap (int &a, int &b)
{
int k;
k=a; a=b; b=k;
}
©Copyright 2004, Cognizant Academy, All Rights Reserved 47
References
actualint otherint
int actualint = 123;
int &otherint = actualint;
otherint++
123
otherint++
124
int & const otherint = actualint; // Error
• Reference
• Creates a alternate name or alias for the variable
• Used to read or modify the original data stored in that
variable
• Syntax of reference variable declaration:
Standard or user defined data type: char, short, int, float, etc.
Reference operator
count
count
1
2
ref
ref
• Prototype (Declaration):
void ConvertFToC(float f, float &c);
• Definition:
Basic rules:
• When a default is provided, all remaining
parameters must be given defaults.
Object A
Object C
class account {
private:
int currentBal;
static int RateofInt; Shared by
}; all objects
myclass A(0), B(1);
Object
Ra
te B
of
In
t
Object A
currentBal=0 currentBal=1
Syntax:
const <variable-name> = <value>
class Time
{
int hour;
public:
Time(int k) { hour=k; }
void update( ) { hour++; }
int value( ) const { return hour; }
void cheat( ) const { hour++; } // error this is const
};
void main (void)
{
Time t(10);
cout<<t.value( ); 11
}
Prototype (Declaration):
Definition:
Output
Input values Maximum value
1, 2, 3 3
1.1 , 2.1, 1.8 2.1
A, B,C C
Introduction:
• Constructors are special member functions with the
same name as the class.
• A constructor initializes each object when it is
created
• They are commonly used to initialize the member
variables.
Objects s1 s2 s3 s4
x=10 x=10 x=10 x=10
data
hello\0
data
data hello\0
Deep copy
data
hello\0
Declaration
– Same name as class
• Preceded with tilde (~)
~sum( ) { }
For example:
#include<iostream.h>
class account
{ int acc_no; Account constructor
public: Account constructor
account() Account destructor
{cout<<"Account constructor"; }
~account()
{cout<<"Account desctructor"; }
};
void create_stobj()
{ account vip;
}
void main(void) sta
{ tic
acc
clrscr(); oun
account acc1; t vi
p;
create_stobj(); Account constructor
getch(); Account constructor
}
Name
Sex
Age
Reg.No. Edn.Qual.
Course Designation
Marks Specialization
Types of Inheritance
• Inheritance are of the following types
• Simple or Single Inheritance
• Multi level or Varied Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
• Virtual Inheritance
class Person
Person superclass(base class)
{ ….. };
{
Student subclass(derived class) …………
};
visibility mode
Class Person
Person {……};
Class Student : public Person
{……};
Class Employee : public Person
Student Employee
{……};
Person
Class Person
{ ……};
Class Student : public Person
{ ……};
Student Gate Score Class Gate Score
{…….};
Class PG - Student : public Student
public Gate Score
PG - Student {………};
Student Employee
Class Person
{……};
Part-time Student Class Student : public Person
{……};
Class Employee : public Person
{……};
Class Part-time Student : public Student,
public Employee
{…….};
©Copyright 2004, Cognizant Academy, All Rights Reserved 111
Inheritance cont’d…,
Class X Y::mderived( )
{ {
int priv; priv =1; //Error priv is private and
protected: //cannot be inherited
int prot; prot =2; // Ok
public: publ=3; // Ok
int publ; }
void m( ); void global_fun(Y *p)
}; {
void X::m( ) p->priv = 1; //Error : priv is
{ //private of X
priv =1; //Ok p->prot = 2; //Error : prot is
prot =1; //Ok //protected and the function global_fun( )
publ =1; //Ok // is not a friend or a member of X or Y
} p->publ =3; // Ok
class Y : public X }
{ void mderived( ); }
Public Derivation
protected:
protected : protected :
int a2;
int a2; int b2;
int b2,a3;
Protected Derivation
©Copyright 2004, Cognizant Academy, All Rights Reserved 118
Protected derivation - example
class A
{ void main( )
private: int a; {
protected: int b; B b1;
public : void get_a( ) { cin>>a;} b1.get_a( ); //ERROR
void get_b( ) { cin>>b;} b1.get_b( ); //ERROR
void print_a( ) { cout<<a;} b1.get_ab( );
void print_b( ) {cout<<b;} b1.get_c( );
}; b1.get_d( );
class B : protected A b1.print_all( );
{ private : int c; }
protected: int d;
public : void get_c( ) { cin>>c;}
void get_d( ) {cin >>d;}
void get_ab( ) { get_a( ); get_b( );}
void print_cd( ){ cout<<c<<d;}
void print_all( ) { print_a( );
cout<<b<<c<<d;};
}
©Copyright 2004, Cognizant Academy, All Rights Reserved 119
Private derivation - example
The inherited public and protected members of a private
derivation become private members of the derived class.
Private Derivation
©Copyright 2004, Cognizant Academy, All Rights Reserved 120
Private derivation - example
class A Class C : public B
{ { public :
private: int a; void get_all( )
protected: int b; { get_a( ); //ERROR
public : void get_a( ) { cin>>a;} get_b( ); //ERROR
void get_b( ) { cin>>b;}
get_ab( ); //Ok
void print_a( ) { cout<<a;}
get_c( ); //Ok
void print_b( ) {cout<<b;}
get_d( ); //Ok }
};
void print_all( )
class B : private A
{ print_a( ); //ERROR
{
print_b( ); //ERROR
private : int c;
print_cd( ); //Ok
protected: int d;
print_abcd( ); //Ok } };
public : void get_c( ) { cin>>c;}
void main( )
void get_d( ) {cin >>d;}
{ C c1;
void get_ab( ) { get_a( ); get_b( );}
c1.get_a( ); //ERROR
void print_cd( ){ cout<<c<<d;}
c1.get_b( ); //ERROR
void print_abcd( ) { print_a( );
cout<<b<<c<<d; } c1.get_c( ); // Ok
}; c1.get_d( ); //Ok
c1.getall( ); //Ok
c1.print_all( ); //Ok }
©Copyright 2004, Cognizant Academy, All Rights Reserved 121
Derived Class Constructors
class B
{
int x;
public :
B( ) { cout<<”B Constructor Invoked…”<<endl;}
~B( ) { cout<<”B Destructor Invoked …”<<endl;}
}; B Constructor Invoked…
class D : public B D Constructor Invoked…
{ D Destructor Invoked…
int y; B Destructor Invoked …
public :
D( ) { cout<<”D Constructor Invoked …”<<endl;}
~ D( ) { cout<<”D Destructor Invoked…”<<endl;}
};
void main( )
{ D d; }
• A subclass may be derived from a class and inherit its methods and
members.
• Base class constructors are also executed whenever derived class objects
created.
Objective:
– After completing this module,you will be able to
understand,
– Static Polymorphism
– Overloaded Functions
– Overloaded Operators
– Dynamic Polymorphism
– Virtual Functions
Polymorphism
Static Dynamic
Polymorphism Polymorphism
Operator to be overloaded
∙ ٫ → ?: sizeof
©Copyright 2004, Cognizant Academy, All Rights Reserved 138
Unary Operators Overloading
class Point
{
public:
Point &operator=( Point & ); // Right side is the argument.
...
};
class assign()
{
int a;
public:
operator + (assign var1);
};
assign :: operator+ (assign var1)
{ a = 2 + var1.a; }
assign object1,object2;
main( )
{
object1.a=5;
object2.a = object2+ object1;
cout<<“Object2.a is”<< object2.a<<“\n”; Object2.a is 7
}
class One
{
public:
void whoami()
{
cout<<”One”<<endl;
}
};
int main()
{
One one ;
Two two; The Output is
Three three;
One * array[3]; One
One
array[0]=&one;
One
array[1]=&two;
array[2]=&three;
for(int i=0;i<3;i++)
array[i]->whoami();
return EXIT_SUCCESS;
}
Output
– A class that serves only as a base class from which other classes
can be derived ;
– no instance of an abstract base class are ever created.
– If a base class declares a pure virtual functions , no instance of the
class can be created, and so it is necessarily an abstract base class.
Base::f1()
Base Members Base::f2()
Base::f3()
vptr
Derived vtbl;
Derived d1;
Base::f1()
Base Members Derived::f2()
Base::f3()
vptr
Derived::f4()
Derived Members
URL http://www.desy.de/gna/html/cc/Tutorial/tutorial.html
All
http://www.bitpipe.com/rlist/term/Object-Oriented-Programming.html
All
http://www.well.com/user/ritchie/oo.html All
http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
All
Object Oriented
Programming