Skip to content

Commit 6bf181f

Browse files
committed
Added more concepts
1 parent f716936 commit 6bf181f

File tree

23 files changed

+584
-21
lines changed

23 files changed

+584
-21
lines changed

Argument_passing/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222

2323

2424
---
25-
[Next](https://github.com/Lavin-tom/cpp_programming/tree/master/)
25+
[Next](https://github.com/Lavin-tom/cpp_programming/tree/master/Namespace)

Datatypes/Readme.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
## Datatypes
2+
3+
4+
### Bool
5+
`bool` data type is used to represent Boolean values, which can be either `true` or `false`. It is typically used in conditional statements, logical operations, and boolean expressions.
6+
7+
In memory, a bool value is usually stored as 1 byte, where a value of 1 represents true and a value of 0 represents false.
8+
9+
example:
10+
```c++
11+
bool isTrue = true; // declaring and initializing a bool variable
12+
13+
if (isTrue) {
14+
// code to be executed if isTrue is true
15+
cout << "The value is true." << endl;
16+
} else {
17+
// code to be executed if isTrue is false
18+
cout << "The value is false." << endl;
19+
}
20+
21+
```
22+
To print true or false we can use `boolalpha`
23+
24+
example:
25+
```c++
26+
#include<iostream>
27+
using namespace std;
28+
int main()
29+
{
30+
bool a = 1;
31+
cout << boolalpha << a << endl;
32+
}
33+
34+
output:
35+
true
36+
```
37+
#### Why size of `bool` is one byte?
38+
The size of a bool is typically one byte, which is equivalent to 8 bits. This size is chosen to ensure that a bool variable can hold either true or false values.
39+
40+
Although a bool logically represents a single bit of information (1 or 0), using a whole byte provides several advantages:
41+
42+
- Memory Alignment: Memory is usually organized in byte-sized chunks, and accessing single bytes is generally more efficient than accessing individual bits.
43+
44+
- Compatibility: Many computer architectures and programming languages are designed to work with bytes as the smallest addressable unit of memory. Using a single byte for a bool allows for easier interoperability and compatibility between different systems and programming languages.
45+
46+
- Simplicity: Using a byte for a bool simplifies memory management and makes it consistent with other data types. It avoids complexities that could arise from manipulating individual bits directly.
47+
48+
---
49+
50+
### wchar_t
51+
- `wchar_t` is a built-in data type that represents a wide character. It is used to store Unicode characters that may require more than a single byte to represent.
52+
53+
- The wchar_t type is typically used when working with wide strings or characters that are outside the ASCII range. It ensures that characters from various character sets, including non-Latin scripts, can be represented accurately.
54+
55+
- The size of wchar_t is implementation-defined and can vary depending on the platform and compiler. It is guaranteed to be large enough to hold the largest supported wide character set for the target environment.CopyCopy
56+
57+
- In most modern systems, wchar_t is 2 or 4 bytes in size, allowing it to accommodate a wide range of Unicode characters. The exact size can be determined using the `sizeof` operator.
58+
59+
60+
61+
### string
62+
`string` data type is used to represent and manipulate text or character sequences. It is part of the C++ Standard Library and provides a more convenient and flexible way to work with strings compared to C-style character arrays.
63+
64+
65+
- Header File Inclusion:
66+
To use string, you need to include the `<string>` header file in your C++ program.
67+
68+
- Dynamic Size:
69+
Unlike fixed-size character arrays, string can dynamically grow or shrink to accommodate the length of the string. You don't need to specify the size beforehand.
70+
71+
- Easy Initialization:
72+
You can initialize an string using various methods:
73+
74+
- Direct initialization: string str = "Hello";
75+
- Copy initialization: string str("Hello");
76+
- Assignment: string str; str = "Hello";
77+
78+
- String Operations:
79+
string provides a wide range of member functions for common string operations, including:
80+
81+
- Accessing individual characters: str[i], str.at(i)
82+
- Concatenation: str1 + str2, str1.append(str2)
83+
- Length: str.length(), str.size()
84+
- Substring extraction: str.substr(startIndex, length)
85+
- Searching for substrings: str.find(substring), str.rfind(substring)
86+
- Modifying strings: str.replace(), str.erase(), str.insert()
87+
- Comparison: str1 == str2, str1 < str2, etc.
88+
- String Input and Output:
89+
string can be easily read from and written to the standard input/output streams using the familiar `>>` and `<<` operators.
90+
91+
92+
---
93+
[Next](https://github.com/Lavin-tom/cpp_programming/tree/master/Function_Overloading)

Datatypes/bool.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
using namespace std;
3+
int main()
4+
{
5+
6+
bool isTrue = true; // declaring and initializing a bool variable
7+
if (isTrue)
8+
{
9+
// code to be executed if isTrue is true
10+
cout << "The value is true." << endl;
11+
}
12+
else
13+
{
14+
// code to be executed if isTrue is false
15+
cout << "The value is false." << endl;
16+
}
17+
}

Datatypes/string.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<iostream>
2+
#include<string>
3+
using namespace std;
4+
int main()
5+
{
6+
string str = "Hello";
7+
cout<<str<<endl;
8+
9+
//Accessing individual character
10+
//Accessing individual characters: str[i], str.at(i)
11+
cout<<str[0]<<endl; //method one
12+
cout<<str.at(0)<<endl; //methond two
13+
14+
//Length of string
15+
//Length: str.length(), str.size()
16+
cout<<"Length of string: "<<str.length()<<endl; //method one
17+
cout<<"Length of string: "<<str.size()<<endl; //method two
18+
19+
//concatenation
20+
//Concatenation: str1 + str2, str1.append(str2)
21+
string str2 = "World";
22+
cout<<str+str2<<endl; //method one
23+
cout<<str.append(str2)<<endl; //method two
24+
25+
//extraction
26+
//Substring extraction: str.substr(startIndex, length)
27+
cout<<str.substr(0,5)<<endl;
28+
29+
//substring
30+
//Searching for substrings: str.find(substring), str.rfind(substring)
31+
cout<<str.find("World")<<endl; //method one
32+
cout<<str.rfind("World")<<endl; //method two
33+
34+
//modifying string
35+
str.replace(5,4,"everyone!"); //replace 4 characters and add "everyone" from 5 th index position
36+
cout<<str<<endl;
37+
38+
str.erase(5,7); //deleting 7 characters from index 5
39+
cout<<str<<endl;
40+
41+
str.insert(5,"everyone!");
42+
cout<<str<<endl;
43+
44+
//comaparison
45+
//Comparison: str1 == str2, str1 < str2, etc.
46+
if(str==str2)
47+
cout<<str<<" and "<<str2<<" are same"<<endl;
48+
else
49+
cout<<str<<" and "<<str2<<" are not same"<<endl;
50+
51+
52+
string str3;
53+
cin>>str3;
54+
cout<<str3<<endl;
55+
string str4;
56+
getline(cin,str4); //to print string with space
57+
cout<<str4<<endl;
58+
}

Datatypes/wchar_t.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
using namespace std;
3+
int main() {
4+
wchar_t myWideChar = L'';
5+
wcout << "Wide character: " << myWideChar << endl;
6+
wcout << "Size of wchar_t: " << sizeof(wchar_t) << " bytes" << endl;
7+
return 0;
8+
}

Function_overloading/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Function Overloading
2+
3+
Function overloading is a feature in C++ that allows `multiple functions with the same name, but different parameter lists`, to be defined. This means that you can have multiple functions in a program with the same name, as long as they have different types or number of parameters.
4+
5+
example:
6+
7+
```C++
8+
#include <iostream>
9+
using namespace std;
10+
void print(int num) {
11+
cout << "Printing an integer: " << num << endl;
12+
}
13+
14+
void print(double num) {
15+
cout << "Printing a double: " << num << endl;
16+
}
17+
18+
void print(char c) {
19+
cout << "Printing a character: " << c << endl;
20+
}
21+
22+
int main() {
23+
print(5); // Calls the function print(int)
24+
print(3.14); // Calls the function print(double)
25+
print('a'); // Calls the function print(char)
26+
27+
return 0;
28+
}
29+
30+
31+
```
32+
---
33+
34+
[Next](https://github.com/Lavin-tom/cpp_programming/tree/master/Inline_functions)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//swapping using function over loading
2+
#include<iostream>
3+
using namespace std;
4+
void swap_data(int a,int b)
5+
{
6+
int t;
7+
cout<<"integer swaping"<<endl;
8+
t=a;
9+
a=b;
10+
b=t;
11+
cout<<"a: "<<a<<" b: "<<b<<endl;
12+
}
13+
void swap_data(char ch1,char ch2)
14+
{
15+
char t;
16+
cout<<"char swaping"<<endl;
17+
t=ch1;
18+
ch1=ch2;
19+
ch2=t;
20+
cout<<"ch1: "<<ch1<<" ch2: "<<ch2<<endl;
21+
}
22+
void swap_data(float f1,float f2)
23+
{
24+
float t;
25+
cout<<"float swaping"<<endl;
26+
t=f1;
27+
f1=f2;
28+
f2=t;
29+
cout<<"f1: "<<f1<<" f2: "<<f2<<endl;
30+
}
31+
int main()
32+
{
33+
int a=10,b=20;
34+
char ch1='A',ch2='B';
35+
float f1=12.3,f2=34.5;
36+
cout<<"before swpping"<<endl;
37+
cout<<"a: "<<a<<" b: "<<b<<endl;
38+
cout<<"ch1: "<<ch1<< "ch2: "<<ch2<<endl;
39+
cout<<"f1: "<<f1<< " f2: "<<f2<<endl;
40+
cout<<"after swaping"<<endl;
41+
swap_data(a,b);
42+
swap_data(ch1,ch2);
43+
swap_data(f1,f2);
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//function overloading
2+
//same function name different parameter list
3+
#include<iostream>
4+
using namespace std;
5+
void print(int a)
6+
{
7+
cout<<a<<endl;
8+
}
9+
void print(double a)
10+
{
11+
cout<<a<<endl;
12+
}
13+
void print(char a)
14+
{
15+
cout<<a<<endl;
16+
}
17+
void print(string a)
18+
{
19+
cout<<a<<endl;
20+
}
21+
int main()
22+
{
23+
print(10);
24+
print(12.34);
25+
print('?');
26+
print("testing");
27+
}

Inline_functions/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Inline function
2+
3+
An inline function is a function that is expanded by the compiler at the location where it is called, rather than being executed as a separate function call.
4+
5+
To define an inline function in C++, the keyword `inline` is placed before the function declaration. For example:
6+
7+
```C++
8+
inline int multiply(int x, int y) {
9+
return x * y;
10+
}
11+
```
12+
13+
When an inline function is called, `the compiler replaces the function call with the actual code of the function`. This eliminates the overhead of making a function call, resulting in faster execution.
14+
15+
#### Notes :
16+
- inline keyword is just a suggestion to the compiler, and the compiler is free to decide whether or not to actually inline the function.
17+
- inline functions should be kept small, as large functions may not be suitable for inlining.
18+
19+
---
20+
21+
[Next](https://github.com/Lavin-tom/cpp_programming/tree/master/Dynamic_memory_allocation)

Introduction-to-C++/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
|Not have Exception Handling|Having exception handling|
5151
|Template is not available|Template is available|
5252

53-
```
53+
```C++
5454
#include<iostream>
5555
using namespace std;
5656
int main()

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ The repository is organized into multiple directories, each focusing on a specif
1010

1111
If you are new to C++ or programming in general, we recommend starting with the [Introduction to C++](https://github.com/Lavin-tom/cpp_programming/tree/master/Introduction-to-C++) directory. Here, you will find introductory topics, basic syntax, and fundamental programming concepts that will lay the groundwork for your C++ journey.
1212

13+
## Topic Covered
14+
### Flow of Concepts
15+
- [Introduction to C++](https://github.com/Lavin-tom/cpp_programming/tree/master/Introduction-to-C++)
16+
- [Scope Resolution Operator](https://github.com/Lavin-tom/cpp_programming/tree/master/Scope_resolution_operator)
17+
- [Reference Operator](https://github.com/Lavin-tom/cpp_programming/tree/master/Reference_operator)
18+
- [Argument Passing](https://github.com/Lavin-tom/cpp_programming/tree/master/Argument_passing)
19+
- [Namespace](https://github.com/Lavin-tom/cpp_programming/tree/master/Namespace)
20+
- [Datatypes](https://github.com/Lavin-tom/cpp_programming/tree/master/Datatypes)
21+
- [Function Overloading](https://github.com/Lavin-tom/cpp_programming/tree/master/Function_Overloading)
22+
- [Inline Functions](https://github.com/Lavin-tom/cpp_programming/tree/master/Inline_functions)
23+
- [Dynamic Memory Allocation](https://github.com/Lavin-tom/cpp_programming/tree/master/Dynamic_memory_allocation)
24+
- [Structure](https://github.com/Lavin-tom/cpp_programming/tree/master/Structure)
25+
1326
## Start Learning C++ Today!
1427

1528
We believe that learning programming should be an enjoyable and enriching experience. With this C++ Beginner's Repository, we hope to provide you with the necessary resources to kickstart your journey into the world of C++ programming. Happy coding!

Reference_operator/Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Used to provide alias name/duplicate name for existing varibale.
44

55
Syntax : `datatype &new_name = existing_name;`
6-
```
6+
```C++
77
example:
88

99
#include<iostream>
@@ -24,7 +24,7 @@ x: 20
2424
#### Reference to Array
2525
Syntax : `datatype (&new_array)[size] = existing_array; `
2626

27-
```
27+
```C++
2828
example:
2929

3030
#include<iostream>
@@ -45,7 +45,7 @@ output:
4545
#### Reference to Pointer
4646
Syntax : `datatype* (&new_variable) = existing_variable; `
4747

48-
```
48+
```C++
4949
example:
5050

5151
#include<iostream>

Scope_resolution_operator/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
To access global variable when there is local varibale with same.
44

5-
```
5+
```C++
66
example:
77

88
#include<iostream>

0 commit comments

Comments
 (0)