C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
Constructor in C++ Tutorial
A constructor is a special member function of a class that is automatically called when an object of the class is created. The constructor's main purpose is to initialize the object's member variables and set up the required resources. This tutorial will cover the basics of constructors in C++.
Defining a Constructor
A constructor has the same name as the class and has no return type. It can take parameters to initialize the object's member variables.
Example:
class MyClass { int x; public: MyClass(int a) { // Constructor with one parameter x = a; } };
In this example, we define a constructor for the MyClass
class that takes one parameter a
and initializes the x
member variable with its value.
Default Constructor
A default constructor is a constructor that takes no parameters. If you don't define any constructor for a class, the compiler automatically generates a default constructor for you. However, if you define a constructor with parameters, the compiler no longer generates a default constructor. In this case, you should define one yourself if you want to create objects without providing initial values for the member variables.
Example:
class MyClass { int x; public: MyClass() { // Default constructor x = 0; } MyClass(int a) { // Constructor with one parameter x = a; } };
In this example, we define both a default constructor and a constructor with one parameter for the MyClass
class.
Constructor Overloading
Like other functions in C++, constructors can be overloaded. You can define multiple constructors with different numbers of parameters or different types of parameters.
Example:
class MyClass { int x, y; public: MyClass() { // Default constructor x = 0; y = 0; } MyClass(int a) { // Constructor with one parameter x = a; y = 0; } MyClass(int a, int b) { // Constructor with two parameters x = a; y = b; } };
In this example, we define three constructors for the MyClass
class with different numbers of parameters.
Member Initializer Lists
A more efficient and concise way to initialize member variables in the constructor is to use member initializer lists. Instead of assigning values inside the constructor body, you can initialize the member variables directly in the constructor's declaration.
Example:
class MyClass { int x, y; public: MyClass() : x(0), y(0) {} // Default constructor with member initializer list MyClass(int a) : x(a), y(0) {} // Constructor with one parameter and member initializer list MyClass(int a, int b) : x(a), y(b) {} // Constructor with two parameters and member initializer list };
In this example, we use member initializer lists to initialize the member variables in the constructors for the MyClass
class.
Default constructor in C++:
class MyClass { public: // Default constructor MyClass() { // Initialization code if needed } };
Parameterized constructor in C++:
class MyClass { public: // Parameterized constructor MyClass(int value) { // Initialization using the parameter } };
Constructor overloading in C++:
class MyClass { public: // Default constructor MyClass() {} // Parameterized constructor MyClass(int value) {} };
Initialization lists in C++ constructors:
class MyClass { private: int data; public: // Constructor with initialization list MyClass(int value) : data(value) { // Constructor body } };
Constructor chaining in C++:
class MyClass { private: int data; public: // Parameterized constructor MyClass(int value) : data(value) {} // Constructor chaining using another constructor MyClass() : MyClass(0) {} };
Copy constructor in C++:
class MyClass { public: int data; // Copy constructor MyClass(const MyClass& other) : data(other.data) {} };
Constructor with default arguments in C++:
class MyClass { public: int data; // Constructor with default argument MyClass(int value = 42) : data(value) {} };
Static constructor in C++:
class MyClass { public: static int staticData; // Static constructor static MyClass initialize() { staticData = 10; return MyClass(); } };
Inheritance and constructors in C++:
class Base { public: Base(int value) { // Base class constructor } }; class Derived : public Base { public: // Derived class constructor calling base class constructor Derived(int value) : Base(value) { // Derived class constructor body } };