Вы находитесь на странице: 1из 83

All are Welcome to

Seminar on,

Object Oriented Programming


04/07/11 1
WHAT IS AN OBJECT ORIENTED
ROGRAMMING?
 It is a style of programming that represent a program as a
system of object.

 A technique of designing and implementing a software.

 A new way of approaching the job of programming.

 The best ideas of structure programming are combined with


several powerful concepts.

 Not concerned with the details of the program operation.

04/07/11 2
Deals with over all organization of the program.

Define abstract data types.

Does not impart anything to a finished soft ware.

Programmer can gain significant advantages in large


software.

Enables the programmer to remain close to the conceptual


,high level model of the real world program.

04/07/11 3
ADVANTAGES OF OBJECT ORIENTED
PROGRAMMING
Program are easier to understand because data and programs are stored
together(meaning).

Application program can be modeled in a more natural way.

Hierarchical structure of oop make programs easier to design and understand.

Improved reliability

04/07/11 4
Enhanced programmers productivity because of reusability
oops:
[marriage takes place between data and
code]
We can eliminate redundant code and me existing classes
Data hiding help the programmer to build source programs
that cannot be invaded by other parts of the program.
OO system can be easily upgraded from small to large
systems.[Microsoft produce new OS for every 6 months]

04/07/11 5
CHARATERISTICS

 Data is critical element in oop.


 Emphasis is on data rather than procedure.
 Data and functions are obtained together.
 Protect the data from accidental modification from
outside function.
 Allows to decompose a problem into a number of
entries called object.

04/07/11 6
KEY CONCEPTS OF OOP
1. Class
2. Object
3. Information hiding and Data encapsulation
4. Abstraction
5. Constructor
6. Destructor
7. Friend function
8. Operator and function overloading
9. Inheritance
10. Overriding (function)
11. Virtual and pure virtual function
12. Dynamic polymorphism
13. Abstract class
14. Virtual class

04/07/11 7
04/07/11 8
OBJECT
A tangible & visible entity
An object is different from other objects
An object has its own properties
We differentiate the objects by their Attributes
and characteristics(properties)

How do you differentiate the having objects ?


[by their
1.behaviours
2.properties/Attributes]

04/07/11 9
Real World Objects

Living Non-Living

Attributes Behaviors Attributes

Psychologi
Physical
cal
04/07/11 10
REPRESENTING AN
OBJECT

Object’s Identity

Object’s State

Object’s Behavior

04/07/11 11
SOFTWARE OBJECTS

o Are modeled after real world objects

o They too have attributes and behaviors

o Maintain it attributes in variables and implement its


behaviors with methods.

o A software object is the encapsulation of behavior


and data in a programmed unit.
04/07/11 12
PROBLEM SOLVING

1. Statements
2. Understanding
3. Analysis
4. Design
5. Implementation
6. Testing

OUTPU
INPUT PROCESS
T
04/07/11 13
1. OO Analysis

2. OO Design

3. OO Implementation

4. OO Testing
Ex:-
Sum of two numbers
04/07/11 14
ANALYSIS
I. Find out the properties/Attributes
- What is given and asked ?

I. Find out the behavior (methods)


*Action performed on the attributes
[Attributes that are related to the problem]

04/07/11 15
DESIGN

S2NOS Object’s Identity

privat
e
- X,Y,SUM : int Object’s State
set() :
void
+
publi add() : Object’s Behavior
c + void
+ print () :
04/07/11 void 16
DECLARATION
class S2NOS
{
private : int x,y,sum;
instance
variables
public: void get();
void add(); Methods
Member
void print(); s of
}; The
Class

04/07/11 17
IMPLEMENTATION /DEFINITION

#include<iostream.h>

void S2NOS::get() { x=10;y=20; }

void S2NOS::add(){ sum=x+y; }

void S2NOS::print(){ cout<<sum; }

04/07/11 18
CLASS

It is a blue print of an object.


It is a template.
Defines a standard set of attributes and
behavior.
Since it defines a structure, It is virtual in nature.
Just like real engineering blue print of a building.

04/07/11 19
OBJECT CREATION &
TESTING

void main()
ad
d( {
t() )
se S2NOS ob;
X , Y,
SUM
ob.set();
ob.add();
ob.print();
print()
}

04/07/11 20
OBJECT
 Instance of a class.
 Can be uniquely identified by it’s name
 Defines a state which is represented by the values of
its attributes at a particular time.
 Can be considered a “thing” that can perform a set of
activities.
 The set of activities that the object performs defines
the object is behavior.
 Object can communicate by passing message to each
