0% found this document useful (0 votes)
14 views104 pages

Oops

Uploaded by

Kishore Kumar
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)
14 views104 pages

Oops

Uploaded by

Kishore Kumar
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/ 104

Sathya B

Assistant Professor
PSG Tech
 Unstructured Programming
 Procedural Programming
 Object Oriented Programming
 Sequence of commands – Ex. Assembly
Language Program
 Assembly Language VS Machine Language

 Intermediate between high level language and


Machine Language
 Set of functions / procedures
 Collection of Objects that communicate
through messages
 Object – basic unit of OOP
◦ Property or attribute - Characteristics of certain object
◦ Method - an action performed by an object

 Class - a category of similar objects (UDD)


 A class is a prototype, idea, and blueprint for
creating objects.
 An object is an instance of a class.
Syntax Example
 Local Variable

◦ Declared inside the

class method

 Instance/Class

/Member variable

◦ Member of a class
Syntax Example
Syntax Example
 It is very common for some part of an
object to require initialization before it
can be used.
 Suppose you are working on 100's of objects
and the default value of a particular data
member is needed to be zero.
 Initialising all objects manually will be very
tedious job.
 Instead, you can define a constructor function
which initialises that data member to zero. Then
all you have to do is declare object and
constructor will initialise object automatically. 35
11-03-2020
 Member function with same name as class
used to initialize objects of a class
 Allocate memory for objects
 Called automatically when object is created
 There is no need to write any statement to
invoke the constructor function.
 If a ‘normal’ member function is defined for
initialization, we need to invoke that function
for each and every objects separately.
 The default constructor for class A will be A :
:A( )

37
11-03-2020
 They must be declared in the public scope.
 They are invoked automatically when the objects are created.
 They do not have return types, not even void and they
cannot return values.
 They cannot be inherited, though a derived class can call the
base class constructor.
 Like other C++ functions, Constructors can have
default arguments.
 Constructors can not be virtual.

11-03-2020 38
 We can not refer to their addresses.
 An object with a constructor (or destructor) can not be
used as a member of a union.
 They make ‘implicit calls’ to the operators new and
delete when memory allocation is required.

11-03-2020 39
 Default

 Parameterized

 Copy

Example Types
 The constructor function is responsible for
creation of object.
 But in previous examples, we have not
defined any constructor in class, so how
come the objects were created of those
classes?
 The answer is, If no constructor is defined in
the class in such situation the compiler
implicitly provides a constructor, which is
called as default constructor.

11-03-2020 41
class sample class sample
{ {
int int
someDataMember; someDataMember;
public: public :
void someFunction sample()
After {
() Compilati
{ on
}
.. void someFunction
.. ()
} {
}; ..
..
}
Compiler has implicitly added a constructor
}; to the class,
which has
empty body, because compiler is not supposed to put any
11-03-2020 42
logic in that.
 A constructor without any parameter is
known as non-arg constructor or
simply default constructor.
 If no constructor is defined in a class,
then compiler implicitly provides an
empty body constructor which is called
as default constructor.

11-03-2020 43
class circle
{ • In the example behind
float radius; the constructor takes no
public: argument and simply
circle() initializes the radius to
{
zero
radius = 0;
}
}; • Non-arg constructor is
also called as default
constructor.

11-03-2020 44
class circle  In example beside, we
{ have not defined any
float radius;
public: constructor, so
compiler will provide
}
an empty body
;
constructor to the class,
class circle which is called as
{
float radius; default constructor.
public:
circle()
{
}
};
45
11-03-2020
class circle
{
float radius; Non-Arg (Default)
public: constructor,
circle() which takes no
{ arguments
radius
= 0;
}
circle(float r)
{ Parametirised
radius = r; constructor, which
} takes 1 arguments as
} radius.
;

46
11-03-2020
class circle
{
 When a constructor is
float radius; Parameterized,we must
public: pass the to the
circle()
{ arguments constructor
radius = function when an
0; declared.
}
object is
circle(float r)
{  Consider following
radius = r;
} declaration circle
}; firstObject;
circle secondObject(15);

47
11-03-2020
 Sometimes it becomes necessary to
initialize the various data elements of an
objects with different values when they are
created.
 This is achieved by passing arguments to
the constructor function when the objects
are created
 The constructor that takes arguments are
called parameterized constructors

11-03-2020 48
Default Parameterized
class circle
{
o Implicit call (shorthand
float radius; method)
public:  circle ob(7.6);
circle()
{ o Explicit call
radius =
 circle ob;
0;
}  ob = circle(7.6);
circle(float r)
{
radius
= r;
}
};

51
11-03-2020
class add
{  The first constructor
int m, n ; public : receives no arguments.
add ( ) {m = 0 ; n = 0 ;}
add (int a, int b)  The second constructor
{m = a ; n = b ;} receives two integer
add (add & i) arguments.
{m = i.m ; n = i.n ;}
 The third constructor
}; receives one add object
as an argument.

