We cover a lot in this tutorial. We cover Polymorphism, Structs, Friend Classes, Abstract Classes, Override, Final, Virtual and much more.
All of the code used follows the video below. For best results print it and take notes as you watch. Please leave any questions that you have. I hope this tutorial clears up Polymorphism for everyone.
If you like videos like this, consider donating $1, or simply turn off Ad Blocking software. Either helps me to continue making free tutorials.
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
// ---------- C++ TUTORIAL 11 ---------- // ---------- STRUCT EXAMPLE ---------- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <ctime> #include <numeric> #include <cmath> // Classes have default private fields and methods // while structs have public // Structs are used to model new data types, while // classes model more complex real world objects struct Shape{ // Variables are public by default double length, width; // Constructors are public by default Shape(double l = 1, double w = 1){ length = l; width = w; } // Structs can contain functions double Area(){ return length * width; } // Structs can contain private members private: int id; }; // You can inherit from a struct struct Circle : Shape{ // Override the constructor // You also use this with structs Circle(double width){ this->width = width; } // Override Area() double Area(){ return 3.14159 * std::pow((width / 2), 2); } }; int main() { // Create a struct Shape shape(10, 10); // Call a Struct function std::cout << "Square Area : " << shape.Area() << "\n"; // Create a struct Circle circle(10); // Call a Struct function std::cout << "Circle Area : " << circle.Area() << "\n"; // You can initialize a struct or class using // an aggregate Shape rectangle{10,15}; std::cout << "Rectangle Area : " << rectangle.Area() << "\n"; return 0; } // ---------- END STRUCT EXAMPLE ---------- // ---------- FRIEND CLASS EXAMPLE ---------- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <ctime> #include <numeric> #include <cmath> // Private members of a class are not accessable // to outside code unless a class is marked as // a friend class Customer{ private: friend class GetCustomerData; std::string name; public: Customer(std::string name){ this->name = name; } }; class GetCustomerData{ public: static std::string GetName(Customer& customer){ return customer.name; } }; int main() { Customer tom("Tom"); GetCustomerData getName; std::cout << "Name : " << getName.GetName(tom) << "\n"; return 0; } // ---------- END FRIEND CLASS EXAMPLE ---------- // ---------- POLYMORPHISM EXAMPLE ---------- // Polymorphism is a feature in which similar // objects can be treated the same, but also // utilize their differences automatically // for any methods marked as virtual class Shape{ protected: double height; double width; public: // Assume that if only 1 value is passed // that height and width are equal Shape(double length){ height = length; width = length; } Shape(double h, double w){ height = h; width = w; } virtual double Area(){ return height * width; } }; class Circle : public Shape{ public: Circle(double w) : Shape(w){ } // Override Area() double Area(){ return 3.14159 * std::pow((width / 2), 2); } }; // This function receives Shapes but uses the // correct Area() automatically void ShowArea(Shape& shape){ std::cout << "Area : " << shape.Area() << "\n"; } int main() { Shape square(10,5); Circle circle(10); ShowArea(square); ShowArea(circle); return 0; } // ---------- END POLYMORPHISM EXAMPLE ---------- // ---------- ABSTRACT CLASS / OVERRIDE EXAMPLE ---------- // Shape here has no purpose except for // being able to to group similar objects // so it should be instead an Abstract // Class class Shape{ public: virtual double Area() = 0; }; class Circle : public Shape{ protected: double width; public: Circle(double w){ width = w; } // Override Area() // You should use override to force the // compiler to check if the base class // virtual fucntion is the same as // the subclass double Area() override{ return 3.14159 * std::pow((width / 2), 2); } }; class Rectangle : public Shape{ protected: double height, width; public: Rectangle(double h, double w){ height = h; width = w; } // Override Area() // Marking a method as final means // that subclasses that inherit from // Rectangle can't override Area() double Area() override final{ return height * width; } }; class Square : public Rectangle{ public: Square(double h, double w) : Rectangle(h,w){ } /* This would trigger an error double Area() override{ return height * 2; } */ }; // This function receives Shapes but uses the // correct Area() automatically void ShowArea(Shape& shape){ std::cout << "Area : " << shape.Area() << "\n"; } int main() { Rectangle rectangle(10,5); Circle circle(10); ShowArea(rectangle); ShowArea(circle); Square square(10,10); ShowArea(square); return 0; } // ---------- END ABSTRACT CLASS / OVERRIDE EXAMPLE ---------- // ---------- END C++ TUTORIAL 11 ---------- |
Leave a Reply