Oops
Oops
Assistant Professor
PSG Tech
Unstructured Programming
Procedural Programming
Object Oriented Programming
Sequence of commands – Ex. Assembly
Language Program
Assembly Language VS Machine Language
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
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
11-03-2020 61
• A destructor never takes any argument nor does it
return any value.
existing classes
Reusability
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);
}
value of x is 7
value of x is 9.132
value of x and y is 85, 64
10
5
Friend class
Friend function
Virtual class
Abstract class
Exception Handling
Templates