C++ parameterized constructor

C++ parameter constructor

The constructor integer(), defined above initializes the data members of all the objects to zero. whenever in practice it may be necessary to initialize. The various data elements of different objects with different values when they are create. c++ language permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that take arguments are known parameterized constructor

The constructor integer() may be modified to take arguments show below

class integer
{
int m,n;
public:
integer(int x,int y); // parameterized constructor
……….
……….
};
integer::integer(int x,int y)
{
m=x;
n=y;
}

when a constructor has been parameterized, the object declaration of integer class statement such as integer int1;
may not be work. we have to pass the initial values as arguments to the constructor when an object is declared.

This can be done in 2 types

1. by calling the constructor explicitly
2. by calling the constructor implicitly

The following declaration illustrates the first method

integer int1=integer(0,100); // explicitly call

This statement creates an integer object int1 and passes the values zero (0) and 100(Hundred) to it. The second is implemented as follows

integer int1(0,100); //this is implicit calling

This method, sometimes called the shorthand method is used very often as it is shorter looks better and is easy to implement

Remember , when the constructor is parameterized, we must provide appropriate arguments for the constructor.

Class with CONSTRCUTOR

#include<iostream>
using namespace std;
class integer
{
int m,n;
public:
integer(int,int); // constructor declared
void display()
{
cout <<"m="<<m<<"\n";
cout <<"n="<<n<<"\n";
}
};
integer:: integer(int x,int y) // define constructor
{
m=x;
n=y;
}
int main()
{
integer int1(0,100); // constructor called implicitly
integer int2=integer(25,75); // constructor called explicitly
std::cout<<"\n object1" << "\n";
int1.display();
std::cout<< "\n object2" << "\n";
int2.display();
return 0;
}

output:
object1
m=0
n=100
object2
m=25
n=75

The constructor function can also be defined as inline functions examples

class integer
{
int m,n;
public:
integer(int x,int y) // inline constructor
{
m=x;
y=n;
}
…….
…….
};

The paramters of a constructor can be any type except that of the class to which it belongs to for example

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

Thus the statement

is illegal
because however, a constructor can accept a reference to its own class as a parameter.

Class All
{
……
……
public:
A(A&);
};

is valid in such cases the constructor is called the copy constructor

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise