Constructors
 Constructor is a special method which is invoked
automatically at the time of object creation. It is used to
initialize the data members of new objects generally.
 Constructors have the same name as class or structure.
 Constructors don’t have a return type. (Not even void)
 Constructors are only called once, at object creation.
Types of constructor
 There can be three types of constructors .
 1. Non-Parameterized constructor :A constructor which
has no argument is known as non-parameterized
constructor(or no-argument constructor). It is invoked at
the time of creating an object.
Code example
 #include <iostream>
 using namespace std;
 class Student {
 private:
 string name;
 int age;
 string course;
 public:
 // Non-parameterized constructor
 Student() {
 name = “Ali";
 age = 0;
 course = “2nd";
 }
 // Function to display student details
 void displayDetails() {
 cout << "Name: " << name << endl;
 cout << "Age: " << age << endl;
 cout << "Course: " << course << endl;
 }
 };
 int main() {
 // Create an object of Student using non-parameterized constructor
 Student student1;
 // Display default student details
 student1.displayDetails();
 return 0;
 }
 2. Parameterized constructor : Constructor which has
parameters is called a parameterized constructor. It is
used to provide
 different values to distinct objects.
 #include <iostream>
 using namespace std;
 class Student {
 private:
 string name;
 int age;
 string course;
 public:
 // Parameterized constructor
 Student(string studentName, int studentAge, string studentCourse) {
 name = studentName;
 age = studentAge;
 course = studentCourse;
 }
 // Function to display student details
 void displayDetails() {
 cout << "Name: " << name << endl;
 cout << "Age: " << age << endl;
 cout << "Course: " << course << endl;
 }
 };
 int main() {
 // Creating an object of Student using the parameterized constructor
 Student student1("Alice", 20, "Computer Science");

 // Display student details
 student1.displayDetails();
 return 0;
 }
 Copy Constructor
 A copy constructor is a member function that initializes
an object using another object of the same class.
 #include <iostream>
 using namespace std;
 class Student {
 private:
 string name;
 int age;
 string course;
 public:
 // Parameterized constructor
 Student(string studentName, int studentAge, string studentCourse) {
 name = studentName;
 age = studentAge;
 course = studentCourse;
 }
 // Copy constructor
 Student(const Student &source) {
 name = source.name;
 age = source.age;
 course = source.course;
 cout << "Copy constructor called!" << endl;
 }
 // Function to display student details
 void displayDetails() {
 cout << "Name: " << name << endl;
 cout << "Age: " << age << endl;
 cout << "Course: " << course << endl;
 }
 };
 int main() {
 // Create an object of Student using parameterized constructor
 Student student1("Bob", 22, "Mathematics");

 // Create a new object using the copy constructor
 Student student2 = student1; // Copy constructor is called here

 // Display details of both students
 cout << "Details of student1:" << endl;
 student1.displayDetails();
 cout << "nDetails of student2 (copy of student1):" << endl;
 student2.displayDetails();
 return 0;
 }

Constructors in c++ and types of constructor.pptx

  • 1.
  • 2.
     Constructor isa special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally.  Constructors have the same name as class or structure.  Constructors don’t have a return type. (Not even void)  Constructors are only called once, at object creation.
  • 3.
    Types of constructor There can be three types of constructors .  1. Non-Parameterized constructor :A constructor which has no argument is known as non-parameterized constructor(or no-argument constructor). It is invoked at the time of creating an object.
  • 4.
    Code example  #include<iostream>  using namespace std;  class Student {  private:  string name;  int age;  string course;  public:  // Non-parameterized constructor  Student() {  name = “Ali";  age = 0;  course = “2nd";  }  // Function to display student details  void displayDetails() {  cout << "Name: " << name << endl;  cout << "Age: " << age << endl;  cout << "Course: " << course << endl;  }  };  int main() {  // Create an object of Student using non-parameterized constructor  Student student1;  // Display default student details  student1.displayDetails();  return 0;  }
  • 5.
     2. Parameterizedconstructor : Constructor which has parameters is called a parameterized constructor. It is used to provide  different values to distinct objects.
  • 6.
     #include <iostream> using namespace std;  class Student {  private:  string name;  int age;  string course;  public:  // Parameterized constructor  Student(string studentName, int studentAge, string studentCourse) {  name = studentName;  age = studentAge;  course = studentCourse;  }  // Function to display student details  void displayDetails() {  cout << "Name: " << name << endl;  cout << "Age: " << age << endl;  cout << "Course: " << course << endl;  }  };  int main() {  // Creating an object of Student using the parameterized constructor  Student student1("Alice", 20, "Computer Science");   // Display student details  student1.displayDetails();  return 0;  }
  • 7.
     Copy Constructor A copy constructor is a member function that initializes an object using another object of the same class.
  • 8.
     #include <iostream> using namespace std;  class Student {  private:  string name;  int age;  string course;  public:  // Parameterized constructor  Student(string studentName, int studentAge, string studentCourse) {  name = studentName;  age = studentAge;  course = studentCourse;  }  // Copy constructor  Student(const Student &source) {  name = source.name;  age = source.age;  course = source.course;  cout << "Copy constructor called!" << endl;  }  // Function to display student details  void displayDetails() {  cout << "Name: " << name << endl;  cout << "Age: " << age << endl;  cout << "Course: " << course << endl;  }  };
  • 9.
     int main(){  // Create an object of Student using parameterized constructor  Student student1("Bob", 22, "Mathematics");   // Create a new object using the copy constructor  Student student2 = student1; // Copy constructor is called here   // Display details of both students  cout << "Details of student1:" << endl;  student1.displayDetails();  cout << "nDetails of student2 (copy of student1):" << endl;  student2.displayDetails();  return 0;  }