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- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> using namespace std; class Counter {
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> using namespace std; // Base class
- 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 →cpp#include <iostream> using namespace std; class Calculator {
- 14Operator OverloadingIntermediateOverload +, -, *, and << for a Complex class in C++. Learn member vs friend operators, why operator<< returns ostream&, and complex multiplication.Open example →cpp#include <iostream> using namespace std; class Complex {
- 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 →cpp#include <iostream> using namespace std; // Base class
- 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 →cpp#include <iostream> #include <string> using namespace std;
- 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 →cpp#include <iostream> using namespace std; class Box {
- 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 →cpp#include <iostream> #include <string> using namespace std;