-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yuth
committed
Nov 22, 2024
1 parent
683fb7a
commit 3a76231
Showing
64 changed files
with
2,757 additions
and
253 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
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,36 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
|
||
// pointer array address 1 | ||
void create_new_array_from_old() { | ||
int arry[5] = {10, 20, 30, 40, 50}; | ||
int *arry2[5]; | ||
|
||
for (int i = 0; i < 5; ++i) { | ||
arry2[i] = &arry[i]; | ||
} | ||
|
||
for (int i = 0; i < 5; ++i) { | ||
std::cout << *arry2[i] << std::endl; | ||
} | ||
} | ||
|
||
// pointer array address 2 | ||
void pointer_array2() { | ||
int balance[5] = {1000, 2, 3, 17, 50}; | ||
|
||
int *ptr = &balance[0] + 1; // the address increment using add and substract | ||
std::cout << *ptr << std::endl; | ||
|
||
// the array address is the first element of the array address | ||
// all the code below print the address of the array all the same | ||
std::cout << balance << std::endl; | ||
std::cout << &balance << std::endl; | ||
std::cout << &balance[0] << std::endl; | ||
} | ||
|
||
int main() { | ||
create_new_array_from_old(); | ||
pointer_array2(); | ||
return 0; | ||
} |
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,38 @@ | ||
#include <iostream> | ||
// casting is the process of converting a variable from one type to another. | ||
|
||
int main(int argc, char const *argv[]) { | ||
|
||
// TODO static cast | ||
// it performs a compile-time check and is used for well-defined, | ||
// non-polymorphic conversions like converting between numeric types, | ||
// or pointers and references of related types (upcasting or downcasting). | ||
// syntax -> new_type variable_name = static_cast<new_type>(old_variable); | ||
int x = 10; | ||
double y = static_cast<double>(x); // Converts int to double | ||
|
||
|
||
// TODO dynamic cast | ||
// This cast is used for handling polymorphism. | ||
// It is primarily used to cast pointers or references to base class objects to derived class objects. | ||
// It checks types at runtime and is useful in situations where you’re dealing with inheritance and want to ensure the cast is valid. | ||
|
||
// syntax -> Derived* d = dynamic_cast<Derived*>(base_pointer); | ||
class Base { | ||
virtual void foo() {} // Needs at least one virtual function | ||
}; | ||
|
||
class Derived : public Base { | ||
// Some code here | ||
}; | ||
|
||
Base *b = new Derived(); | ||
Derived *d = dynamic_cast<Derived *>(b); // Valid cast | ||
|
||
Base *b2 = new Base(); | ||
Derived *d2 = dynamic_cast<Derived *>(b2); // Invalid cast, returns nullptr | ||
|
||
|
||
|
||
return 0; | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,33 +1,66 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
|
||
// &variable = to get the address of the variable | ||
// *address = to get the value inside of address | ||
// basic pointer | ||
void pointer_tutorial() { | ||
// &variable = to get the address of the variable | ||
// *address = to get the value inside of address | ||
|
||
std::string name = "Johnyuth"; // the original string variable "name" that has the value of "Johnyuth" | ||
std::string *neam = &name; // a pointer of type string "neam" that used to store the memory address of the reference variable "name" | ||
std::string name = "Johnyuth"; // name | ||
std::string *pname = &name; // a pointer store the memory address of the reference variable "name" | ||
|
||
int main() { | ||
// create original variable | ||
std::cout << "Original = " << name << std::endl; | ||
std::cout << "Original Value of name : " << name << std::endl; // create original variable | ||
std::cout << "Original Adrs of name : " << &name << std::endl; // print out the memory address of name | ||
|
||
std::cout << "Pointer Value : " << pname << std::endl; | ||
std::cout << "Pointer Devalue : " << *pname << std::endl; // dereference, asterisk *pointer_variable | ||
|
||
// print out the memory address of name | ||
std::cout << "Adrs of Original = " << &name << std::endl; | ||
*pname = "Johnyuth has now changed"; // change the value of the pointer | ||
std::cout << "Changed value of the pointer : " << *pname << std::endl; | ||
std::cout << "Changed value of the Original : " << name << std::endl; | ||
} | ||
|
||
// create a pointer to the original variable and call the pointer 'neam' and print the 'neam' out | ||
std::cout << "Pointer address of Original = " << neam << std::endl; | ||
// function to generate and return random numbers | ||
int *getRandom() { | ||
srand((unsigned)time(NULL)); // set the seed | ||
static int r[3]; | ||
r[0] = rand(); | ||
r[1] = rand(); | ||
r[2] = rand(); | ||
|
||
// dereference : to get the value back that the memory of that address has. this operation can be done | ||
// with asterisk symbol (*). example : *pointer_variable | ||
std::cout << "Deference back for the value of Pointer = " << *neam << std::endl; | ||
for (int i = 0; i < 3; ++i) { | ||
r[i] = rand(); | ||
std::cout << r[i] << std::endl; | ||
} | ||
return r; | ||
} | ||
|
||
// change the value of the pointer | ||
*neam = "GOD YUTH"; | ||
// pass array pointer in | ||
double getAverage(int *arr, int size) { | ||
int sum = 0; | ||
for (int i = 0; i < size; ++i) { | ||
sum += arr[i]; | ||
} | ||
double avg = double(sum) / size; | ||
return avg; | ||
} | ||
|
||
// print out the new pointer value | ||
std::cout << "Changed value of the pointer = " << *neam << std::endl; | ||
void test_getAverage() { | ||
int balance[5] = {1000, 2, 3, 17, 50}; | ||
double avg = getAverage(balance, 5); | ||
std::cout << "Average value is: " << avg << std::endl; | ||
} | ||
|
||
int main() { | ||
pointer_tutorial(); | ||
|
||
std::cout << "Changed value of the Original = " << name << std::endl; | ||
int *p; // a pointer to an int. | ||
p = getRandom(); | ||
int a = *(p + 0); | ||
int b = *(p + 1); | ||
std::cout << "First value is :" << a << std::endl; | ||
std::cout << "Second value is :" << b << std::endl; | ||
|
||
// test_getAverage(); | ||
return 0; | ||
} |
Oops, something went wrong.