ProgramsC++

Object-Oriented Programming

Object-oriented programming in C++: classes, constructors, destructors, static members, five kinds of inheritance, overloading, virtual functions, abstract classes, friends, and this.

18 programs · open one, then the next

Start with Class and Object Basics
  1. 01Class and Object BasicsBeginnerCreate your first C++ class with private data and public member functions, then make objects from it. Learn the dot operator and why data is hidden.Open example →
  2. 02Default ConstructorBeginnerWrite a default constructor in C++ that runs when an object is created with no arguments. See how it sets safe starting values and when the compiler adds one.Open example →
  3. 03Parameterized ConstructorBeginnerPass values into a C++ parameterized constructor to set up each object in one line. Covers initializer lists, overloading, and why the default one vanishes.Open example →
  4. 04Copy ConstructorIntermediateUnderstand the C++ copy constructor: when it runs, how the default one copies member by member, and why classes that own raw pointers need a deep copy.Open example →
  5. 05DestructorIntermediateSee when a C++ destructor runs, why objects are destroyed in reverse order of creation, and how destructors release memory and other resources automatically.Open example →
  6. 06Static MembersIntermediateUse static data members and static functions in C++ to share one value, like an object counter, across every object. Covers defining it outside the class.Open example →
  7. 07Single InheritanceBeginnerLearn single inheritance in C++ by deriving Dog from Animal. Covers protected members, calling the base constructor, and what main can call on a Dog.Open example →
  8. 08Multiple InheritanceIntermediateLearn multiple inheritance in C++ with a Manager that inherits from Person and Employee. Covers constructor order, name clashes, and the :: fix.Open example →
  9. 09Multilevel InheritanceIntermediateBuild a three-level class chain in C++: Vehicle, Car, and SportsCar. See how constructors pass arguments up one level at a time and display cascades down.Open example →
  10. 10Hierarchical InheritanceIntermediateOne parent, many children in C++: Circle, Rectangle, and Triangle inherit from Shape and override display and calculateArea. Covers virtual and override.Open example →
  11. 11Hybrid InheritanceAdvancedHybrid inheritance in C++: a TeachingAssistant inherits from Student and Employee, both built on Person. See the diamond problem and the virtual fix.Open example →
  12. 12Function OverridingIntermediateOverride a base class function in C++ so Dog and Cat each supply their own makeSound. Shows how virtual enables polymorphism and what hiding looks like.Open example →
  13. 13Function OverloadingBeginnerOverload functions in C++ by giving one name several parameter lists, such as add for ints, doubles, and strings. Learn how the compiler picks a version.Open example →
  14. 14Operator OverloadingIntermediateOverload +, -, *, and << for a Complex class in C++. Learn member vs friend operators, why operator<< returns ostream&, and complex multiplication.Open example →
  15. 15Virtual FunctionsIntermediateUse virtual functions in C++ to reach the right draw and area code through a Shape pointer. Covers pure virtual functions, vtables, virtual destructors.Open example →
  16. 16Abstract ClassIntermediateCreate an abstract class in C++ with pure virtual makeSound and move, then implement it in Dog and Bird. Learn why it cannot be instantiated directly.Open example →
  17. 17Friend FunctionIntermediateLet an outside function or class read private data in C++ with friend. Covers a friend function and a friend class for a Box, and why friendship is limited.Open example →
  18. 18This PointerIntermediateUnderstand the this pointer in C++: separate members from same-named parameters, return *this for method chaining, and compare object addresses to test identity.Open example →