Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuth committed Nov 22, 2024
1 parent 683fb7a commit 3a76231
Show file tree
Hide file tree
Showing 64 changed files with 2,757 additions and 253 deletions.
8 changes: 6 additions & 2 deletions class/polymorphism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ int main() {
Teacher tec1("NB-", "CU", 26, "Math");
tec1.IntroduceYourself();

Employee *e1 = &dev1; // polymorphism : create a pointer of type super class that point to address to derived child class
// polymorphism : create a pointer of type super class that point to address to derived child class
Employee *e1 = &dev1;
Employee *t1 = &tec1;

e1->work(); // polymorphism : then a pointer can directly use method and properties of derived child class. for normal object use "." symbol. for pointer object use "->".
// polymorphism : then a pointer can directly use method and properties of derived child class.
// for normal object use "." symbol. for pointer object use "->".
e1->work();

t1->work();

return 0;
Expand Down
36 changes: 36 additions & 0 deletions cppsyntax/array.cpp
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;
}
38 changes: 38 additions & 0 deletions cppsyntax/casting.cpp
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;
}
30 changes: 29 additions & 1 deletion cppsyntax/data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int x = 5; // 2 or 4 bytes | Integer (whole number without de
float y = 5.99f; // 4 bytes | Decimal point number Sufficient for storing 7 decimal digits
double z = 9.98; // 8 bytes | Decimal point number Sufficient for storing 15 decimal digits
char D = 'D'; // 1 bytes | Character Stores a single character/letter/number, or ASCII values
const char* st = "string"; // | string literal in pure form. "" must be use instead of ''
const char *st = "string"; // | string literal in pure form. "" must be use instead of ''
std::string string = "Hello"; // | string class. must include string
char sta[] = "string"; // | string in form of array of char
bool bol = true; // 1 bytes | true or false. Stores true or false values
Expand All @@ -24,5 +24,33 @@ int main() {
std::cout << sta << "\n";
std::cout << imin << " , " << imax << std::endl;

std::cout << "Hello \rMr. Nobby-"; // print hello to screen
std::cout << "\n"; // print newline
std::cout << 5; // can directly print number
std::cout << "\n";
std::cout << "5"; // can directly print number
std::cout << "\n";
std::cout << "\r";

int num = 42;
double pi = 3.14159;
double *pPi = &pi;
char ch = 'A';
const char *str = "Hello, World!";

printf("Integer: %d\n", num);
printf("Unsigned Integer: %u\n", (unsigned int)num);
printf("Octal: %o\n", num);
printf("Hexadecimal: %x\n", num);
printf("Floating-point: %f\n", pi);
printf("Scientific Notation: %e\n", pi);
printf("Character: %c\n", ch);
printf("String: %s\n", str);

printf("Space Padding: %15f\n", pi); // must be bigger than string size
printf("Precision: %.15f\n", pi);

printf("Pointer Address: %p\n", pPi);

return 0;
}
19 changes: 0 additions & 19 deletions cppsyntax/function_array_pointer.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions cppsyntax/function_input_array.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions cppsyntax/function_pass_by_reference.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions cppsyntax/function_pointer_argument.cpp

This file was deleted.

49 changes: 0 additions & 49 deletions cppsyntax/function_return_array.cpp

This file was deleted.

71 changes: 52 additions & 19 deletions cppsyntax/pointer.cpp
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;
}
Loading

0 comments on commit 3a76231

Please sign in to comment.