GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Object Oriented Programming in C++ - GeeksforGeeks Object Oriented Programming in C++Last Updated : 23 Oct, 2026 Object-Oriented Programming (OOP) is a programming paradigm that organizes programs around classes and objects. A class defines the data members and member functions, while an object is an instance of a class. OOP helps build modular, reusable, and maintainable software by modeling real-world entities.
Characteristics of OOPObject-Oriented Programming is characterized by several fundamental concepts that make software more modular, reusable, and easier to maintain. ![]() ClassA class in C++ is a user-defined data type that acts as a blueprint for creating objects. It defines the data members and member functions that describe the properties and behavior of the objects.
Example: A Car represents a class (blueprint), while BMW, Mercedes, and Audi are objects (instances) created from that class. ![]() ObjectAn object in C++ is an instance of a class that represents a real-world entity. It is used to access the data members and member functions defined in the class.
Example: Dog is a class, while Tommy is an object (instance) of that class. ![]() ConstructorA constructor is a special member function of a class that is automatically called when an object is created. It is mainly used to initialize the object's data members and establish its initial state.
Example: When creating a Car object, a constructor can automatically initialize data members such as brand, model, and color. Types of Constructors C++ provides different types of constructors that are used to initialize objects in different ways. ![]()
DestructorA Destructor is a special member function that is automatically invoked when an object is destroyed. It is primarily used to release resources such as dynamically allocated memory, open files, or network connections.
Example: When a File object is destroyed, its destructor can automatically close the file and release associated resources. Access SpecifiersAccess specifiers control the visibility and accessibility of class members. They help implement encapsulation by restricting access to data and member functions.
Example: A bank account stores its balance as a private member while allowing controlled access through public member functions such as deposit() and withdraw(). Data HidingData hiding is the practice of restricting direct access to an object's data and allowing it to be accessed only through controlled member functions. In C++, it is achieved using access specifiers such as private and protected.
Example: A BankAccount class keeps its balance private. Users cannot modify it directly and must use functions such as deposit(), withdraw(), or getBalance(). Four Pillars of OOP in C++Object-Oriented Programming in C++ is built on four fundamental principles that help create modular, reusable, and maintainable software. 1. AbstractionAbstraction is the process of hiding implementation details and exposing only the essential features of an object. It allows users to interact with an object without knowing how its functionality is implemented internally.
Example: An ATM or a coffee machine demonstrates abstraction, where users perform operations such as withdrawing cash or selecting a beverage without knowing the internal implementation. How to Achieve Abstraction in C++ Abstraction in C++ is achieved using abstract classes and pure virtual functions. An abstract class defines a common interface, while derived classes provide the implementation for the abstract functions.
Example: An abstract class Shape can declare a pure virtual function draw(), while derived classes such as Circle and Rectangle provide their own implementations of draw(). This allows users to work with the common Shape interface without knowing the implementation details of each shape. 2. EncapsulationEncapsulation is the process of combining data members and member functions into a single unit (class) while restricting direct access to the data. It helps protect data by allowing controlled access through member functions.
Example: A Bank Account hides its balance using the private access specifier. The balance can only be accessed or modified through public functions such as deposit(), withdraw(), and getBalance(). ![]() 3. InheritanceInheritance is a fundamental OOP concept in C++ that allows a class to acquire the data members and member functions of another class. It establishes an "is-a" relationship between classes and promotes code reuse.
Example: Dog, Cat, and Cow can be derived classes of the Animal base class because each one is an Animal. ![]() Types of Inheritance C++ supports the following types of inheritance:
4. PolymorphismPolymorphism means "many forms", where the same interface can perform different actions depending on the object or context. In C++, it allows the same function or operator to exhibit different behavior in different situations.
Example: Different animals demonstrate polymorphism, where the same speak() function produces different outputs such as Bark, Meow, and Moo depending on whether the object is of type Dog, Cat, or Cow. ![]() Types of Polymorphism in C++ Polymorphism in C++ is mainly classified into the following two types: ![]() 1. Compile-time Polymorphism (Static Polymorphism) Compile-time polymorphism is resolved by the compiler during compilation. Compile-time polymorphism in C++ is achieved using:
2. Run-time Polymorphism (Dynamic Polymorphism) Run-time polymorphism is resolved during program execution based on the actual type of the object. Run-time polymorphism in C++ is achieved using:
Virtual FunctionsRuntime polymorphism in C++ is implemented using virtual functions. A virtual function allows a derived class to override a function of the base class, and the appropriate function is selected during program execution.
Example: A base class Shape defines a virtual draw() function, while derived classes such as Circle and Rectangle provide their own implementations. Relationships Between Classes in C++Classes in C++ can be related to one another in different ways to model real-world relationships. These relationships describe how objects interact and whether one object owns another.
AssociationAssociation is a relationship between two or more classes where their objects interact with each other while remaining independent. It represents a "uses-a" relationship between objects.
Example: A Teacher teaches Students, but both can exist independently. Types of AssociationAssociation in C++ is commonly classified into the following types. Aggregation (Weak Association) Aggregation represents a "has-a" relationship where one class contains or refers to another class, but both objects have independent lifetimes.
Example: A Company has Employees, but employees can continue to exist even if the company no longer exists. ![]() Composition (Strong Association) Composition is a stronger form of association in which one class owns another class. The lifetime of the child object depends on the parent object.
Example: A House is composed of Rooms. If the house is destroyed, the rooms are also destroyed. ![]() Advantages of OOP in C++Object-Oriented Programming (OOP) offers several advantages over procedural programming by organizing programs around objects and classes.
Limitations of OOP in C++Although OOP provides many benefits, it also has some limitations.
Comment ExploreStandard Template Library(STL) Practice & Problems |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |