Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
175 views
in C++ by (178k points)
What are the types of constructors in C++?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

There are three types of constructors in C++: default constructor, parameterized constructor, and copy constructor.

  • Default Constructor: It is a constructor that takes no arguments. If a class does not have any constructors defined, the compiler generates a default constructor automatically.

Example code:

class MyClass {
public:
  // Default constructor
  MyClass() {
    // Initialization code
    // ...
  }
};
 
  • Parameterized Constructor: It is a constructor that takes one or more parameters. It allows you to initialize the object with specific values.

Example code:

class MyClass {
public:
  // Parameterized constructor
  MyClass(int value) {
    // Initialization code
    // ...
  }
};

int main() {
  // Creating an object of MyClass with a parameter
  MyClass obj(10);
  // ...
  return 0;
}
 
  • Copy Constructor: It is a constructor that creates a new object by copying the values from an existing object of the same class.

Example code:

class MyClass {
public:
  // Copy constructor
  MyClass(const MyClass& other) {
    // Copy the values from 'other' to the new object
    // ...
  }
};

int main() {
  // Creating an object of MyClass and initializing it with another object
  MyClass obj1;
  MyClass obj2(obj1);  // Copy constructor called
  // ...
  return 0;
}

Related questions

0 votes
1 answer
0 votes
2 answers
asked Jun 7, 2023 in C++ by kvdevika (178k points)
0 votes
1 answer
asked Jun 7, 2023 in C++ by kvdevika (178k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...