other.
04/07/11 21
OBJECTS Vs CLASS

 Classes are blueprint.

 Objects are instance of class.

04/07/11 22
DATA HIDING AND ENCAPSULATION

RAM
ad
( ) d(
et )
s
X , Y,
SUM

print(
)

04/07/11 23
- Object’s nothing but an atom.
 Data is the nucleus.
 Methods are electrons.
- from the diagram
 variables make up the centre ot nucleus
of the object
 Method surround and hide the object’s
nucleus from other objects.
 Packing an object’s variables within the
protective custody of its methods is
called encapsulation.
(E.g.: Capsule)
04/07/11 24
1. MODULARITY

* Objects can be written and maintained


independently .

2. INFORMATION HIDING

* The object can maintain private attributes and


methods that can changed at any time without offering
the other objects that depending on it.

04/07/11 25
* ABSTRACTION
Hiding the details

* TWO TYPES OF ABSTRACTING


- Data abstraction
- Procedural abstraction

* DATA ABSTRACTION
• All data types are abstract. they are not real.

04/07/11 26
ACCESS SPECIFIERS
* Specifies the accessibility of members
* private, protected, public
private :
* Members can be used only by member functions and
friend of the class.
protected :
* Member can be used by member function and
friends of the class.
*They can be med by class derived from the class.
public :
* Member can be used by any function.
The default access specifier to class members is private.
04/07/11 27
CONSTRUCTORS

main()
S2NOS
{
- X,Y,SUM : int S2NOS ob;
ob.set();
S2NOS()
+ void ob.add();
+ add(); ob.print();
+ void print }
();
04/07/11 28
CONSTRUCTORS

Constructor is nothing but a method.


Having the same name an the class.
Method to initialize objects of a class.
Is always called an the time of instantiation.
Has no return values not even void
Is called only one (i.e) at the time of instantiation.

04/07/11 29
Can’t be invoked explicitly
Cannot be declared static, constant or variable.
[In c#,java, all the properties of a
window is set by constructor]
Types :
1. Default constructor
2. Parameterized constructor
3. Overloaded constructor
4. Copy constructor

04/07/11 30
DEFAULT CONSTRUCTOR:

It is a constructor with no argument.


Used to initialize the instance variables
Set all instance variable to their default values

all numeric data contained in the instance field


would be zeroed out.
all object values would point to nill
all Boolean would be false

04/07/11 31
if you don’t provide a constructor,the complier
provide default constructor
complier creates a default can only when your class
has no other constructor.

