W1 Day5 ANSI C++ - polymorphism
This commit is contained in:
parent
4c7c1cfb2a
commit
b64f6711bd
10
.gitignore
vendored
10
.gitignore
vendored
@ -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
|
||||
|
||||
64
.vscode/settings.json
vendored
64
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.1/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.1/Makefile
Normal file
@ -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
|
||||
26
cpp/ANSI/W1/Day5/5.1/main.cpp
Normal file
26
cpp/ANSI/W1/Day5/5.1/main.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// Demonstrates how to declare functions
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.10/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.10/Makefile
Normal file
@ -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
|
||||
36
cpp/ANSI/W1/Day5/5.10/main.cpp
Normal file
36
cpp/ANSI/W1/Day5/5.10/main.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
/* Demonstrates fibonacci series
|
||||
using recursion */
|
||||
#include <iostream>
|
||||
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));
|
||||
}
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.2/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.2/Makefile
Normal file
@ -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
|
||||
24
cpp/ANSI/W1/Day5/5.2/main.cpp
Normal file
24
cpp/ANSI/W1/Day5/5.2/main.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
/* Demonstrates how variables defined inside functions
|
||||
are only available inside that function*/
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.3/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.3/Makefile
Normal file
@ -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
|
||||
35
cpp/ANSI/W1/Day5/5.3/main.cpp
Normal file
35
cpp/ANSI/W1/Day5/5.3/main.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
/* Demonstrates how the same variable name can be used in
|
||||
different functions with different values*/
|
||||
#include <iostream>
|
||||
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";
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.4/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.4/Makefile
Normal file
@ -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
|
||||
28
cpp/ANSI/W1/Day5/5.4/main.cpp
Normal file
28
cpp/ANSI/W1/Day5/5.4/main.cpp
Normal file
@ -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 <iostream>
|
||||
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;
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.5/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.5/Makefile
Normal file
@ -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
|
||||
27
cpp/ANSI/W1/Day5/5.5/main.cpp
Normal file
27
cpp/ANSI/W1/Day5/5.5/main.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
/* Further demonstration of global and local variables */
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.6/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.6/Makefile
Normal file
@ -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
|
||||
33
cpp/ANSI/W1/Day5/5.6/main.cpp
Normal file
33
cpp/ANSI/W1/Day5/5.6/main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
/* Demonstrates multiple return statements */
|
||||
#include <iostream>
|
||||
|
||||
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";
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.7/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.7/Makefile
Normal file
@ -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
|
||||
31
cpp/ANSI/W1/Day5/5.7/main.cpp
Normal file
31
cpp/ANSI/W1/Day5/5.7/main.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
/* Demonstrates the use
|
||||
default parameter values */
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.8/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.8/Makefile
Normal file
@ -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
|
||||
60
cpp/ANSI/W1/Day5/5.8/main.cpp
Normal file
60
cpp/ANSI/W1/Day5/5.8/main.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
/* Demonstrates function polymorphism
|
||||
also known as function overloading */
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
13
cpp/ANSI/W1/Day5/5.9/Makefile
Normal file
13
cpp/ANSI/W1/Day5/5.9/Makefile
Normal file
@ -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
|
||||
30
cpp/ANSI/W1/Day5/5.9/main.cpp
Normal file
30
cpp/ANSI/W1/Day5/5.9/main.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* Demonstrates inline functions */
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user