Constructor is a special type of method that is used to initialize the object.A constructor does not have any return type.
A constructor has a same name as the class in which it declare or use.Constructor can not be abstracted,static,final or synchronized.
#include class Student { public: Student() { cout<<"Non Parameterized Constructor" << endl; } }; void main() { Student s; //creating an object }
output:
Non Parameterized Constructor
#include class Student { public: int id; string name; float percentage; Student(int i, string n, float s) { id = i; name = n; percentage = s; } void display() { cout<< id<<" "<< name<<" "<< salary<< endl; } }; void main() { Student s1 = Student(1, "Rahul", 79.4); //creating an object of Employee Student s2 = Student(2, "Mohit", 87.67); s1.display(); s2.display(); }
output:
1 Rahul 79.4
2 Mohit 87.67