C++ Parameterized Constructor

constructor which has a parameter is called parameterized constructor.

#include <iostream>
using namespace std;
class Employee {
   public:
       int id;//data member (also instance variable)    
       string name;//data member(also instance variable)
       float salary;
       Employee(int i, string n, float s)  
        {  
            id = i;  
            name = n;  
            salary = s;
        }  
       void display()  
        {  
            cout<<id<<"  "<<name<<"  "<<salary<<endl;  
        }  
};
int main(void) {
    Employee e1 =Employee(100, "abhishek", 10000); //creating an object of Employee 
    Employee e2=Employee(101, "Om", 9000); 
    e1.display();  
    e2.display();  
    return 0;
}
Output:
100  abhishek  10000
101  Om  9000
Translate »