Parameterized constructor in C++

What is parameterized constructor ?

Let us learn about parameterized constructor in C++ more, in this article.  In C++, there are three types of constructor namely default constructor, parameterized constructor and copy constructor.
parameterized constructor in C++

Benefits of parameterized constructor

  • Parameterized constructor helps in initializing each data member of the class to different values.
  • Parameterized constructor is helpful for constructor overloading.

Parameterized Constructor (Inside Class)

Syntax

class class_name 
{ 
    public: class_name(variables) //Parameterized constructor declared
    { 
        // initialization of variables
    } 
};

Example Code

#include
using namespace std; 
  
class PrepInsta 
{ 
private: 
    string name;
    int age;
  
public:
    PrepInsta(string val1, int val2)// Parameterized Constructor 
    { 
        name = val1;
        age = val2;
    } 
    
    string getName(){ 
        return name; 
    }
    int getAge(){ 
        return age; 
    } 
    
};

int main() 
{ 
    // Paramterized Constructor called
    PrepInsta obj("John Snow", 25);
    
    cout << "Name: " << obj.getName() << "\nAge: " << obj.getAge(); 
  
    return 0;
}

Output

Name: John Snow
Age: 25

Parameterized Constructor (Outside Class)

Code

#include<iostream>
using namespace std; 
  
class PrepInsta 
{ 
private: 
    string name;
    int age;
  
public:
    // Parameterized Constructor declaration
    PrepInsta(string name, int age);
    
    string getName(){ 
        return name; 
    }
    int getAge(){ 
        return age; 
    } 
    
};
// declaration outside class
PrepInsta::PrepInsta(string val1, int val2)// Parameterized Constructor 
{ 
        name = val1;
        age = val2;
} 

int main() 
{ 
    // Paramterized Constructor called
    PrepInsta obj("John Snow", 25);
    
    cout << "Name: " << obj.getName() << "\nAge: " << obj.getAge(); 
  
    return 0;
}

Output

Name: John Snow
Age: 25

Syntax

class class_name 
{
//declaration class_name(variables);
}; class_name :: class_name(variables) //Parameterized constructor definition { // initialization of variables }

More Examples for Parameterized Constructor

Example 1

#include <iostream>
using namespace std;

class myClass {
    private:
        int a, b;
    public:
    // parameterized constructor
    myClass (int a1, int b1){
        a = a1;
        b = b1;
    }

    int getA (){
        return a;
    }

    int getB (){
        return b;
    }
};
int main ()
{
    myClass obj1(10, 15);
    
    cout << "obj1.a = " << obj1.getA() << endl;
    cout << "obj1.b = " << obj1.getB() << endl;
    
    return 0;
}

Output

obj1.a = 10
obj1.b = 15

Example 2

#include<iostream>
using namespace std;

class Student {
    public:
    int roll_no;
    string st_name;
    float weight;

    Student (int num, string name, float w){
        roll_no = num;
        st_name = name;
        weight = w;
    }

    void display (){
        cout << roll_no << " " << st_name << " " << weight << endl;
    }
};

int main(void) {
    Student s1 = Student(1, "Karen", 91.1);
    Student s2 = Student(2, "Phils", 67);
    
    s1.display();
    s2.display();
    
    return 0;
}

Output

1 Karen 91.1
2 Phils 67

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription