-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Shubhadip Bhowmik <[email protected]>
- Loading branch information
1 parent
0b56ce3
commit 00228d5
Showing
8 changed files
with
525 additions
and
5 deletions.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
sidebar_position: 6 | ||
title: "Logical Operators" | ||
description: "In this tutorial, we will learn about Logical Operators in C++ programming with the help of examples." | ||
sidebar_label: "Logical Operators" | ||
slug: Logical-Operators-cpp | ||
--- | ||
# Logical Operators in C++ | ||
|
||
This repository provides an explanation of logical operators in C++, along with examples of their usage. | ||
|
||
## Table of Contents | ||
|
||
- [Introduction](#introduction) | ||
- [Logical Operators](#logical-operators) | ||
- [AND (`&&`)](#and-) | ||
- [OR (`||`)](#or-) | ||
- [NOT (`!`)](#not-) | ||
- [Examples](#examples) | ||
|
||
## Introduction | ||
|
||
Logical operators in C++ are used to perform logical operations on boolean operands. This document provides an overview of these operators and demonstrates their usage through examples. | ||
|
||
## Logical Operators | ||
|
||
### AND (`&&`) | ||
|
||
The AND operator returns `true` if both operands are `true`, otherwise it returns `false`. | ||
|
||
### OR (`||`) | ||
|
||
The OR operator returns `true` if at least one of the operands is `true`, otherwise it returns `false`. | ||
|
||
### NOT (`!`) | ||
|
||
The NOT operator negates the value of its operand. If the operand is `true`, it returns `false`, and if the operand is `false`, it returns `true`. | ||
|
||
## Examples | ||
|
||
```cpp | ||
bool a = true; | ||
bool b = false; | ||
|
||
// Using AND operator | ||
bool result = (a && b); // result will be false | ||
|
||
// Using OR operator | ||
result = (a || b); // result will be true | ||
|
||
// Using NOT operator | ||
result = !a; // result will be false | ||
``` |
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,67 @@ | ||
--- | ||
sidebar_position: 4 | ||
title: "Arithmetic Operators" | ||
description: "In this tutorial, we will learn about Arithmetic Operators in C++ programming with the help of examples." | ||
sidebar_label: "Arithmetic Operators" | ||
slug: Arithmetic-Operators-cpp | ||
--- | ||
|
||
# Arithmetic Operators in C++ | ||
|
||
Arithmetic operators in C++ are symbols used to perform mathematical operations on operands. These operators allow you to perform addition, subtraction, multiplication, division, and modulus operations. | ||
|
||
## Addition (+) | ||
|
||
Adds two operands together. | ||
|
||
```cpp | ||
|
||
int result = 5 + 3; // result will be 8 | ||
|
||
``` | ||
## Subtraction (-) | ||
|
||
Subtracts the right operand from the left operand. | ||
|
||
```cpp | ||
|
||
int result = 7 - 4; // result will be 3 | ||
|
||
``` | ||
## Multiplication (*) | ||
|
||
Multiplies two operands. | ||
|
||
```cpp | ||
|
||
int result = 6 * 2; // result will be 12 | ||
|
||
``` | ||
|
||
## Division (/) | ||
|
||
Divides the left operand by the right operand. If both operands are integers, the result will be an integer, with any remainder discarded. | ||
|
||
```cpp | ||
|
||
int result = 10 / 3; // result will be 3 | ||
|
||
``` | ||
If you want to get a floating-point result, you can use floating-point operands: | ||
|
||
```cpp | ||
|
||
double result = 10.0 / 3.0; // result will be approximately 3.33333 | ||
|
||
``` | ||
|
||
## Modulus (%) | ||
|
||
Returns the remainder of the division of the left operand by the right operand. | ||
|
||
```cpp | ||
|
||
int result = 10 % 3; // result will be 1 | ||
|
||
``` | ||
These operators can be used with variables, constants, or expressions. They follow the usual rules of precedence and associativity. Additionally, parentheses can be used to enforce a specific order of evaluation. |
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,75 @@ | ||
--- | ||
sidebar_position: 5 | ||
title: "Relational Operators" | ||
description: "In this tutorial, we will learn about Relational Operators in C++ programming with the help of examples." | ||
sidebar_label: "Relational Operators" | ||
slug: Relational-Operators-cpp | ||
--- | ||
|
||
# Relational Operators in C++ | ||
|
||
Relational operators in C++ are used to compare two values and determine the relationship between them. These operators return a boolean value of true or false based on whether the comparison is true or false. Here are the commonly used relational operators: | ||
|
||
## Equal to (==) | ||
|
||
Compares if two operands are equal. Returns true if they are equal, false otherwise. | ||
|
||
```cpp | ||
int a = 5, b = 5; | ||
if (a == b) { | ||
// This block will be executed because a is equal to b | ||
} | ||
``` | ||
|
||
## Not equal to (!=) | ||
|
||
Compares if two operands are not equal. Returns true if they are not equal, false otherwise. | ||
|
||
```cpp | ||
int a = 5, b = 10; | ||
if (a != b) { | ||
// This block will be executed because a is not equal to b | ||
} | ||
``` | ||
## Greater than (>) | ||
|
||
Checks if the left operand is greater than the right operand. Returns true if it is, false otherwise. | ||
|
||
```cpp | ||
int a = 10, b = 5; | ||
if (a > b) { | ||
// This block will be executed because a is greater than b | ||
} | ||
``` | ||
|
||
## Less than ( < ) | ||
Checks if the left operand is less than the right operand. Returns true if it is, false otherwise. | ||
|
||
```cpp | ||
int a = 5, b = 10; | ||
if (a < b) { | ||
// This block will be executed because a is less than b | ||
} | ||
|
||
``` | ||
## Greater than or equal to (>=) | ||
|
||
Checks if the left operand is greater than or equal to the right operand. Returns true if it is, false otherwise. | ||
|
||
```cpp | ||
int a = 10, b = 5; | ||
if (a >= b) { | ||
// This block will be executed because a is greater than or equal to b | ||
} | ||
``` | ||
## Less than or equal to (< =) | ||
|
||
Checks if the left operand is less than or equal to the right operand. Returns true if it is, false otherwise. | ||
|
||
```cpp | ||
int a = 5, b = 10; | ||
if (a <= b) { | ||
// This block will be executed because a is less than or equal to b | ||
} | ||
|
||
``` |
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,79 @@ | ||
--- | ||
sidebar_position: 4 | ||
title: "If Statements in C++" | ||
description: "In this tutorial, we will learn about the If Statements in C++ programming with the help of examples. The If statement is used to conditional execution." | ||
sidebar_label: "If Statements in C++" | ||
slug: If-Statements | ||
--- | ||
# If Statements in C++ | ||
|
||
This repository provides an explanation of if statements in C++, along with examples of their usage. | ||
|
||
## Table of Contents | ||
|
||
- [Introduction](#introduction) | ||
- [Basic If Statement](#basic-if-statement) | ||
- [If-Else Statement](#if-else-statement) | ||
- [Nested If Statements](#nested-if-statements) | ||
- [If-Else If-Else Ladder](#if-else-if-else-ladder) | ||
## Introduction | ||
|
||
If statements in C++ are used for conditional execution, allowing you to control the flow of your program based on certain conditions. | ||
|
||
## Basic If Statement | ||
|
||
```cpp | ||
int x = 10; | ||
|
||
if (x > 5) { | ||
std::cout << "x is greater than 5" << std::endl; | ||
} | ||
``` | ||
In this example, the condition `x > 5` is evaluated. If it's true (which it is, since `x` is `10`), the code inside the curly braces `{}` is executed, and "x is greater than 5" is printed.` | ||
|
||
## If-Else Statement | ||
|
||
```cpp | ||
int y = 2; | ||
|
||
if (y % 2 == 0) { | ||
std::cout << "y is even" << std::endl; | ||
} else { | ||
std::cout << "y is odd" << std::endl; | ||
} | ||
|
||
``` | ||
In this example, if `y` is divisible by `2` (i.e., it's an even number), the first block of code is executed. Otherwise, if the condition is false, the code inside the `else` block is executed.` | ||
|
||
## Nested If Statements | ||
|
||
```cpp | ||
int a = 5; | ||
int b = 10; | ||
|
||
if (a > 0) { | ||
if (b > 5) { | ||
std::cout << "Both a and b are positive and b is greater than 5" << std::endl; | ||
} | ||
} | ||
|
||
``` | ||
Here, we have an if statement nested inside another if statement. Both conditions must be true for the inner block of code to execute. | ||
|
||
## If-Else If-Else Ladder | ||
|
||
```cpp | ||
int mark = 70; | ||
|
||
if (mark >= 90) { | ||
std::cout << "Grade A" << std::endl; | ||
} else if (mark >= 80) { | ||
std::cout << "Grade B" << std::endl; | ||
} else if (mark >= 70) { | ||
std::cout << "Grade C" << std::endl; | ||
} else { | ||
std::cout << "Grade D" << std::endl; | ||
} | ||
|
||
``` | ||
In this example, the program checks the value of `mark` against multiple conditions in a sequential manner. Once a condition is met, the corresponding block of code is executed, and the rest of the conditions are skipped. |
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,51 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
# Access Specifiers in C++ | ||
|
||
Access specifiers in C++ are keywords that control the visibility and accessibility of class members. | ||
|
||
## Types of Access Specifiers | ||
|
||
### 1. Public | ||
- **Description**: Public members are accessible from outside the class. | ||
- **Usage**: | ||
```cpp | ||
class MyClass { | ||
public: | ||
int myPublicMember; | ||
void myPublicFunction() { | ||
// Function body | ||
} | ||
}; | ||
``` | ||
### 2. Private | ||
- **Description**: Private members are accessible only within the class. | ||
- **Usage**: | ||
```cpp | ||
class MyClass { | ||
private: | ||
int myPrivateMember; | ||
void myPrivateFunction() { | ||
// Function body | ||
} | ||
}; | ||
``` | ||
### 3. Protected | ||
- **Description**: Protected members are accessible within the class and its subclasses (derived classes). | ||
- **Usage**: | ||
```cpp | ||
class MyBaseClass { | ||
protected: | ||
int myProtectedMember; | ||
}; | ||
class MyDerivedClass : public MyBaseClass { | ||
public: | ||
void myFunction() { | ||
myProtectedMember = 10; // Accessing protected member of the base class | ||
} | ||
}; | ||
``` | ||
Access specifiers provide control over data hiding and encapsulation, enabling better design and implementation of classes in C++. |
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,52 @@ | ||
--- | ||
sidebar_position: 2 | ||
--- | ||
|
||
# Classes and Objects in C++ | ||
|
||
## Classes | ||
|
||
In C++, a class is a user-defined data type that represents a blueprint for creating objects. It encapsulates data for the object and functions that operate on that data. | ||
|
||
### Class Definition | ||
```cpp | ||
class MyClass { | ||
private: | ||
int myNumber; | ||
string myString; | ||
public: | ||
void setValues(int num, string str) { | ||
myNumber = num; | ||
myString = str; | ||
} | ||
void display() { | ||
cout << "Number: " << myNumber << endl; | ||
cout << "String: " << myString << endl; | ||
} | ||
}; | ||
``` | ||
- **Access Specifiers**: `private`, `public`, and `protected` control the access level of class members. | ||
- **Private members**: Can only be accessed within the class. | ||
- **Public members**: Can be accessed from outside the class. | ||
## Objects | ||
An object is an instance of a class. It represents a real-world entity and can store data and perform actions using member functions. | ||
### Object Creation | ||
```cpp | ||
MyClass obj1; // Creating an object of MyClass | ||
MyClass obj2; // Another object of MyClass | ||
``` | ||
|
||
### Usage | ||
|
||
```cpp | ||
obj1.setValues(10, "Hello"); | ||
obj2.setValues(20, "World"); | ||
|
||
obj1.display(); // Output: Number: 10 String: Hello | ||
obj2.display(); // Output: Number: 20 String: World | ||
``` | ||
|
||
In this example, `obj1` and `obj2` are instances of the `MyClass` class. The `setValues` function sets the values of `myNumber` and `myString` for each object, and the `display` function prints these values. |
Oops, something went wrong.