Object Oriented Programming Using C++
Object Oriented Programming Using C++
using C++
Lecture 07
Constructors
Constructor
• When ever an object is created, a special
function is called
– The Constructor function
• ensure that each object is initialized properly
before it’s used in a program
• The constructor function is defined inside the
class body similar to other functions
• If a class does not explicitly include a
constructor, the compiler provides a default
constructor: a constructor with no parameters
2
Constructor
Constructor Syntax
Class Box
The name of the
{
constructor function
….. is same as that of
….. the class
Box(){
height=0;width=0;length=0;
} Constructor functions
}; have no return type
5
Constructor: Without Object
Constructor and Multiple Objects
Constructor: Example 2
Constructor: Example 2 Output
Constructor: Example 2 Output
Basic Characteristics
Constructor Types
• Default Constructor
– Also called no argument constructor
– A constructor with default parameter values
12
Default Constructor
• A constructor with no parameters
13
Constructor – Default
class Box { Box::Box()
private: {
int height; height = 10;
int width; width = 5;
int length; length = 5;
public: }
Box();
};
14
Parameterized Constructors
• Constructor is a function, so it can also have
parameters
• These parameters can be used to do any task
– Mainly for initializing the data members
– For any other desired functionality in the class
class Box {
private: Box::Box(int x, int y, int z){
int height; height = x;
int width;
int length;
width = y;
public: length = z;
Box(int,int,int); }
int volume();
};
15
Arguments to Constructors
• To pass the arguments to constructor we do not
need to call it explicitly.
• We pass the arguments to the object.
void main()
{
int a, b, c;
cin >> a >> b >> c;
Box B1(a,b,c);
}
16
Parameterized Constructors
#include <iostream> Rectangle::Rectangle (int a,
using namespace std; int b) {
width = a;
class Rectangle { height = b;
int width, height; }
public:
Rectangle (int,int);
int area () {return int main () {
(width*height);} Rectangle rect (3,4);
}; Rectangle rectb (5,6);
cout << "rect area: " <<
rect.area() << endl;
cout << "rectb area: " <<
rectb.area() << endl;
return 0;
} 17
Parameterized Constructors
class Box {
private: Box::Box(int x, int y, int z){
int height; height = x;
int width;
int length;
width = y;
public: length = z;
Box(int,int,int); }
int volume();
};
void main()
{
int a, b, c;
cin >> a >> b >> c;
Box B1(a,b,c);
Box B2;
}
Parameterized Constructors
Constructor with Default Arguments
• Similar to default arguments of functions,
constructors may also have default arguments
20
Constructor with Default Arguments
class Box { Box::Box(int a,int b,int c){
private: height=a;
int height; width=b;
int width; Default arguments values length=c;
int length; are entered here }
public:
Box(int a = 5,int b =10,int c = 5); int Box::cal_volume(){
int cal_volume(); return length*width*height;
}; }
int main(){
Box b;
b.cal_volume();
21
Constructor with Default initialization
Values
class Box {
– private:
int height;
int width;
int length;
– public:
Box();
Box(int hval=2, int wval=4,int lval=6);
int volume();
};
Box smallbox;
22
Constructor with Default initialization Values
• We will get an error message from the compiler
• We have an ambiguous call to an overloaded
function
• A call with default parameter values is
indistinguishable from a call to default constructor
with no parameters
• A constructor with default parameter values also
serves as default constructor
• If you get rid of the constructor with no parameters
than it will compile and execute ok.
23
Overloaded Constructors
Class Box
{ A class can have > 1
… constructor
Box(){
height=0;width=0;length=0;
}
Box(int h, int w, int l){
height = h;
width = w; Overloaded constructors
length = l; in a class must have
} different parameter lists
}; 24
Overloaded Constructors
class Box {
private:
int height; void main()
int width;
int length;
{
public: Box B2;
Box(){ Box B1(10, 15, 10);
height=0;width=0;length=0;
}
cout << B1. cal_volume();
Box(int h, int w, int l){ cout << B2. cal_volume();
height = h; }
width = w;
length = l;
}
int cal_volume(){
ret height*width*length;
}
}; 25
Constructors Exercise 1
29
Constructors Example
30
Initialization List
• When a constructor is used to initialize other members, these
other members can be initialized directly, without resorting to
statements in its body.
• This is done by inserting, before the constructor's body, a colon
(:) and a list of initializations for class members. For example,
consider a class with the following declaration:
31
Initialization List
32
Initialization List
• Constructor initialize private data members
– Through Assignment
• For initialization using assignment the data
member is created first and then initialized
– Through Initializer list
• The initial value is used to initialize the data
member (private) as it is created
33
Initialization List Example
Class Distance
{
private:
int feet;
float inches
public: Class Distance
Distance(){ {
feet=0; private:
inches = 0.0;
int feet;
}
float inches
};
public:
Distance() : feet(0), inches(0.0)
{}
};
Initialization List Example
Class Distance
{
private:
int feet;
float inches
public: Class Distance
Distance(int ft, float in){ {
feet = ft; private:
inches = in;
int feet;
}
float inches
};
public:
Distance(int ft, float in): feet(ft), inches(in)
{}
};
Initialization List Example
Class Distance
{
private:
int feet;
float inches
public: Class Distance
Distance(){ {
feet=0; private:
inches = 0.0;
int feet;
}
float inches
Distance(int ft, float in){
public:
feet = ft; Distance() : feet(0), inches(0.0)
inches = in; {}
} Distance(int ft, float in): feet(ft), inches(in)
} {}
}
Initialization List Example
Box::Box() : length(0),width(1),height(1)
{
cout<< “Box default constructor is called” << endl;
}
37