oops seminar
TRANSCRIPT
-
8/7/2019 OOPS seminar
1/83
04/07/11 1
All are Welcome to
A
Seminar on,
ObjectOrientedProgramming
-
8/7/2019 OOPS seminar
2/83
04/07/11 2
WHAT IS AN OBJECT ORIENTED
ROGRAMMING?
It is a style of programming that represent a program as asystem 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.
-
8/7/2019 OOPS seminar
3/83
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
-
8/7/2019 OOPS seminar
4/83
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
-
8/7/2019 OOPS seminar
5/83
Enhanced programmers productivity because of reusability
oops:
[marriage takes place between data andcode]
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 largesystems.[Microsoft produce new OS for every 6 months]
04/07/11 5
-
8/7/2019 OOPS seminar
6/83
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 ofentries called object.
04/07/11 6
-
8/7/2019 OOPS seminar
7/83
KEY CONCEPTS OF OOP
1. Class2. Object
3. Information hiding and Data encapsulation
4. Abstraction
5. Constructor
6. Destructor7. 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
-
8/7/2019 OOPS seminar
8/83
04/07/11 8
-
8/7/2019 OOPS seminar
9/83
OBJECT
A tangible & visible entityAn 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.behaviours2.properties/Attributes]
04/07/11 9
-
8/7/2019 OOPS seminar
10/83
Real World Objects
Behaviors AttributesAttributes
Living Non-Living
Psychologi
cal
Physical
04/07/11 10
-
8/7/2019 OOPS seminar
11/83
REPRESENTING ANOBJECT
Objects Identity
Objects State
Objects Behavior
04/07/11 11
-
8/7/2019 OOPS seminar
12/83
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
-
8/7/2019 OOPS seminar
13/83
PROBLEM SOLVING
1. Statements
2. Understanding
3. Analysis4. Design
5. Implementation
6. Testing
INPUT PROCESSOUTPU
T
04/07/11 13
-
8/7/2019 OOPS seminar
14/83
1. OO Analysis
2. OO Design
3. OO Implementation
4. OO Testing
Ex:-
Sum of two numbers
04/07/11 14
-
8/7/2019 OOPS seminar
15/83
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
-
8/7/2019 OOPS seminar
16/83
X,Y,SUM : int
S2NOS
set() :void
add() :voidprint () :
void
++
+
-private
public
Objects State
Objects Behavior
Objects Identity
04/07/11 16
DESIGN
-
8/7/2019 OOPS seminar
17/83
class S2NOS
{private : int x,y,sum;
instance
variablespublic: void get();
void add(); Methods
void print();
};
Members ofThe
Class
04/07/11 17
DECLARATION
-
8/7/2019 OOPS seminar
18/83
#include
void S2NOS::get() { x=10;y=20; }
void S2NOS::add(){ sum=x+y; }
void S2NOS::print(){ cout
-
8/7/2019 OOPS seminar
19/83
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
-
8/7/2019 OOPS seminar
20/83
OBJECT CREATION &
TESTING
void main()
{
S2NOS ob;ob.set();
ob.add();
ob.print();
}
set()
add()
print()
X , Y,SUM
04/07/11 20
-
8/7/2019 OOPS seminar
21/83
OBJECT
Instance of a class. Can be uniquely identified by its 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
-
8/7/2019 OOPS seminar
22/83
OBJECTS Vs CLASS
Classes are blueprint.
Objects are instance of class.
04/07/11 22
-
8/7/2019 OOPS seminar
23/83
DATA HIDING AND ENCAPSULATION
set()
a
dd()
print()
X , Y,SUM
RAM
04/07/11 23
-
8/7/2019 OOPS seminar
24/83
- Objects nothing but an atom.Data is the nucleus.
Methods are electrons.
- from the diagramvariables make up the centre ot nucleus
of the object Method surround and hide the objects
nucleus from other objects.
Packing an objects variables within the
protective custody of its methods iscalled encapsulation.
(E.g.: Capsule)
04/07/11 24
-
8/7/2019 OOPS seminar
25/83
1. MODULARITY
* Objects can be written and maintainedindependently .
2. INFORMATION HIDING
* The object can maintain private attributes and
methods that can changed at any time without offeringthe other objects that depending on it.
04/07/11 25
-
8/7/2019 OOPS seminar
26/83
* 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
-
8/7/2019 OOPS seminar
27/83
ACCESS SPECIFIERS
* Specifies the accessibility of members
* private, protected, publicprivate :
* Members can be used only by member functions andfriend of the class.
protected :
* Member can be used by member function andfriends 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
-
8/7/2019 OOPS seminar
28/83
CONSTRUCTORS
main()
{
S2NOS ob;ob.set();
ob.add();
ob.print();}
X,Y,SUM : int
S2NOS
S2NOS()void
add();void print
();
+
+
+
-
04/07/11 28
-
8/7/2019 OOPS seminar
29/83
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
CONSTRUCTORS
-
8/7/2019 OOPS seminar
30/83
Cant be invoked explicitlyCannot 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 constructor3. Overloaded constructor
4. Copy constructor
04/07/11 30
-
8/7/2019 OOPS seminar
31/83
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 fieldwould be zeroed out.
all object values would point to nill
all Boolean would be false
04/07/11 31
-
8/7/2019 OOPS seminar
32/83
if you dont provide a constructor,the complier
provide default constructor
complier creates a default can only when your classhas no other constructor.
S2NO
S
()add()
print()
X , Y,SUM
X,Y,SUM : int
S2NOS
S2NOS()add() :
voidprint () :
void
+
+
+
-
04/07/11 32
-
8/7/2019 OOPS seminar
33/83
#include
S2NOS::S2NOS() { x=10;y=20; }
void S2NOS::add(){ sum=x+y; }
void S2NOS::print(){ cout
-
8/7/2019 OOPS seminar
34/83
PARAMETERIZED CONSTRUCTOR
It is a constructor which invoked, when the arguments
are passed at the time of object creation
S2NOS
()
add(
)
print()
X , Y,SUM
X,Y,SUM : int
S2NOS
S2NOS()
S2NOS(int,int)add() :
void
print () :
+
+
+
-
S2NOS(int,int)
+
04/07/11 34
-
8/7/2019 OOPS seminar
35/83
#include
S2NOS::S2NOS(){ x=10;y=20; }
S2NOS::S2NOS(int x, int y)
{this.x=x; this.y=y;}
void S2NOS::add(){ sum=x+y; }
main()
{
S2NOSob(10,15);
ob.add();
ob.print();
}
04/07/11 35
OVERLOADED CONSTRUCTOR :
-
8/7/2019 OOPS seminar
36/83
OVERLOADED CONSTRUCTOR :
* A Constructor behave differently at different situations.* A Constructor changes its functionality with respect to
their arguments.
X,Y,SUM : int
S2NOS
S2NOS()S2NOS(int)S2NOS(int,int)add() : void
print () : void
-
S2NOS
()
add()
print()
X ,
Y,SUM
S2NO
S(int,i
nt)
S2NOS(in
t)
++++
+04/07/11 36
-
8/7/2019 OOPS seminar
37/83
#include
S2NOS::S2NOS() { x=10;y=20; }
S2NOS::S2NOS(int x){this.x=x;y=30;}
S2NOS::S2NOS(int x, int y)
{this.x=x; this.y=y;}
void S2NOS::add(){ sum=x+y; }
void S2NOS::print()
main()
{
S2NOS ob1;S2NOSob2(25);
S2NOSob3(10,15);
ob.add();
ob.print();
}04/07/11 37
-
8/7/2019 OOPS seminar
38/83
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.
S2NOS(
)
a
dd()
print()
X , Y,SUM
X,Y,SUM : int
S2NOS
S2NOS()
S2NOS(S2NOS&a)add() : void
print () : void
+
+
+
-
S2NOS(S2NOS&a)
+
04/07/11 38
-
8/7/2019 OOPS seminar
39/83
#include
S2NOS::S2NOS(){ x=10;y=20; }
S2NOS::S2NOS(S2NOS&a)
{this.x=a.x; this.y=a.y;}
void S2NOS::add(){ sum=x+y; }
main()
{
S2NOS ob1;S2NOSob2(ob1);
ob1.add();
ob1.print();
}
04/07/11 39
-
8/7/2019 OOPS seminar
40/83
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(~)
Cant be static, const or volatile.
Dont have new type nor they return values.
A destructor can be declared virtual or pure virtual
04/07/11 40
-
8/7/2019 OOPS seminar
41/83
X,Y,SUM : int
S2NOS
S2NOS()S2NOS(int,int)add() : voidprint () : void~S2NOS()
-
S2NOS()
add()
print()
X ,Y,
SUM
S2NO
S(int,i
nt)
++
+
++
~S2NOS(
)
04/07/11 41
-
8/7/2019 OOPS seminar
42/83
#include
S2NOS::S2NOS() { x=10;y=20; }
S2NOS::S2NOS(int x,int y){this.x=x; this.y=y;}
void S2NOS::add(){ sum=x+y; }
void S2NOS::print(){ cout
-
8/7/2019 OOPS seminar
43/83
M
main() { S2NOS N,M,P; }
P
N
04/07/11 43
-
8/7/2019 OOPS seminar
44/83
CODE STACK
X Y SUM
X Y SUM
X Y SUM
P
M
N
RAM
04/07/11 44
-
8/7/2019 OOPS seminar
45/83
main(){
int a=10,b=15,c=15;
int Y=Big(a,b,c);
printf(%d,Y);
}
int Big (int x, int y, int z)
{
int t; t=a.x>a.y?a.x>a.z?a.x:a.z:a.y>a.y:a.z;
return t;
}
c
b
a
Returning
Address
STACK
04/07/11 45
-
8/7/2019 OOPS seminar
46/83
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
t pedef str ct XYZ
-
8/7/2019 OOPS seminar
47/83
Structure
declaration
typedef struct XYZ
{
int x,y,big;
}BIG;
BIG set()
{
BIG a;
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);
}
function
main
main(){
BIG a;
a=set();
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
-
8/7/2019 OOPS seminar
48/83
-
8/7/2019 OOPS seminar
49/83
TOWARDS CLASS
In structure funtions outside.
In Class functions are put inside the structure
Structure
definition
CLASS
Function
prototype
typedef struct XYZ{
int x,y,big;
void set();
void find();
void print();
}BIG;
void set(BIG*);
void find(BIG*);
void print(BIG*);
Function
Implemen
-tation
void BIG::set(){x=10;y=20;z=30;}
void BIG::FIND(){big=x>y?x>z?x:z:y>z;}
void BIG::print(){printf(%d%d%d%d,x,y,z,big;);}04/07/11 49
-
8/7/2019 OOPS seminar
50/83
main()
{
BIG a;
a.set();a.find();
a.print();
}
main()
{
BIG a;
a.set(&a);a.find(&a);
a.print(&a);
}
Compilerchanges
04/07/11 50
-
8/7/2019 OOPS seminar
51/83
typedef struct XYZ
{
int x,y,z,big;
void set();
void find();
void print();
}BIG;
void set(BIG this&);
void find(BIG this&);void print(BIG this&);
In all C++ functions find hidden argument is this pointer
04/07/11 51
-
8/7/2019 OOPS seminar
52/83
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
-
8/7/2019 OOPS seminar
53/83
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
-
8/7/2019 OOPS seminar
54/83
(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
-
8/7/2019 OOPS seminar
55/83
Mother Motherly love
Father DisciplineGrand parents Moral values(stories)
Television All about the world
Brothers/Sisters Give and take
Neighbours Cunning
Teachers Knowledge &Discipline
Friends HabitsLovers Sharing of hearts
Wife Commitment
Children Responsibility
CHILD ATTRIBUTE LEVEL
04/07/11 55
-
8/7/2019 OOPS seminar
56/83
S2NOS
S2NOS(int,int)S2NOS(int)S2NOS(s2nos)setx(int):void
sety(int):void set y(int a){y=a;}setxy(int,int):voidsetobj(s2nos):voidset():void
getx():intgety():int;getsum():int;getobj();s2nos{return this}}04/07/11 56
METHOD OVERLOADING(static polymorphism)
-
8/7/2019 OOPS seminar
57/83
* One of the ways that C++ implements polymorphism
* Occurs if several methods have the same name butdifferent 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.
METHOD OVERLOADING(static polymorphism)
04/07/11 57
-
8/7/2019 OOPS seminar
58/83
+ S2NOS
+ S2NOS(int)
+ S2NOS(int,int)
+ S2NOS(S2NOS)
+ set():void
+ set(int):void
+ set(int):int
S2NOS
- x,y,sum;int
04/07/11 58
-
8/7/2019 OOPS seminar
59/83
04/07/11 59
-
8/7/2019 OOPS seminar
60/83
04/07/11 60
-
8/7/2019 OOPS seminar
61/83
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 classSuper and sub class from the language set parent class is the
super set of the child class.
04/07/11 61
-
8/7/2019 OOPS seminar
62/83
Reasons:
Reusability
Overridability
Extendability
04/07/11 62
REUSABILITY
-
8/7/2019 OOPS seminar
63/83
main()
{child t;
t.print();
t.set (100,200);
t.print();
t.set();
t.print();
cout
-
8/7/2019 OOPS seminar
64/83
CONSTRUCTOR OF DERIVEDCLASS
- If the derived class constructor does not callclass 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
-
8/7/2019 OOPS seminar
65/83
EXTENDABILITY
A2N
OP2N
+ sum() : int+ subtract():int+ prod():int+ div():int
sum() {return getx()+gety();}
subtract(){return getx() - gety();}
prod(){return getx() * gety();}
div(){return getx() / gety();}
04/07/11 65
-
8/7/2019 OOPS seminar
66/83
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
OVERRIDING METHODSclass base{
-
8/7/2019 OOPS seminar
67/83
Print()
Print()
OVERRIDING METHODS {public:
void print()
{
printf( Hai );;
}
};
class derived : public base
{
public:
void print()
{
printf(*****************);
printf( Hai Welcome);;
printf(*****************);
}
}
main()
{
derived ob;
ob.print();
}04/07/11 67
-
8/7/2019 OOPS seminar
68/83
POLYMORPHISM
Polymorphism means the ability to assumemany forms.
The ability to have a single statement invokemay different function.
Late binding/Run time polymorphism.
04/07/11 68
-
8/7/2019 OOPS seminar
69/83
class A2N{
protected: int x,y;
public:void set{x=10;y=20;}
void print{cout
-
8/7/2019 OOPS seminar
70/83
04/07/11 70
void main() {
A2N *a;
a=new A2A;
a->set();a->print();
A3N *b;
b=new A3A;b->set();
b->print();
}
class A2N{
protected: int x,y;
public:
void set{x=10;y=20;}
void print{cout
-
8/7/2019 OOPS seminar
71/83
void main() {
A2N *a;
a=new A2A;
a->set();a->print();
A3N *b;
b=new A3A;
b->set();b->print();
a=b;
a->set();a->print(); }
04/07/11 71
class A2N{
protected: int x,y;
public:
void set{x=10;y=20;}
void print{cout
-
8/7/2019 OOPS seminar
72/83
04/07/11 72
class A2N{
protected: int x,y;
public:
virtual void set{x=10;y=20;}
virtual void print{cout
-
8/7/2019 OOPS seminar
73/83
VIRTUAL FUNCTION
A virtual function is a member that isredefined in derived class.
When a virtual function is called through
a pointer to a base class the derived classversion of the function is executed.
04/07/11 73
In java all base class methods become virtual.
-
8/7/2019 OOPS seminar
74/83
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
04/07/11 74
PURE VIRTUAL FUNCTION
class base
{
virtual void set()=0;
virtual void disp(){}
};
-
8/7/2019 OOPS seminar
75/83
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
Cant be instantiated
Behaves an super class
Object cannot be created
04/07/11 75
class A2N
{
virtual void set()=0;
virtual void print()=0;
};
-
8/7/2019 OOPS seminar
76/83
finalbefore the method isstop the overridability
final before the variable isconstant
final before the class has nochild class
04/07/11 76
FINAL
KEYWORD
-
8/7/2019 OOPS seminar
77/83
FRIEND FUNCTION
It is a function that can access the private members of a classan 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 accessto the private and protected member of a class
You can declare friend functions or friend classes to accessnot only public members but also protected and privatemembers.
04/07/11 77
class Sum
-
8/7/2019 OOPS seminar
78/83
class Sum
{
int a,b;
public :
void test() {
a=100;
b=200;
}
friend int compute(Sum e1);
};
int compute (Sum e1){
return int(e1.a+e1.b)-5;
}
04/07/11 78
main()
{
Sum e;
e.test();
cout
-
8/7/2019 OOPS seminar
79/83
04/07/11 79
A
B C
D
VIRTUAL BASE CLASS
A class which retains more than one copy in itsderived class is known as virtual base class.
VIRTUALBASECLASS
class A
{Class D:public B, public C
-
8/7/2019 OOPS seminar
80/83
04/07/11 80
{
protected: int a;
public :
void setA()
{ a=10; }};
class B:virtual public A
{
protected: int b;
public:void setB()
{ b=20; }
};
Class C:virtual public A
{
protected: int c;public:
void setC()
{ c=30; }
};
{
protected: int d;
public:
void add()
{
d=a+b+c;
}
};
main()
{
D obj;
obj.setA();
obj.setB();
obj.setC();
obj.add();
}
-
8/7/2019 OOPS seminar
81/83
04/07/11 81
-
8/7/2019 OOPS seminar
82/83
04/07/11 82
My sincere thanks to
V.Nirmala &S.Keerthika
(for helping me to prepare this presentation)
-
8/7/2019 OOPS seminar
83/83