Академический Документы
Профессиональный Документы
Культура Документы
Seminar on,
04/07/11 2
Deals with over all organization of the program.
04/07/11 3
ADVANTAGES OF OBJECT ORIENTED
PROGRAMMING
Program are easier to understand because data and programs are stored
together(meaning).
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
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)
04/07/11 9
Real World Objects
Living Non-Living
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
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 ?
04/07/11 15
DESIGN
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>
04/07/11 18
CLASS
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
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
2. INFORMATION HIDING
04/07/11 25
* ABSTRACTION
Hiding the details
* 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
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:
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 :
,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>
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;
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
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;
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;
}
};
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)
Must differ in
+ Number of parameters
+ Type
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
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
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();
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
class A2N
{
virtual void set()=0;
virtual void print()=0;
};
04/07/11 75
final before the method is
stop the overridability
04/07/11 76
FRIEND FUNCTION
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
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