S ad S2NOS
O d(
2 N )
S () - X,Y,SUM : int
X , Y,
SUM S2NOS()
+ add() :
print(
+ void
)
+ print () :
04/07/11 void 32
#include<iostream.h> main()
S2NOS::S2NOS() { x=10;y=20; }
{
S2NOS
void S2NOS::add(){ sum=x+y; } ob;
void S2NOS::print(){ cout<<sum; } ob.set();

ob.add()
;

04/07/11
ob.print(
33
PARAMETERIZED CONSTRUCTOR
• It is a constructor which invoked, when the arguments
are passed at the time of object creation

S2N S2NOS
OS
(int
nt) ,i
- S2NOS()
X,Y,SUM : int
S
S2NO

X , Y,
()

d(

SUM
ad

+
)

S2NOS(int,int)
+
print( add() :
) + void
+
04/07/11
print () : 34
#include<iostream.h>

main()
S2NOS::S2NOS()
{ x=10;y=20; } {
S2NOS
S2NOS::S2NOS(int x, int y) ob(10,15);
{this.x=x; this.y=y;} ob.add();
ob.print();
void S2NOS::add() }
{ sum=x+y; }
04/07/11 35
OVERLOADED CONSTRUCTOR :

* A Constructor behave differently at different situations.


* A Constructor changes it’s functionality with respect to
their arguments.

,i
S2NOS
(i nt
S
S2

N O t)
S2 n
NO )

- X,Y,SUM : int
S(
in

X,
t
S2NOS()

Y,
+ S2NOS()
()

SUM
+ S2NOS(int)
d
ad

+ S2NOS(int,int)
print() + add() : void
04/07/11 + print () : void 36
#include<iostream.h>

S2NOS::S2NOS() { x=10;y=20; }main()


{
S2NOS::S2NOS(int x) S2NOS ob1;
{this.x=x;y=30;}
S2NOS
ob2(25);
S2NOS::S2NOS(int x, int y)
S2NOS
{this.x=x; this.y=y;}
ob3(10,15);
void S2NOS::add(){ sum=x+y; }
ob.add();
ob.print();
04/07/11
void S2NOS::print() } 37
COPY CONSTRUCTOR
 It is a constructor which is invoked when an object of same
type given as argument.
 It is used initialize an object from an existing object.

S2N
OS
S2NOS
(S2
&a) NOS
-
S(

X,Y,SUM : int
S2NO

X , Y,
)

d()

SUM S2NOS()
+
ad

+
print()
S2NOS(S2NOS&a)
+ add() : void
+ print () : void
04/07/11 38
#include<iostream.h>
main()
S2NOS::S2NOS() {
{ x=10;y=20; }
S2NOS ob1;
S2NOS
S2NOS::S2NOS(S2NOS&a) ob2(ob1);
{this.x=a.x; this.y=a.y;} ob1.add();
ob1.print();
void S2NOS::add() }
{ sum=x+y; }
04/07/11 39
DESTRUCTOR
To clear the object dynamically allocated.
It is the counter part of constructor.
Used to deallocate memory
Called automatically when the object goes out of scope
Purpose is to clean up work memory before the object is
destroyed.
Designated by preceeding(~)
Can’t be static, const or volatile.
Don’t have new type nor they return values.
A destructor can be declared virtual or pure virtual

04/07/11 40
S2NOS
t,i
(in
S
S2N
O
N nt)
S2
OS - X,Y,SUM : int
X, ()

Y, +
S(

S2NOS()
()
O

SUM + S2NOS(int,int)
)
N

d
ad
~S2

+ add() : void
print()
+ print () : void
+ ~S2NOS()

04/07/11 41
#include<iostream.h>

main()
S2NOS::S2NOS() { x=10;y=20; }
{
S2NOS::S2NOS(int x,int y) S2NOS ob1;
{this.x=x; this.y=y;} S2NOS
ob2(20,25);
void S2NOS::add(){ sum=x+y; }
ob1.add();ob1.pri
void S2NOS::print()
nt();
{ cout<<sum; }
ob2.add();ob2.pr
04/07/11
S2NOS::~S2NOS(){} int(); 42
main() { S2NOS N,M,P; }

M
P

04/07/11 43
RAM

P
X Y SUM

M
X Y SUM

X Y SUM N

CODE
04/07/11 STACK 44
main()
{ c
int a=10,b=15,c=15;
int Y=Big(a,b,c); b
printf(“%d”,Y);
}
a
int Big (int x, int y, int z)
{ Returning
int t; t=a.x>a.y?a.x>a.z? Address
a.x:a.z:a.y>a.y:a.z;
return t;
} STACK

04/07/11 45
C++ evolved from structures and functions of “C
language”
Structure is nothing but a suitcase
Set of data from a function to another function.
It is used to group a different type of elements
together

04/07/11 46
typedef struct XYZ
Structure {
declaration int x,y,big;
}BIG;

BIG set()
{
BIG a;
function
a.x=10;a.y=20;a.z=30;
return a;
}
void print (BIG a)
{
printf(“%d %d %d %d”,a.x,a.y,a.z,a.big);
}

main(){
BIG a;
a=set();
main
a.big= t=a.x>a.y?a.x>a.z?a.x:a.z:a.y>a.y:a.z;
print(a);
04/07/11 } 47
typedef struct XYZ
Structure { int x,y,big;
declaration }BIG;

void set(BIG *);


Function void find(BIG *);
prototype void print(BIG *);

void set(BIG *a){a->a=10;a->y=20;a->z=30;}

Function void find(BIG *a){


Implemen a->big=a->x>a->y?a->x>a->z?a->x:a->z:a->y>a->y:a->z;
-tation }

Void print(BIG *a)


{ printf(“%d %d %d %d”,a->x,a->y,a->z,a->big);

main(){
BIG a;
main set(&a);
find(&a);
Print(&a)
04/07/11 48
}
TOWARDS CLASS
In structure funtions outside.
In Class functions are put inside the structure
CLASS

Structure typedef struct XYZ{


definition
int x,y,big;

void set(); void set(BIG*);


Function void find(); void find(BIG*);
prototype void print(); void print(BIG*);
}BIG;

void BIG::set(){x=10;y=20;z=30;}
Function
Implemen
void BIG::FIND(){big=x>y?x>z?x:z:y>z;}
-tation
04/07/11 void BIG::print(){printf(“%d%d%d%d”,x,y,z,big;);} 49
main() main()
{ {

BIG a; BIG a;
a.set(); a.set(&a);
a.find(); a.find(&a);
a.print(); a.print(&a);

} }

Compiler
changes
04/07/11 50
typedef struct XYZ
{
int x,y,z,big;
void set(); void set(BIG this&);
void find(); void find(BIG this&);
void print(); void print(BIG this&);
}BIG;

In all C++ functions find hidden argument is “this” pointer


04/07/11 51
void BIG::set(Big&this)
{
this.x=10;this.y=20;this.z=30;
}
void BIG::find(Big&this)
{
this.big=this.x>this.y?this.x>this.z?this.x:this.z:this.y>this.z;
}
void BIG::print(Big&this)
{
printf(“%d%d%d%d”,this.x,this.y,this.z,this.big;
}

04/07/11 52
CONSTRUCTOR:
Class S2NOS
{
int x,y,z;
public:
S2NOS(){
struct S2NOS this;
this.x=0;this.y=0;this.z=0;
return this;
}
};

return type of the constructor is structure (the class itself)


04/07/11 53
(In C++) class can be substituted by
structure it will works:
Structure-- default access specifier is
public
Class -- def aultaccess specifier is
private

The compiler by default specify the return type


of the constructor(this pointer)

04/07/11 54
CHILD ATTRIBUTE LEVEL
Mother Motherly love
Father Discipline
Grand parents Moral values(stories)
Television All about the world
Brothers/Sisters Give and take
Neighbours Cunning
Teachers Knowledge &
Discipline
Friends Habits
Lovers Sharing of hearts
Wife Commitment
Children Responsibility
04/07/11 55
 S2NOS
 S2NOS(int,int)
 S2NOS(int)
 S2NOS(s2nos)
 setx(int):void
 sety(int):void set y(int a){y=a;}
 setxy(int,int):void
 setobj(s2nos):void
 set():void
 getx():int
 gety():int;
 getsum():int;
 getobj();s2nos{return this}}
04/07/11 56
METHOD OVERLOADING(static polymorphism)

* One of the ways that C++ implements polymorphism


* Occurs if several methods have the same name but
different arguments

Must differ in
+ Number of parameters
+ Type

* May have different return types


* Return method alone is insufficient to distinguish two
versions of the method.

04/07/11 57
S2NOS
- x,y,sum;int

+ S2NOS + set():void
+ S2NOS(int) + set(int):void
+ S2NOS(int,int) + set(int):int
+ S2NOS(S2NOS)

04/07/11 58
04/07/11 59
04/07/11 60
INHERITANCE
The mechanism of deriving a new class from an old one.
The old class is known as-base class (or)super class (or) parent
class.
The new class is called an-sub class (or)derived class (or)child
class
It is one of the corner stages of oop
Super class is not superior to its sub class or contains more
functionality
In fact, sub class have more functionality than their super class
Super and sub class from the language set parent class is the
super set of the child class.

04/07/11 61
Reasons:

Reusability
Overridability
Extendability

04/07/11 62
REUSABILITY
class A2N
{ class child: public A2N
int x,y; {
void set(); };
void set(int,int);
int getx(); main()
int gety(); {
void print(); child t;
}; t.print();
void A2N::set(){x=100;y=150} t.set (100,200);
void A2N::set(int a,int b) t.print();
{x=a;y=b;} t.set();
int A2N::getx() {return x;} t.print();
int A2N::gety() {return y;} cout<<t.get x()<<t.gety();
void A2N::print() }
{cout<<x<<y;}
04/07/11 63
CONSTRUCTOR OF DERIVED
CLASS
- If the derived class constructor does not call
class child:public:A2A
{
child();
child(int x);
child(child x);
};
Child::child(): A2A(){}
Child::child(int x):A2A(x){}
Child::child(child x):A2A(x){}
Child not having any attributes but having only methods

04/07/11 64
EXTENDABILITY

A2N

OP2N

+ sum() : int sum() {return getx()+gety();}


+ subtract():int subtract(){return getx() - gety();}
+ prod():int prod(){return getx() * gety();}
+ div():int
div(){return getx() / gety();}

04/07/11 65
OVERRIDING METHODS

Defining methods in the derived class that has the same name,
same argument& same return types that is available in the base
class
 In derive class, responds differently to the same method that is
defined in the base class.
04/07/11 66
class base
OVERRIDING METHODS {
public:
void print()
{
printf(“ Hai ”);;
}
};
class derived : public base
Print() {
public:
void print()
{
printf(“*****************”);
printf(“ Hai Welcome”);;
printf(“*****************”);
}
Print()
}
main()
{
derived ob;
ob.print();
04/07/11 } 67
POLYMORPHISM

 Polymorphism means the ability to assume


many forms.
 The ability to have a single statement invoke
may different function.
 Late binding/Run time polymorphism.

04/07/11 68
class A2N{ void main()
protected: int x,y; {
public: A2N a;
a.set();
void set{x=10;y=20;} a.print();
void print{cout<<x<<y;}
}; A3N b;
class A3N:public A2N b.set();
b.print();
{
}
private: int z;
public:
void set(){x=100;y=200;z=300;} OUTPUT:
void print(){cout<<x<<y<<z;}
} x=10 y=20
x=100 y=200 z=300
04/07/11 69
class A2N{ void main() {
protected: int x,y; A2N *a;
public: a=new A2A;
a->set();
void set{x=10;y=20;}
a->print();
void print{cout<<x<<y;}
new-
}; Dynami
A3N *b;
c
b=new A3A; allocati
class A3N:public A2N b->set(); on
{ b->print();
private: int z; }
public:
void set(){x=100;y=200;z=300;} OUTPUT:
void print(){cout<<x<<y<<z;} x=10 y=20
x=100 y=200 z=300
04/07/11 70
class A2N{ void main() {
protected: int x,y; A2N *a;
a=new A2A;
public:
a->set();a->print();
void set{x=10;y=20;}
void print{cout<<x<<y;} A3N *b;
}; b=new A3A;
b->set();b->print();

class A3N:public A2N


a=b;
{ a->set();a->print(); }
private: int z;
public:
void set(){x=100;y=200;z=300;} OUTPUT:
void print(){cout<<x<<y<<z;} x=10 y=20
x=100 y=200 z=300
04/07/11
x=10 y=20 71
VIRTUAL void main() {
class A2N{ A2N *a;
protected: int x,y; a=new A2A;
public: a->set();a->print();
virtual void set{x=10;y=20;}
virtual void print{cout<<x<<y;} A3N *b;
}; b=new A3A;
b->set();b->print();
class A3N:public A2N
{ a=b;
private: int z; a->set();a->print(); }
public:
OUTPUT:
void set(){x=100;y=200;z=300;} x=10 y=20
void print(){cout<<x<<y<<z;} x=100 y=200 z=300
04/07/11
x=100 y=200 z=300 72
VIRTUAL FUNCTION

 A virtual function is a member that is


redefined in derived class.
 When a virtual function is called through
a pointer to a base class the derived class
version of the function is executed.

In java all base class methods become virtual.

04/07/11 73
PURE VIRTUAL FUNCTION

Parent class hold that the method is definitely defined in child class.
It is a virtual function with no body
Since it has no body the programmer must add the notation is for the of the
pure virtual function the base class

class base
{
virtual void set()=0;
virtual void disp(){}
};
04/07/11 74
ABSTRACT CLASS

A class is the one or more pure virtual functions.


The class having methods which are incomplete (no code)
Can only be extended
Can’t be instantiated
Behaves an super class
Object cannot be created

class A2N
{
virtual void set()=0;
virtual void print()=0;
};
04/07/11 75
final before the method is
stop the overridability

FINAL final before the variable is


KEYWORD constant

final before the class has no


child class

04/07/11 76
FRIEND FUNCTION

It is a function that can access the private members of a class


an through it were a member of that class

It is more convenient the grant member level

The friend keyword allows a function or class to gain access


to the private and protected member of a class

You can declare friend functions or friend classes to access


not only public members but also protected and private
members.

04/07/11 77
class Sum
{
int a,b;
public : main()
void test() { {
a=100; Sum e;
b=200; e.test();
}
cout<<“The
result:”<<compute (e);
friend int compute(Sum e1);
}
};
int compute (Sum e1)
{
return int(e1.a+e1.b)-5;
}
04/07/11 78
VIRTUAL BASE CLASS

A class which retains more than one copy in it’s


derived class is known as virtual base class.

VIRTUAL
A BASE
CLASS

B C

04/07/11 79
class A
Class D:public B, public C
{
{
protected: int a;
protected: int d;
public :
public:
void setA()
void add()
{ a=10; }
{
};
d=a+b+c;
}
class B:virtual public A
};
{
protected: int b;
public:
void setB() main()
{ b=20; } {
}; D obj;
Class C:virtual public A obj.setA();
{ obj.setB();
protected: int c; obj.setC();
public: obj.add();
void setC() }
{ c=30; }
};
04/07/11 80
04/07/11 81
My sincere thanks to…

V.Nirmala &
S.Keerthika
(for helping me to prepare this presentation)

04/07/11 82
04/07/11 83

Вам также может понравиться