Parametrized Constructor – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

parametrized constructorParametrized Constructor – Reference Note
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Parameterized Constructor
We can write a constructor in C++ which can accept parameters for its invocation. Such constructor that can take arguments are called parameterized constructor. In other words; “Declaring a constructor with arguments hides the default constructor”. This means that we can always specify the arguments whenever we declare an instance of the class. To illustrate parameterized constructor well, let us write the syntax and take one example;

Syntax

class classname
{
//data
Public:
classname(arguments);       //parametrized constructor
…………………….
…………………….
};

Now let us take one example;
class student
{
char name[20];
int rno;
public:
student(char, int);       //parametrized constructor
};
student::student(char n, int r)
{
name=n;
rno=r;
}

In the above example parameterized constructor has been shown by comment line. When the constructor is parameterized, we must provide appropriate arguments for the constructor.

(Visited 132 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary