-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added topic constructors to day 12 (#153)
- Loading branch information
1 parent
be99020
commit 0b42f8b
Showing
9 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
"type": "generated-index" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
sidebar_position: 4 | ||
title: "Destructors" | ||
description: "They're used to clean up when an object goes out of scope or is deleted. " | ||
sidebar_label: "Destructors" | ||
slug: destructors | ||
--- | ||
|
||
### Definition | ||
|
||
Destructor is an instance member function that is invoked automatically whenever an object is going to be destroyed. Meaning, a destructor is the last function that is going to be called before an object is destroyed. | ||
|
||
![destructors](../../static/img/day-12/destructors.jpg) | ||
|
||
### Syntax | ||
The destructor has the same name as the class preceded by a tilde (~) and does not take any arguments. | ||
|
||
```cpp | ||
class Example { | ||
public: | ||
~Example() { | ||
std::cout << "Destructor Called" << std::endl; | ||
} | ||
}; | ||
|
||
``` | ||
Destructors are essential for releasing resources, such as memory or file handles, that the object may have acquired during its lifetime. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
sidebar_position: 1 | ||
title: "Introduction to Constructors" | ||
description: "A constructor is a special member function that is executed whenever an object of the class is created. " | ||
sidebar_label: "Introduction" | ||
slug: introduction | ||
--- | ||
|
||
### Definition | ||
|
||
A constructor is a special member function that is executed whenever an object of the class is created. Constructors are used to initialize objects. | ||
|
||
![introduction](../../static/img/day-12/introduction.png) | ||
|
||
### Syntax | ||
The constructor has the same name as the class and does not have a return type. | ||
|
||
```cpp | ||
class ClassName { | ||
public: | ||
ClassName() { | ||
// Constructor body | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
sidebar_position: 3 | ||
title: "Member Initializer List" | ||
description: "Constructors can also initialize class members using an initializer list, which can be more efficient." | ||
sidebar_label: "Member Initializer List" | ||
slug: member-init-list | ||
--- | ||
|
||
### What does it mean? | ||
|
||
Constructors can also initialize class members using an initializer list, which can be more efficient. | ||
|
||
```cpp | ||
class Example { | ||
public: | ||
int value; | ||
|
||
// Using Initializer List | ||
Example(int v) : value(v) { | ||
std::cout << "Constructor with Initializer List Called" << std::endl; | ||
} | ||
}; | ||
``` | ||
Using an initializer list is often more efficient than assigning values in the constructor body, especially for complex data members or base class initialization. | ||
|
||
![member-init-list](../../static/img/day-12/member-init-list.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
sidebar_position: 2 | ||
title: "Types of Constructors" | ||
description: "Constructors can be classified based on in which situations they are being used." | ||
sidebar_label: "Types of Constructors" | ||
slug: types-of-constructors | ||
--- | ||
|
||
Constructors can be classified based on in which situations they are being used. Each type of constructor serves a different purpose, from providing default values to copying the state from another object. | ||
|
||
![types-of-constructors](../../static/img/day-12/types-of-constructors.png) | ||
|
||
## 1. Default Constructor | ||
A constructor that takes no arguments. | ||
|
||
```cpp | ||
class Example { | ||
public: | ||
Example() { | ||
std::cout << "Default Constructor Called" << std::endl; | ||
} | ||
}; | ||
``` | ||
## 2. Parameterized Constructor | ||
A constructor that takes arguments to initialize an object with specific values. | ||
```cpp | ||
class Example { | ||
public: | ||
int value; | ||
Example(int v) { | ||
value = v; | ||
std::cout << "Parameterized Constructor Called with value: " << value << std::endl; | ||
} | ||
}; | ||
``` | ||
|
||
## 3. Copy Constructor | ||
A constructor that initializes an object using another object of the same class. | ||
|
||
```cpp | ||
class Example { | ||
public: | ||
int value; | ||
|
||
Example(int v) { | ||
value = v; | ||
} | ||
|
||
Example(const Example &obj) { | ||
value = obj.value; | ||
std::cout << "Copy Constructor Called" << std::endl; | ||
} | ||
}; | ||
``` | ||
## 4. Move Constructor | ||
Move constructors are special member functions used to transfer resources from one object to another. Unlike copy constructors, which create a new copy of an object, move constructors "move" the resources, leaving the original object in a valid but unspecified state. | ||
Move constructors are particularly useful when dealing with resource-intensive objects like those managing dynamic memory, file handles, or network connections. | ||
```cpp | ||
class ClassName { | ||
public: | ||
ClassName(ClassName&& other) { | ||
// Move constructor body | ||
} | ||
}; | ||
``` | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.