0% found this document useful (0 votes)
6 views

Constructors

Uploaded by

kalasingayvonne
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Constructors

Uploaded by

kalasingayvonne
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object Oriented Programing MSc.

Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University Collage

Constructor in C++
What is constructor in C++ programming and its purpose?

A constructor is a member function of a class which initializes object of a class. In C++


constructor automatically called when object (instance of class) create. It is special member
function of the class.

The main purpose of the class constructor is to construct an object of the class. In other word, it
is used to initialize all class data members.

How constructors are different from a normal member function?

A constructor is different in following ways:

• Constructor has same name as the class itself.


• Constructor don’t have return type.
• A constructor is automatically called when an object is created.
• If we don’t specify a constructor, C++ compiler generates a default constructor for us
(expects no parameters and has an empty body).

1
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University Collage

1. : : A constructor that accepts no parameters is known as default


constructor. If no constructor is defined then the compiler
supplies a default constructor.
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
construct() // Default Constructor
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically when the object is created
construct c;
cout << "a: " << c.a << endl;
cout << "b: " << c.b<< endl;
return 1;
}
Output:

2
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University Collage

2. : A constructor that receives arguments/parameters, is


called parameterized constructor.

#include <iostream>
using namespace std;
class Point
{
private:
int a, b;
public:
Point(int i, int j) // Parameterized Constructor
{
a = i;
b = j;
}
void show()
{
cout<<"a=" <<a<<endl;
cout<<"b=" <<b<<endl;
}
};
int main()
{
Point p1(10, 15); // Constructor called
p1.show();
return 0;
}
Output:

3
Object Oriented Programing MSc. Abeer Mohammed
Computer Engineering Department -2nd Stage Al- Esraa University Collage

Uses of Parameterized constructor:

1. It is used to initialize the various data elements of different objects with different values
when they are created.
2. It is used to overload constructors.
A constructor that initializes an object using values of another object
passed to it as parameter, is called copy constructor. It creates the copy
of the passed object.

Home Work

1. Design OOP program that include class name (Room) as illustrated in following chart ,
then create object from the int main that using constructor to assigning private values and
calling, show()

2. Design OOP program that include class name (Distance) as illustrated in following chart ,
then create object from the int main that using constructor to assigning private values and
calling, getdist(), showdist()

You might also like