Skip to content

Commit

Permalink
Added topic constructors to day 12 (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
pankhuri92 authored Jun 12, 2024
1 parent be99020 commit 0b42f8b
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/day-12/_category_.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"type": "generated-index"
}
}

27 changes: 27 additions & 0 deletions docs/day-12/destructors.md
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.
25 changes: 25 additions & 0 deletions docs/day-12/introduction.md
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
}
};
```
26 changes: 26 additions & 0 deletions docs/day-12/member-init-list.md
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)
73 changes: 73 additions & 0 deletions docs/day-12/types-of-constructors.md
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
}
};
```


Binary file added static/img/day-12/destructors.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/day-12/introduction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/day-12/member-init-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/day-12/types-of-constructors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0b42f8b

Please sign in to comment.