Constructor:

- A constructor is one kind of feature in oops.
- Constructor means, class name the same as the function or method name.
- It is similar to the function so it’s called special member function and that class name the same as the constructor name.
- It used to construct and initialize all the data members.
- Constructor automatically called, when the object is created.
- It can be defined inside the class and definitions are outside the class definition help of the class name.
- A constructor should be declared in the public section.
- The name should be equal to the class name followed by the parenthesis.
- It does not have any return type, not even void.
- They cannot be inherited through a derived class can call the base class constructor.
- It cannot be a virtual function.
Types of
constructor:
- Three different types of constructors:
- Default constructor
- Parameterized constructor
- Copy constructor
- A constructor with no parameters is known as a default constructor.
Syntax:
Example:
Parameterized
constructor:
- A constructor with parameters is known as a parameterized constructor.
Syntax:
Example:
Copy
constructor:
- It is used when one object of class initializes another object.
- It takes a reference to an object of the same class as an argument.
Syntax:
Example:
No comments:
Post a Comment