Constructors in C++

Constructors in C++

  • Constructor in C++ is a special member function used to initialize an object when it is created.
  • Constructors have the same name as the class and do not have a return type.
  • Constructors are automatically invoked when an object is created.
  • In C++, a class can have multiple constructors, each with a different set of parameters.

Constructors in C++

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

C++ Default Constructor

  • A default constructor is a special constructor in C++ that doesn't take any arguments.
  • Default constructors are automatically called when you create an object of a class.
Loading…
  • The Person class has a default constructor, which initializes the name to "Unknown" and age to 0.
  • When we create an object person without providing any arguments, the default constructor is automatically called,

C++ Parameterized Constructor

  • A parameterized constructor is a special constructor that takes one or more arguments.
  • Its purpose is to initialize an object with specific values for its attributes when it is created.
Loading…
  • The Person class has a parameterized constructor that takes two arguments: n (for the name) and a (for the age).
  • When we create an object person and provide values "Alice" and 25 as arguments, the parameterized constructor is called, and the object's attributes are initialized with these specific values.

C++ Copy Constructor

  • A copy constructor is a special constructor that initializes an object using another object of the same class.
  • It is used when objects are passed by value or when new objects are created as copies of existing ones.
Loading…
  • We have a copy constructor in the Person class that creates a copy of another Person object.
  • When copyOfAlice is initialized with Alice, the copy constructor is called, creating a new object with the same attributes.

What is Constructor Overloading?

Constructor overloading in C++ allows you to define multiple constructors within a class, each with a different set of parameters. for example:
Loading…
  • When you create person1, it uses the default constructor with no arguments.
  • When you create person2, it uses the parameterized constructor with two arguments to set both name and age.
  • When you create person3, it uses the parameterized constructor with one argument to set only the name while age defaults to 0.

What are the characteristics of a constructor?

  • Name matches class.
  • No return type.
  • Automatically invoked.
  • Initializes object.
  • Can have parameters.
  • Multiplicity with overloading.
  • Access specifier (public).
  • Default constructor provided if none.

What distinguishes constructors from a typical member function?

Name and Purpose

  • Constructors have the same name as the class and are used to initialize objects of that class when they are created.
  • Typical Member Functions have names that describe their purpose, and they perform various operations on objects after they have been initialized by a constructor.

Automatic Invocation

  • Constructors are automatically invoked or called when an object is created.
  • Typical Member Functions need to be called explicitly,

Return Type

  • Constructors do not have a return type specified, not even void. They initialize the object's state but do not return any value.
  • Typical Member Functions can have a return type, and they often return values.

What is Destructor?

  • Destructor is a special member function used to clean up resources when an object is destroyed.
  • It has the same name as the class preceded by a tilde (~) and does not take any parameters.
Loading…
  • We have a class Person with a constructor and a destructor.
  • The constructor initializes the name attribute.
  • The destructor is automatically called when the object person goes out of scope and prints a message.