11-03-2020 52
class add
{
int m, n;  Add a1;
public :  Would automatically
add ( ) invoke the first
{m = 0 ; n = 0 ;} constructor and set both
add (int a, int b) m and n of a1 to zero.
{m = a ; n = b ;}  Add a2(10,20);
add (add & i)  Would call the second
{m = i.m ; n = i.n ;} constructor which will
}; initialize the data members
m and n of a2 to 10 and 20
respectively.
11-03-2020 53
class add  Add a3(a2);
{  Would invoke the
int m, n; third constructor
which copies the
public : values of a2 into a3.
add ( )
 This type of constructor
{m = 0 ; n = 0 ;} is called the “copy
add (int a, int b) constructor”.
{m = a ; n = b ;}
 Constructor Overloading
add (add & i)
 More than one
{m = i.m ; n = i.n ;}
constructor function
}; is defined in a class.

11-03-2020 54
Destructor

 Class function which destroys objects when scope


of object ends
DEFINITION
C++ destructor is a special member function that
is executed automatically when an object is destroyed
that has been created by the constructor. C++
destructors are used to de-allocate the memory that
has been allocated for the object by the constructor.

SYNTAX
Its syntax is same as constructor except the fact that it
is preceded by the tilde sign.

~class_name() { };
11-03-2020 56
STRUCTURE

/*...syntax of destructor....*/
class class_name
{
public:
class_name();
//constructor.
~class_name(); //destructor.
} 11-03-2020 57
EXAMPLE

11-03-2020 58
11-03-2020 59
IMPORTANT POINTS

A destructor function is called


automatically when the object goes out of
scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called 11-03-2020 60
• There can only one destructor in a class with classname
preceded by ~, no parameters and no return type.
• If we do not write our own destructor in class,
compiler creates a default destructor for us. The
default destructor works fine unless we have
dynamically allocated memory or pointer in class.
When a class contains a pointer to memory allocated
in class, we should write a destructor to release
memory before the class instance is destroyed. This
must be done to avoid memory leak.

11-03-2020 61
• A destructor never takes any argument nor does it
return any value.

• It will be invoked implicitly by the compiler upon


exit from the program – or block or function as the
case may be – to clean up storage that is no
longer accessible.

 It is a good practice to declare destructors in a


program since it releases memory space for
further use.

 Whenever new is used to allocate memory in the


constructor, we should use delete to free that 62
11-03-2020
memory.
Private members are
accessed by class methods
 Provides only essential information to the
outside world and hiding irrelevant
information
 Example:
 Process of creating new class from

existing classes

 Base/Parent/Super class - Inherited

 Child/Derived/Sub class – Inherits

 Reusability

 All members except private are

inherited
Syntax Example
 Based on access modifier
◦ Public
◦ Private
◦ Protected

Visibility
Modes
 Based on # base class
set_data() and area()
are public methods so
it can be accessed
from main()
Hybrid Inheritance is
combination of two or
more types of
inheritance
Predict
the
O/P
Parameterized
constructor of base
class is called from
derived explicitly
Predict
the
O/P
 Defining the base class methods in the
derived class

Base class’s
overridden method
can be accessed using
Scope Resolution
Operator
mother::display();
 Many Forms

 Types
◦ Static / early binding / Compile time polymorphism
◦ Dynamic / late binding / Run time polymorphism
 Method / Function
overloading
◦ Same method name with
 different # parameters
 different type of
parameters
 different order of
parameters
 Operator overloading
◦ Same operator different
functions

Types Example
EXAMPLE

#include <iostream>
using namespace std;
class Add int main()
{ {
public: Add obj;
int sum(int num1, int num2) //This will call the first function
{ return num1+num2; } cout<<"Output: "<<obj.sum(10,
20)<<endl;
int sum(int num1, int num2, int //This will call the second function
num3) cout<<"Output: "<<obj.sum(11,
{ return num1+num2+num3; 22, 33);
}
return 0;
}; 10
2
OUTPUT

Output: 30
Output: 66

10
3
// C++ program for function int main() {
overloading
#include <bits/stdc++.h> Geeks obj1;
using namespace std;
class Geeks
{ // Which function is
public: called will depend on the
// function with 1 int parameter parameters passed
void func(int x) // The first 'func' is
{
cout << "value of x is " << x << endl;
called
} obj1.func(7);
// function with same name but 1 double
parameter // The second 'func'
void func(double x) is called
{
cout << "value of x is " << x << endl; obj1.func(9.132);
}

// function with same name and 2 int parameters


// The third 'func' is
void func(int x, int y) called
{ obj1.func(85,64);
cout << "value of x and y is " << x << ", "
<< y << endl; return 0;
} } 10
};
4
OUTPUT

value of x is 7
value of x is 9.132
value of x and y is 85, 64

In the above example, a single function named func acts


differently in three different situations which is the property
of polymorphism.

10
5
 Friend class
 Friend function
 Virtual class
 Abstract class
 Exception Handling
 Templates

You might also like