From b64f6711bd7ffcd55a87d74b0ba6d116f99c9d7f Mon Sep 17 00:00:00 2001 From: ganome Date: Mon, 26 Aug 2024 21:08:46 -0600 Subject: [PATCH] W1 Day5 ANSI C++ - polymorphism --- .gitignore | 10 ++++++ .vscode/settings.json | 64 +++++++++++++++++++++++++++++++++- cpp/ANSI/W1/Day5/5.1/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.1/main.cpp | 26 ++++++++++++++ cpp/ANSI/W1/Day5/5.10/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.10/main.cpp | 36 +++++++++++++++++++ cpp/ANSI/W1/Day5/5.2/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.2/main.cpp | 24 +++++++++++++ cpp/ANSI/W1/Day5/5.3/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.3/main.cpp | 35 +++++++++++++++++++ cpp/ANSI/W1/Day5/5.4/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.4/main.cpp | 28 +++++++++++++++ cpp/ANSI/W1/Day5/5.5/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.5/main.cpp | 27 ++++++++++++++ cpp/ANSI/W1/Day5/5.6/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.6/main.cpp | 33 ++++++++++++++++++ cpp/ANSI/W1/Day5/5.7/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.7/main.cpp | 31 ++++++++++++++++ cpp/ANSI/W1/Day5/5.8/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.8/main.cpp | 60 +++++++++++++++++++++++++++++++ cpp/ANSI/W1/Day5/5.9/Makefile | 13 +++++++ cpp/ANSI/W1/Day5/5.9/main.cpp | 30 ++++++++++++++++ 22 files changed, 533 insertions(+), 1 deletion(-) create mode 100644 cpp/ANSI/W1/Day5/5.1/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.1/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.10/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.10/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.2/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.2/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.3/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.3/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.4/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.4/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.5/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.5/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.6/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.6/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.7/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.7/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.8/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.8/main.cpp create mode 100644 cpp/ANSI/W1/Day5/5.9/Makefile create mode 100644 cpp/ANSI/W1/Day5/5.9/main.cpp diff --git a/.gitignore b/.gitignore index c95de48..5a45534 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,13 @@ cpp/ANSI/W1/Day4/4.8/fixedbracesif cpp/ANSI/W1/Day4/Quiz/quiz cpp/ANSI/W1/Day4/Ex.LogicalOperators/logicaloperators cpp/ANSI/W1/Day4/4.9/conditionaloperator +cpp/ANSI/W1/Day5/5.1/basicfunction +cpp/ANSI/W1/Day5/5.10/fibonaccisequence +cpp/ANSI/W1/Day5/5.2/localvariables +cpp/ANSI/W1/Day5/5.3/localvariables2 +cpp/ANSI/W1/Day5/5.4/localvariables3 +cpp/ANSI/W1/Day5/5.5/localvariables4 +cpp/ANSI/W1/Day5/5.6/localvariables5 +cpp/ANSI/W1/Day5/5.7/defaultparamvalues +cpp/ANSI/W1/Day5/5.8/polymorphism +cpp/ANSI/W1/Day5/5.9/inlinefunctions diff --git a/.vscode/settings.json b/.vscode/settings.json index 8b983b4..e7f7519 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,68 @@ "initializer_list": "cpp", "span": "cpp", "ostream": "cpp", - "iostream": "cpp" + "iostream": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "atomic": "cpp", + "strstream": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cfenv": "cpp", + "charconv": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "map": "cpp", + "set": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "source_location": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stdfloat": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "variant": "cpp" } } \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.1/Makefile b/cpp/ANSI/W1/Day5/5.1/Makefile new file mode 100644 index 0000000..fe068d6 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.1/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o basicfunction main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: basicfunction + ./basicfunction + +clean: + rm -f basicfunction diff --git a/cpp/ANSI/W1/Day5/5.1/main.cpp b/cpp/ANSI/W1/Day5/5.1/main.cpp new file mode 100644 index 0000000..b93019f --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.1/main.cpp @@ -0,0 +1,26 @@ +// Demonstrates how to declare functions +#include + +int Area(int length, int width); // A function prototype because it ends with a semicolon + +int main() +{ + using namespace std; + + int lengthOfYard, widthOfYard, areaOfYard; + + cout << "\nHow wide is your yard? "; + cin >> widthOfYard; + cout << "\nHow long is your yard? "; + cin >> lengthOfYard; + + areaOfYard = Area(lengthOfYard, widthOfYard); + + cout << "\nYour yard is " << areaOfYard << " square feet!\n\n"; + return 0; +} + +int Area(int len, int wid) +{ + return len * wid; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.10/Makefile b/cpp/ANSI/W1/Day5/5.10/Makefile new file mode 100644 index 0000000..40f5851 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.10/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o fibonaccisequence main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: fibonaccisequence + ./fibonaccisequence + +clean: + rm -f fibonaccisequence diff --git a/cpp/ANSI/W1/Day5/5.10/main.cpp b/cpp/ANSI/W1/Day5/5.10/main.cpp new file mode 100644 index 0000000..ff48a46 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.10/main.cpp @@ -0,0 +1,36 @@ +/* Demonstrates fibonacci series +using recursion */ +#include +double fib(double n); + +using namespace std; + +int main() +{ + double n, answer; + cout << "Enter number to find: "; + cin >> n; + cout << "\n\n"; + + answer = fib(n); + + cout << answer << " is the " << n << "th Fibonacci number\n"; + return 0; +} + +double fib(double n) +{ + cout << "Processing fib(" << n << ")..."; + + if (n < 3) + { + cout << "Return 1!\n"; + return (1); + } + else + { + cout << "Call fib(" << n-2 << ") "; + cout << "and fib (" << n-1 << ").\n"; + return (fib(n-2) + fib(n-1)); + } +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.2/Makefile b/cpp/ANSI/W1/Day5/5.2/Makefile new file mode 100644 index 0000000..b9400a1 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.2/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o localvariables main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: localvariables + ./localvariables + +clean: + rm -f localvariables diff --git a/cpp/ANSI/W1/Day5/5.2/main.cpp b/cpp/ANSI/W1/Day5/5.2/main.cpp new file mode 100644 index 0000000..d948790 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.2/main.cpp @@ -0,0 +1,24 @@ +/* Demonstrates how variables defined inside functions +are only available inside that function*/ +#include + +float Convert(float); +int main() +{ + using namespace std; + + float TempFer; + float TempCel; + + cout << "Please enter the temperature in Fahrenheit: "; + cin >> TempFer; + TempCel = Convert(TempFer); + cout << "\nHeres the temperate in Celsius: " << TempCel << endl; + return 0; +} +float Convert(float TempFer) +{ + float TempCel; + TempCel = ((TempFer -32) * 5 ) / 9; + return TempCel; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.3/Makefile b/cpp/ANSI/W1/Day5/5.3/Makefile new file mode 100644 index 0000000..52e818f --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.3/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o localvariables2 main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: localvariables2 + ./localvariables2 + +clean: + rm -f localvariables2 diff --git a/cpp/ANSI/W1/Day5/5.3/main.cpp b/cpp/ANSI/W1/Day5/5.3/main.cpp new file mode 100644 index 0000000..ccb84c9 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.3/main.cpp @@ -0,0 +1,35 @@ +/* Demonstrates how the same variable name can be used in + different functions with different values*/ + #include +using namespace std; // Global Declaration + + void myFunc(); + + int main() + { + using namespace std; // Local declaration + + int x = 5; + + cout << "In main the x value is: " << x << endl; + + myFunc(); + cout << "\nBack in main again - and X is: " << x << endl; + return 0; + } + + void myFunc() + { + int x = 8; + cout << "In myFunc x is : " << x << endl; + + { + cout << "\nIn a nested code block inside myFunc X is: " << x << endl; + + int x = 420; + + cout << "Very local inside codeblock defined X is: " << x << endl; + } + + cout << "\nOut of the code block and back in myFun the x equals: " << x << " again.\n"; + } \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.4/Makefile b/cpp/ANSI/W1/Day5/5.4/Makefile new file mode 100644 index 0000000..dddbaf4 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.4/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o localvariables3 main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: localvariables3 + ./localvariables3 + +clean: + rm -f localvariables3 diff --git a/cpp/ANSI/W1/Day5/5.4/main.cpp b/cpp/ANSI/W1/Day5/5.4/main.cpp new file mode 100644 index 0000000..6dbfb83 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.4/main.cpp @@ -0,0 +1,28 @@ +/* Demonstrates passing by value. This means that when you pass +a variable or value to a function; that function makes it's own +copy of the variable. leaving the original value in tact in the calling function */ +#include +using namespace std; +void swap(int x, int y); + +int main() +{ + int x = 5, y = 10; + + cout << "Main. Before swap, X: " << x << " Y: " << y << endl; + swap(x,y); + cout << "Main. After swap, X: " << x << " Y: " << y << endl; +} + +void swap (int x, int y) +{ + int temp; + + cout << "in Swap. Before swap, X: " << x << " Y: " << y << endl; + + temp = x; + x = y; + y = temp; + + cout << "in Swap. After swap, X: " << x << " Y: " << y << endl; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.5/Makefile b/cpp/ANSI/W1/Day5/5.5/Makefile new file mode 100644 index 0000000..82dcd37 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.5/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o localvariables4 main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: localvariables4 + ./localvariables4 + +clean: + rm -f localvariables4 diff --git a/cpp/ANSI/W1/Day5/5.5/main.cpp b/cpp/ANSI/W1/Day5/5.5/main.cpp new file mode 100644 index 0000000..34a08e3 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.5/main.cpp @@ -0,0 +1,27 @@ +/* Further demonstration of global and local variables */ +#include + +using namespace std; + +void myFunction(); + +int x = 5, y = 7; // Global variables + +int main() +{ + cout << "x from main: " << x << endl; + cout << "y from main: " << y << endl; + myFunction(); + cout << "Back from myFunction!" << endl << endl; + cout << "x from main: " << x << endl; + cout << "y from main: " << y << endl; + return 0; +} + +void myFunction() +{ + int y = 10; + + cout << "x from myFunction: " << x << endl; + cout << "y from myFunction: " << y << endl << endl; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.6/Makefile b/cpp/ANSI/W1/Day5/5.6/Makefile new file mode 100644 index 0000000..416b3b9 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.6/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o localvariables5 main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: localvariables5 + ./localvariables5 + +clean: + rm -f localvariables5 diff --git a/cpp/ANSI/W1/Day5/5.6/main.cpp b/cpp/ANSI/W1/Day5/5.6/main.cpp new file mode 100644 index 0000000..6f0f5a6 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.6/main.cpp @@ -0,0 +1,33 @@ +/* Demonstrates multiple return statements */ +#include + +using namespace std; + +int Doubler(int AmountToDouble); + +int main() +{ + int result = 0, input; + + cout << "Enter a number between 0 and 10,000 to double: "; + cin >> input; + + cout << "\nBefore doubler is called..."; + cout << "\nInput #: " << input << " doubled: " << result << "\n"; + + result = Doubler(input); + + cout << "\nBack from doubler()...\n"; + cout << "\ninput #: " << input << " doubled: " << result << endl; + + return 0; +} + +int Doubler(int original) +{ + if (original <= 10000) + return original * 2; + else + return -1; + cout << "You cant get here!\n"; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.7/Makefile b/cpp/ANSI/W1/Day5/5.7/Makefile new file mode 100644 index 0000000..97617f7 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.7/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o defaultparamvalues main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: defaultparamvalues + ./defaultparamvalues + +clean: + rm -f defaultparamvalues diff --git a/cpp/ANSI/W1/Day5/5.7/main.cpp b/cpp/ANSI/W1/Day5/5.7/main.cpp new file mode 100644 index 0000000..a46fe42 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.7/main.cpp @@ -0,0 +1,31 @@ +/* Demonstrates the use +default parameter values */ + +#include + +using namespace std; + +int AreaCube(int length, int width = 25, int height = 1); + +int main() +{ + int length = 100; + int width = 50; + int height = 2; + int area; + + area = AreaCube(length, width, height); + cout << "First area equals: " << area << endl; + + area = AreaCube(length, width); + cout << "Second time area equals: " << area << endl; + + area = AreaCube(length); + cout << "Third time area quals: " << area << endl; + return 0; +} + +int AreaCube(int length, int width, int height) +{ + return (length * width * height); +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.8/Makefile b/cpp/ANSI/W1/Day5/5.8/Makefile new file mode 100644 index 0000000..1b7f4e3 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.8/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o polymorphism main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: polymorphism + ./polymorphism + +clean: + rm -f polymorphism diff --git a/cpp/ANSI/W1/Day5/5.8/main.cpp b/cpp/ANSI/W1/Day5/5.8/main.cpp new file mode 100644 index 0000000..4815175 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.8/main.cpp @@ -0,0 +1,60 @@ +/* Demonstrates function polymorphism +also known as function overloading */ +#include + +using namespace std; + +int Double(int); +long Double(long); +float Double(float); +double Double(double); + +int main() +{ + int myInt = 6500; + long myLong = 65000; + float myFloat = 6.5F; + double myDouble = 6.5e20; + + int doubledInt; + long doubledLong; + float doubledFloat; + double doubledDouble; + + cout << "myInt: " << myInt << endl << "myLong: " << myLong << endl; + cout << "myFloat: " << myFloat << endl << "myDouble: " << myDouble << endl; + + doubledInt = Double(myInt); + doubledLong = Double(myLong); + doubledFloat = Double(myFloat); + doubledDouble = Double(myDouble); + + cout << "doubledInt: " << doubledInt << "\ndoubledLong: " << doubledLong << endl; + cout << "doubledFloat: " << doubledFloat << "\ndoubledDouble: " << doubledDouble << endl; + + return 0; +} + +int Double(int original) +{ + cout << "In Double*int)\n"; + return 2 * original; +} + +long Double(long original) +{ + cout << "In Double(long)\n"; + return 2 * original; +} + +float Double(float original) +{ + cout << "in Double(float)\n"; + return 2 * original; +} + +double Double(double original) +{ + cout << "in Double(double)\n"; + return 2 * original; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day5/5.9/Makefile b/cpp/ANSI/W1/Day5/5.9/Makefile new file mode 100644 index 0000000..d55562d --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.9/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o inlinefunctions main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: inlinefunctions + ./inlinefunctions + +clean: + rm -f inlinefunctions diff --git a/cpp/ANSI/W1/Day5/5.9/main.cpp b/cpp/ANSI/W1/Day5/5.9/main.cpp new file mode 100644 index 0000000..0372385 --- /dev/null +++ b/cpp/ANSI/W1/Day5/5.9/main.cpp @@ -0,0 +1,30 @@ +/* Demonstrates inline functions */ +#include + +using namespace std; + +inline int Double(int); + +int main() +{ + int target; + + cout << "Enter a number to work with: "; + cin >> target; + cout << endl; + + target = Double(target); + cout << "Target: " << target << endl; + + target = Double(target); + cout << "Target: " << target << endl; + + target = Double(target); + cout << "Target: " << target << endl; + return 0; +} + +int Double(int target) +{ + return 2 * target; +} \ No newline at end of file