08/16/2022

A constructor in is a special member function that is use to initialize the object of its class. It
is special because its name is the same as the class name. So, The constructor is invoke
whenever an object of its associate class is creat. It is call a constructor because it constructs the value of data members of the class. Then, The constructor doesn’t have a return type. Moreover, I do not specify a constructor C++ compiler that generates the default constructor.

Introduction to Parameterized Constructors

It may be necessary to initialize the various data elements of different objects with different
values when they are created. This can be achieved by passing arguments to the constructor
function when the objects are created. The constructor that can take arguments is called
parameterized constructors.

Then, Using C programming to perform this sorting operation you can follow any language.

So, try it your self on online compiler.

class integer
{
int m, n;
public:
integer(int , int); //parameterized constructor
..…
};
integer :: integer(int x, int y)
{
m = x;
n = y;
}
When a constructor has been parameterized, the object declaration statement such as
integer int1;

may not work. We must pass the initial values as arguments to the constructor
function when an object is created. This can be done in two ways:
By calling the constructor explicitly
By calling the constructor implicitly

Calling the constructor explicitly:

integer int1 = integer(5, 10);
This statement creates an integer object int1 and passes the values 5 and 10 to it.

Calling the constructor implicitly:

integer int1(5, 10);

This method, sometimes call the shorthand method, is used very often as it is
shorter, looks better and is easy to implement. When the constructor is parameterize, we
must provide appropriate arguments for the constructor.

The constructor function can also be define as inline function.

class integer
{
int m, n;
public:
integer(int x, int y)
{
m=x, n=y;
}
};

The parameters of a constructor can be of any type except that of the class to which it
belongs.

class A
{ ……
public :
A (A);
};

However, a constructor can accept a reference to its own class as a parameter.

class A
{ ……
public : 
A (A&);
};

is valid. In such case, the constructor is call the copy constructor.

Then, Using C programming to perform this sorting operation you can follow any language.

So, try it your self on online compiler.

#include<iostream.h>
#include<conio.h>
class Marks
{
public:
 int cpp;
 int java;
 //Parametrized Constructor
 Marks(int mark1,int mark2) 
{
 cpp= mark1;
 java= mark2;
 }
Void display() 
{
 cout << "CPP : " << cpp<<endl;
 cout << "Java :" << java<< endl; }
};
int main()
 {
 //invoke Parametrized Constructor
 Marks m(90,85);
 clrscr();
 m.display();
 getch();
 return 0;
}

Multiple Constructors in a class:

constructors having one name but different forms is call constructor overloading
So far we have used two kinds of constructors.
integer(); // No Argument
integer(int, int); // Two Arguments
In the first case, the constructor itself supplies the data values and no passed by the calling
program. In the second case, the function call passes the appropriate values from main().
C++ permits to use both the constructors in the same class.

#include <iostream.h>
#include<conio.h>
class construct
{
public:
 float area;
 // Constructor with no parameters
 construct()
 {
area = 100;
 }
 // Constructor with two parameters construct(int a, int b)
 {
area = a * b;
 }
 void disp()
 {
cout<< area<< endl;
 }
};
int main()
{
 construct o;
 construct o2( 10, 20);
 clrscr();
 o.disp();
 o2.disp();
 getch();
 return 1;
}
OUTPUT:
100
200

Parameterized Constructors with default arguments:

It is possible to define constructors with default arguments.
Ex.

The constructor integer() can be declared as follows:
integer( int x, int y=3);


The default value of the argument y=3. The statement for calling function such as
integer(5);
assigns 5 to x and 3 to y (by default). integer(5, 10); assigns 5 to x and 10 to y. The actual
parameter, when specified, overrides the default value. The missing arguments must be
trailing ones.

Parameterized Constructors with dynamic allocation:

Class objects can be initialize dynamically too. So, The initial value of an object may be
provided during run-time. Than, This provides the flexibility of using different format of data at
run-time depending upon the situation. Using overloading constructure one can provide
various initialization formates. This is fexibal use of different formate of data at run time
that is depends on situation.

#include <iostream.h> 
#include <conio.h> 
class employee
{ 
int empl_no;
float salary;
public:
employee() //default constructor
{}
employee(int empno,float s)//constructor with arguments
{
empl_no=empno;
salary=s;
}
employee (employee &emp)//copy constructor
{
cout<<"\ncopy constructor working\n";
empl_no=emp.empl_no;
salary=emp.salary;
}
void display ()
{cout<<"\nEmp.No:"<<empl_no<<"salary:"<<salary<<endl;
}
};
void main()
{
int eno;
float sal;
clrscr();
cout<<"Enter the employee number and salary\n";
cin>>eno>>sal;
employee obj1(eno,sal);//dynamic initialization of object
cout<<"\nEnter the employee number and salary\n";
cin>>eno>>sal;
employee obj2(eno,sal); //dynamic initialization of object
obj1.display(); //function called
employee obj3=obj2; //copy constructor called
obj3.display();
getch();
}
OUTPUT:
Enter the employee number and salary 
101 30000
Enter the employee number and salary
102 35000
Emp. NO :101 salary 30000
Emp.No 102 salary 35000

So, for more update going to –learning points – Home | A better learning future starts here

For more OOCP concept go for –Class and Object – Learning Points

Leave a Reply

Your email address will not be published.