Professional Documents
Culture Documents
OOP
Object Initialization
• 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
• 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)
• 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
#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
#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);
#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?
#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 …
int main()
{
cars c1(2015);
cars c2(2016,”Civic”);
}
Out put:
Car Model 2015
Car Model 2016 Car Name Civic
Question?
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
• 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