Created
May 25, 2024 04:21
-
-
Save karaketir16/771722f32c5416dd24892f1b75932217 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
int multiply(int number_a, int number_b){ | |
return number_a * number_b; | |
} | |
int my_function() | |
{ | |
int c; | |
c = 5; | |
int my_number; | |
std::cin >> my_number; | |
const int r = multiply(c, my_number); | |
std::cout << r << std::endl; | |
return 0; | |
} | |
void strings(){ | |
std::string my_string = "Hello"; | |
my_string += ", World!"; | |
std::cout << my_string << std::endl; | |
} | |
void example_if(int score){ | |
std::cout << "Score: " << score << " "; | |
if(score > 50){ | |
std::cout << "Greater Than 50"; | |
} else if (score > 25){ | |
std::cout << "Greater Than 25"; | |
} else { | |
std::cout << "Less Than 25"; | |
} | |
std::cout << std::endl; | |
} | |
void example_switch(char ch){ | |
std::cout << "Character ch: " << ch << " ---- "; | |
switch(ch){ | |
case 'a': | |
std::cout << "Your character is 'a'"; | |
break; | |
case 'b': | |
std::cout << "Your character is 'b'"; | |
case 'c': | |
std::cout << "Your character is 'c'"; | |
break; | |
default: | |
std::cout << "Your character is not a,b,c"; | |
} | |
std::cout << std::endl; | |
} | |
void loops(){ | |
int index = 0; | |
while(index < 5){ | |
std::cout << "While Index: " << index << std::endl; | |
index++; | |
} | |
std::cout << std::endl; | |
for(int i = 0; i < 7; i++){ | |
if(i == 2){ | |
continue; | |
} | |
std::cout << "For i: " << i << std::endl; | |
if(i == 5){ | |
break; | |
} | |
} | |
std::cout << std::endl; | |
} | |
int x = 5; | |
namespace myNamespace { | |
int y = 10; | |
} | |
void example_namespace(){ | |
std::cout << x | |
<< " " | |
<< myNamespace::y | |
<< std::endl; | |
} | |
using namespace std; | |
using namespace myNamespace; | |
void hello(){ | |
cout << "Hello World: " << x << " " << y << endl; | |
} | |
class MyClass { | |
public: | |
MyClass(){ //Constructor | |
attribute = 10; | |
cout << "Constructed: " << attribute << endl; | |
} | |
MyClass(int val){ | |
attribute = val; | |
cout << "Constructed: " << attribute << endl; | |
} | |
~MyClass(){ //Destructor | |
cout << "Destructed: " << attribute << endl; | |
} | |
int attribute; | |
void printAtribute(){ | |
cout << attribute << endl; | |
} | |
}; | |
void example_class(){ | |
MyClass m1; | |
MyClass *ptr; | |
cout << "Before Scope" << endl; | |
{ | |
MyClass m2(20); | |
ptr = new MyClass(30); | |
cout << m1.attribute << endl; | |
m2.printAtribute(); | |
} | |
cout << "After Scope" << endl; | |
ptr->attribute *= 2; | |
ptr->printAtribute(); | |
delete ptr; | |
} | |
class MySecondClass : public MyClass{ | |
public: | |
MySecondClass(int val) : MyClass(val * 2){ //initializer list | |
cout << "MySecondClass Constructed" << endl; | |
} | |
~MySecondClass(){ | |
cout << "MySecondClass Destructed" << endl; | |
} | |
}; | |
void example_class_2(){ | |
// MySecondClass m1; no default constructor | |
MySecondClass m1(5); | |
m1.printAtribute(); | |
} | |
class Class3{ | |
public: | |
int public_member; | |
//Default Constructor | |
protected: | |
int protected_member; | |
private: | |
int private_member; | |
}; | |
class Class4 : private Class3 { | |
public: | |
Class4(){ | |
this->public_member = 1; | |
this->protected_member = 2; | |
// this->private_member = 3; | |
} | |
}; | |
class Class5 : public Class4{ | |
public: | |
Class5(){ | |
// cout << public_member; | |
} | |
}; | |
class Class6 { | |
public: | |
virtual void printHello(){ | |
cout << "Hello from Class 6" << endl; | |
} | |
}; | |
class Class7 : public Class6 { | |
public: | |
void printHello(){ | |
cout << "Hello from Class 7" << endl; | |
} | |
}; | |
void example_class_3(){ | |
Class6 c6; | |
Class7 c7; | |
Class6 *ptr = &c7; | |
c6.printHello(); | |
c7.printHello(); | |
ptr->printHello(); | |
} | |
class MyInterface { | |
public: | |
virtual void Print(string str) = 0; | |
}; | |
void Printer(MyInterface *interface, string str){ | |
interface->Print(str); | |
} | |
class UpperCase: public MyInterface{ | |
public: | |
void Print(string str){ | |
for(int i = 0; i < str.size(); i++){ | |
str[i] = toupper(str[i]); | |
} | |
cout << str; | |
} | |
}; | |
class LowerCase: public MyInterface{ | |
public: | |
void Print(string str){ | |
for(int i = 0; i < str.size(); i++){ | |
str[i] = tolower(str[i]); | |
} | |
cout << str; | |
} | |
}; | |
int main(){ | |
UpperCase up; | |
LowerCase low; | |
Printer(&up, "This Will be Upper"); | |
cout << endl; | |
Printer(&low, "This Will be Lower"); | |
cout << endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment