C++ Module 04
C++ Module 04: Embracing Subtype Polymorphism, Abstract Classes, and Interfaces
Introduction
Module 04 of the C++ curriculum at 1337 coding school delves into the realm of subtype polymorphism, abstract classes, and interfaces. The exercises aim to reinforce the understanding of object-oriented programming principles, emphasizing polymorphism and encapsulation. Students will design and implement classes that leverage inheritance, abstract classes, and interfaces to create a cohesive system.
General Rules Recap
The general rules for this module remain consistent with the previous ones, emphasizing clean code, adherence to naming conventions, and compliance with the C++98 standard. Students are encouraged to leverage the C++ standard library while avoiding external libraries and functions that are forbidden.
Exercise 00: Polymorphism
Task: Implement a base class Animal
and derived classes Dog
and Cat
. Utilize subtype polymorphism to enable each animal to make a distinct sound. Emphasize dynamic dispatch by using pointers to the base class. Introduce a WrongCat
class to test the importance of correct inheritance.
Exercise 01: I don’t want to set the world on fire
Task: Design a Brain
class and incorporate it into the Dog
and Cat
classes. Manage memory appropriately by using dynamic memory allocation and deallocation. Create an array of Animal
objects, ensuring proper destruction of each object and checking for memory leaks.
Exercise 02: Abstract class
Task: Modify the Animal
class to make it abstract, preventing its instantiation. This ensures that only derived classes like Dog
and Cat
can be instantiated. Verify that the functionality remains intact while preventing direct instantiation of Animal
.
Exercise 03: Interface & recap
Task: Implement an interface-like structure using pure abstract classes to define contracts for classes. Create classes AMateria
, Ice
, Cure
, ICharacter
, Character
, IMateriaSource
, and MateriaSource
. Explore deep copying, inventory management, and Materia creation through learning "templates." Emphasize encapsulation and polymorphism.
Conclusion
Module 04 deepens students' understanding of object-oriented programming concepts by introducing subtype polymorphism, abstract classes, and interfaces. The exercises challenge students to apply these principles effectively, fostering the creation of flexible and extensible code. By the end of this module, students gain proficiency in leveraging C++ features to build complex systems with polymorphic behavior.
Happy coding, and may your codebase flourish with the power of polymorphism and abstract design!