This is an old revision of the document!
Corning Community College
CSCS1320 C/C++ Programming
Collaborate in the creation of a useful, originally-voiced informational source on the various aspects of C++ programming, specifically polymorphism, and the implementation of a simple program that has and uses classes with polymorphism.
Additionally, the entire class will be participating in documenting and filling out this project page. It is the responsibility of EACH class member to:
Compile time polymorphism is achieved by overloading. This can be in the form of operator overloading, function overloading, or with templates.
Templates are used to create functions that work with multiple types. At compile time the compiler will substitute the template type with the type given at the function's call.
#include <cstdio> struct Foo; template<typename T> const char* is_nullptr(T var) { if (var == nullptr) return "nullptr"; else return "NOT nullptr"; } int main() { int x{ 5 }; int* xPtr{ &x }; long* nullPtr{ nullptr }; Foo* fooPtr{ nullptr }; std::printf("xPtr is %s\n", is_nullptr(xPtr)); std::printf("nullPtr is %s\n", is_nullptr(nullPtr)); std::printf("fooPtr is %s\n", is_nullptr(fooPtr)); return 0; }
In this example you can call is_nullptr with any type, at compile time the compiler will create three separate functions: one that takes int pointers, one that takes long pointers, and one that takes Foo pointers.
This example produces this result:
xPtr is NOT nullptr nullPtr is nullptr fooPtr is nullptr
Templates help clean up your code and can be extremely powerful, especially when combined with type traits.
Run time polymorphism is achieved by overriding. Overriding occurs when a child class redefines a parent class's member function.
Using the virtual keyword marks your functions as virtual. Virtual functions always call the most derived version of a member function.
#include <cstdio> struct Rectangle { const char* type() const { return "Rectangle"; } }; struct Square : Rectangle { const char* type() const { return "Square"; } }; void print_type(const Rectangle& shape) { std::printf("%s\n", shape.type()); } int main() { const Rectangle rect; const Square square; std::printf("Rectangle: "); print_type(rect); std::printf("Square: "); print_type(square); return 0; }
Running this code produces this result:
Rectangle: Rectangle Square: Rectangle
However, with the addition of the virtual keyword:
virtual const char* type() const { return "Rectangle"; }
We now get the desired result:
Rectangle: Rectangle Square: Square
Notice objects deriving from Rectangle are passed by reference because if you pass by value a new Rectangle object will be created, therefore using Rectangle methods.
Write a program that makes use of polymorphism. It can be anything, so long as you genuinely implement it and it works.
Be sure to make use of and follow proper conventions having public, protected, and private access control (no cheating by making everything public), and demonstrate polymorphism.
Have a main() function that instantiates an instance of your chosen theme of class with polymorphism, and runs that instantiated object through a range of tests demonstrating the class works as intended (likely also prompting the user to input any needed values to configure the attributes of the object).
I'll be looking for the following:
104:oop2:final tally of results (104/104) *:oop2:no compiler messages, program compiles and runs without issue [26/26] *:oop2:specified functionality is implemented [26/26] *:oop2:project page contributions as per project specifications [52/52]
Additionally: