You are on page 1of 32

Lecture 3

OOP
Object Initialization

• When an object is created it’s data members are initialized.


• What could be the value?
• An uninitialized variable typically contains a “garbage” value.
• What if the object already has some initial values of data when its
created?
• These values can be used default till data gets replacement
values!
Constructor

• Each class you declare can provide a constructor that can be used
to initialize an object’s values
• A constructor is a special member function that must be defined
with the same name as the class
• Why same name?
Constructor

• Compiler can distinguish it from the class’s other member


functions
• important difference between constructors and other functions is
that constructors cannot return values
• Normally, constructors are declared public.
Constructor

• C++ requires a constructor call for each object that is created


• This ensure that each object is initialized before it’s used in a
program.
• The constructor call occurs implicitly when the object is created
• If a class does not explicitly include a constructor, the compiler
provides a default constructor.
• It would be a constructor with no parameters.
• So when an object is initialized, a constructor will be called.
Constructor

• For data members that are objects of other classes, the default
constructor implicitly calls
• each data member’s default constructor to ensure that the data
member is initialized properly.
Two Ways to Provide a Default
Constructor for a Class
• Any constructor that takes no arguments is called a default
constructor.
(1)
• The compiler implicitly creates a default constructor in a class
that does not define a constructor
• Such a constructor does not initialize the class’s data members.
• All the default constructors initialized for each data member that
are object of another class.
(2)

• Explicitly define a constructor that takes no arguments.


• Such a default constructor will call the default constructor for
each data member
• That is an object of another class and will perform additional
initialization specified by you.
Defining a Constructor

• Know that the constructor has the same name as its class
• A constructor specifies data in its parameter list to perform
initialization
• When you create a new object, you place this data in the
parentheses that follow the object name
• Second point represent inside class and third point represent
inside main.
Note

•If you define a constructor with


arguments, C++ will not
implicitly create a default
constructor for that class
Defining a constructor - 1

#include <iostream>
using namespace std;
class cars
{
public:
cars()
{
//initialization of data members
}

};
Defining a constructor - 2

#include <iostream>
using namespace std;
class cars
{
public:
cars();
};
cars::cars()
{
//initialization of data members
}
Defining Constructor with arguments

#include <iostream>
using namespace std;
class cars
{
int c_model;
c_model();
public:
cars(int model)
{
//We set default value for cars model
c_model = model;
}

};
Calling a Constructor

• Constructor can be called in two ways.

• Calling its name with object


cars a =cars(2016);
• Calling directly with object
cars a(2016); //Commonly practiced
Recall from Last Lecture

#include <iostream>
using namespace std;

class queue
{
int qval;
public:
void setval(int v)
{
qval = v;
}

Example….

int getval()
{
return qval;
}
};
Example….

int main()
{
queue obj1, obj2;
obj1.setval(5);
obj2.setval(6);
obj1.setval(4);

cout << obj1.getval()<<" " <<obj2.getval() <<" " <<obj1.getval();


}
Output: 4 6 4
Example with constructor

#include <iostream>
using namespace std;
class queue
{
int qval;
public:
void print();
int getval();
queue(int val)
{
setval(val);
}
Example …

void setval(int v)
{
qval = v;
}
};

int queue::getval()
{
return qval;
}
Example …

int main()
{
queue obj1(5), obj2(6);
obj2.setval(4);
cout << obj1.getval() << " " << obj2.getval() << " " <<
obj1.getval();
}

Output?
Is it possible?

• Can multiple objects have different type of data members?


• Can one object use one data member while other implements 3
data members from the same class?
• For example one object uses one Integer argument, while other
uses float and String arguments.
• If yes what does that means?
• Multiple initialization depending upon the object?
Constructor Overloading

• More than one constructor can be defined in a class


• Or a class can have more the one constructor
• Multiple constructors are defined when there is need of multiple
objects with different types of arguments.
• Multiple constructor are defined when there is need of multiple
objects with different no. of arguments.
• Objects using different set of arguments or different no of
arguments during data member initialization is called Constructor
Overloading.
Constructor Overloading

• When we define multiple constructors


• Their names will be same as the class name
• No. of arguments will change
• Type of arguments will change
Constructor Overloading Example

#include <iostream>
using namespace std;
class cars
{
int c_model;
String c_name;
public:
cars(int cmodel)
{
cout << “Car Model ” << cmodel;
c_model = cmodel; // For further use
}
Example …

cars(int cmodel, String cname)


{
cout << “Car Model ” << cmodel;
cout << “Car Name ” << cname;
c_model = cmodel; // For further use
c_name = cname;
}
Example …

int main()
{
cars c1(2015);
cars c2(2016,”Civic”);
}
Out put:
Car Model 2015
Car Model 2016 Car Name Civic
Question?

• What if we want to replace values in the run time?


• What if we want to replace values given by user?
Function Overloading

• Function overloading is exactly


same as constructor
overloading
• Confused?
Difference between Constructor and
Function

Constructor Function
• Constructor name must be • functions cannot have same
same as class name name as class name.
• Constructor does not have • Function usually have a return
return type
• Constructor calls once at the • functions can be called with
time of object objects multiple times
Function overloading

• When function overloading is needed


• Function name will remain same
• No. of arguments will change
• Type of arguments will change
Why of Function Overloading?

• The function can perform different operations and hence


eliminates the use of different function names for the same kind
of operations.
• Program becomes easy to understand.
• Easy maintainability of the code.
• Function overloading brings flexibility in C++ programs.
• Function behaviors change according to data.
But there is one major issue too!!
Function Overloading Example

• Let’s write a program that will have a class with two functions
that will calculate average of 3 subjects. However one function
will calculate average with whole numbers. Other will consider
fraction values. Also do initialization through constructor.
• Initial values will be 0,0,0 & 0.0,0.0,0.0

You might also like