- A Brief Intro To C++
- The Timeline Of C++
- Importance Of C++
- Versions Of C++ Language
- Comparison With Other Popular Programming Languages
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Variables In C++?
- Declaration & Definition Of Variables In C++
- Variable Initialization In C++
- Rules & Regulations For Naming Variables In C++ Language
- Different Types Of Variables In C++
- Different Types of Variable Initialization In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Primitive Data Types In C++?
- Derived Data Types In C++
- User-Defined Data Types In C++
- Abstract Data Types In C++
- Data Type Modifiers In C++
- Declaring Variables With Auto Keyword
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Structure Of C++ Program: Components
- Compilation & Execution Of C++ Programs | Step-by-Step Explanation
- Structure Of C++ Program With Example
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What is Typedef in C++?
- The Role & Applications of Typedef in C++
- Basic Syntax for typedef in C++
- How Does typedef Work in C++?
- How to Use Typedef in C++ With Examples? (Multiple Data Types)
- The Difference Between #define & Typedef in C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Strings In C++?
- Types Of Strings In C++
- How To Declare & Initialize C-Style Strings In C++ Programs?
- How To Declare & Initialize Strings In C++ Using String Keyword?
- List Of String Functions In C++
- Operations On Strings Using String Functions In C++
- Concatenation Of Strings In C++
- How To Convert Int To Strings In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is String Concatenation In C++?
- How To Concatenate Two Strings In C++ Using The ‘+' Operator?
- String Concatenation Using The strcat( ) Function
- Concatenation Of Two Strings In C++ Using Loops
- String Concatenation Using The append() Function
- C++ String Concatenation Using The Inheritance Of Class
- Concatenate Two Strings In C++ With The Friend and strcat() Functions
- Why Do We Need To Concatenate Two Strings?
- How To Reverse Concatenation Of Strings In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Find In String C++?
- What Is A Substring?
- How To Find A Substring In A String In C++?
- How To Find A Character In String C++?
- Find All Substrings From A Given String In C++
- Index Substring In String In C++ From A Specific Start To A Specific Length
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Pointers In C++?
- Pointer Declaration In C++
- How To Initialize And Use Pointers In C++?
- Different Types Of Pointers In C++
- References & Pointers In C++
- Arrays And Pointers In C++
- String Literals & Pointers In C++
- Pointers To Pointers In C++ (Double Pointers)
- Arithmetic Operation On Pointers In C++
- Advantages Of Pointers In C++
- Some Common Mistakes To Avoid With Pointers In Cpp
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Understanding Pointers In C++
- What Is Pointer To Object In C++?
- Declaration And Use Of Object Pointers In C++
- Advantages Of Pointer To Object In C++
- Pointer To Objects In C++ With Arrow Operator
- An Array Of Objects Using Pointers In C++
- Base Class Pointer For Derived Class Object In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is 'This' Pointer In C++?
- Defining ‘this’ Pointer In C++
- Example Of 'this' Pointer In C++
- Describing The Constness Of 'this' Pointer In C++
- Important Uses Of 'this' Pointer In C++
- Method Chaining Using 'this' Pointer In C++
- C++ Programs To Show Application Of 'This' Pointer
- How To Delete The ‘this’ Pointer In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What is Reference?
- What is Pointer?
- Comparison Table Of C++ Pointer Vs. Reference
- Differences Between Reference And Pointer: A Detailed Explanation
- Why Are References Less Powerful Than Pointers?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- How To Declare A 2D Array In C++?
- C++ Multi-Dimensional Arrays
- Ways To Initialize A 2D Array In C++
- Methods To Dynamically Allocate A 2D Array In C++
- Accessing/ Referencing Two-Dimensional Array Elements
- How To Initialize A Two-Dimensional Integer Array In C++?
- How To Initialize A Two-Dimensional Character Array?
- How To Enter Data In Two-Dimensional Array In C++?
- Conclusion
- Frequently Asked Questions
- What Are Arrays Of Strings In C++?
- Different Ways To Create String Arrays In C++
- How To Access The Elements Of A String Array In C++?
- How To Convert Char Array To String?
- Conclusion
- Frequently Asked Questions
- What is Memory Allocation in C++?
- The “new" Operator In C++
- The "delete" Operator In C++
- Dynamic Memory Allocation In C++ | Arrays
- Dynamic Memory Allocation In C++ | Objects
- Deallocation Of Dynamic Memory
- Dynamic Memory Allocation In C++ | Uses
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A Substring In C++ (Substr C++)?
- Example For Substr In C++
- Points To Remember For Substr In C++
- Important Applications Of substr() Function
- How to Get a Substring Before a Character?
- Print All Substrings Of A Given String
- Print Sum Of All Substrings Of A String Representing A Number
- Print Minimum Value Of All Substrings Of A String Representing A Number
- Print Maximum Value Of All Substrings Of A String Representing A Number
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Operator In C++?
- Types Of Operators In C++ With Examples
- What Are Arithmetic Operators In C++?
- What Are Assignment Operators In C++?
- What Are Relational Operators In C++?
- What Are Logical Operators In C++?
- What Are Bitwise Operators In C++?
- What Is Ternary/ Conditional Operator In C++?
- Miscellaneous Operators In C++
- Precedence & Associativity Of Operators In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is The New Operator In C++?
- Example To Understand New Operator In C++
- The Grammar Elements Of The New Operator In C++
- Storage Space Allocation
- How Does The C++ New Operator Works?
- What Happens When Enough Memory In The Program Is Not Available?
- Initializing Objects Allocated With New Operator In C++
- Lifetime Of Objects Allocated With The New Operator In C++
- What Is The Delete Operator In C++?
- Difference Between New And Delete Operator In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Types Of Overloading In C++
- What Is Operator Overloading In C++?
- How To Overload An Operator In C++?
- Overloadable & Non-overloadable Operators In C++
- Unary Operator Overloading In C++
- Binary Operator Overloading In C++
- Special Operator Overloading In C++
- Rules For Operator Overloading In C++
- Advantages And Disadvantages Of Operator Overloading In C++
- Function Overloading In C++
- What Is the Difference Between Operator Functions and Normal Functions?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Operators In C++?
- Introduction To Logical Operators In C++
- Types Of Logical Operators In C++ With Example Program
- Logical AND (&&) Operator In C++
- Logical NOT(!) Operator In C++
- Logical Operator Precedence And Associativity In C++
- Relation Between Conditional Statements And Logical Operators In C++
- C++ Relational Operators
- Conclusion
- Frequently Asked Important Interview Questions:
- Test Your Skills: Quiz Time
- Different Type Of C++ Bitwise Operators
- C++ Bitwise AND Operator
- C++ Bitwise OR Operator
- C++ Bitwise XOR Operator
- Bitwise Left Shift Operator In C++
- Bitwise Right Shift Operator In C++
- Bitwise NOT Operator
- What Is The Meaning Of Set Bit In C++?
- What Does Clear Bit Mean?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Types of Comments in C++
- Single Line Comment In C++
- Multi-Line Comment In C++
- How Do Compilers Process Comments In C++?
- C- Style Comments In C++
- How To Use Comment In C++ For Debugging Purposes?
- When To Use Comments While Writing Codes?
- Why Do We Use Comments In Codes?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Storage Classes In Cpp?
- What Is The Scope Of Variables?
- What Are Lifetime And Visibility Of Variables In C++?
- Types of Storage Classes in C++
- Automatic Storage Class In C++
- Register Storage Class In C++
- Static Storage Class In C++
- External Storage Class In C++
- Mutable Storage Class In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- Decision Making Statements In C++
- Types Of Conditional Statements In C++
- If-Else Statement In C++
- If-Else-If Ladder Statement In C++
- Nested If Statements In C++
- Alternatives To Conditional If-Else In C++
- Switch Case Statement In C++
- Jump Statements & If-Else In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A Switch Statement/ Switch Case In C++?
- Rules Of Switch Case In C++
- How Does Switch Case In C++ Work?
- The break Keyword In Switch Case C++
- The default Keyword In C++ Switch Case
- Switch Case Without Break And Default
- Advantages & Disadvantages of C++ Switch Case
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A For Loop In C++?
- Syntax Of For Loop In C++
- How Does A For Loop In C++ Work?
- Examples Of For Loop Program In C++
- Ranged Based For Loop In C++
- Nested For Loop In C++
- Infinite For Loop In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is A While Loop In C++?
- Parts Of The While Loop In C++
- C++ While Loop Program Example
- How Does A While Loop In C++ Work?
- What Is Pre-checking Process Or Entry-controlled Loop?
- When Are While Loops In C++ Useful?
- Example C++ While Loop Program
- What Are Nested While Loops In C++?
- Infinite While Loop In C++
- Alternatives To While Loop In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are Loops & Its Types In C++?
- What Is A Do-While Loop In C++?
- Do-While Loop Example In C++ To Print Numbers
- How Does A Do-While Loop In C++ Work?
- Various Components Of The Do-While Loop In C++
- Example 2: Adding User-Input Positive Numbers With Do-While Loop
- C++ Nested Do-While Loop
- C++ Infinitive Do-while Loop
- What is the Difference Between While Loop and Do While Loop in C++?
- When To Use A Do-While Loop?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Are 2D Vectors In C++?
- How To Declare 2D Vector In C++?
- How To Initialize 2D Vector In C++?
- C++ Program Examples For 2D Vectors
- How To Access & Modify 2D Vector Elements In C++?
- Methods To Traverse, Manipulate & Print 2D Vectors In C++
- Adding Elements To 2-D Vector Using push_back() Function
- Removing Elements From Vector In C++ Using pop_back() Function
- Creating 2D Vector In C++ With User Input For Size Of Column & Row
- Advantages of 2D Vectors Over Traditional Arrays
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- How To Print A Vector In C++ By Overloading Left Shift (<<) Operator?
- How To Print Vector In C++ Using Range-Based For-Loop?
- Print Vector In C++ With Comma Separator
- Printing Vector In C++ Using Indices (Square Brackets/ Double Brackets & at() Function)
- How To Print A Vector In C++ Using std::copy?
- How To Print A Vector In C++ Using for_each() Function?
- Printing C++ Vector Using The Lambda Function
- How To Print Vector In C++ Using Iterators?
- Conclusion
- Frequently Asked Questions
- Definition Of C++ Find In Vector
- Using The std::find() Function
- How Does find() In Vector C++ Function Work?
- Finding An Element By Custom Comparator Using std::find_if() Function
- Use std::find_if() With std::distance()
- Element Find In Vector C++ Using For Loop
- Using The find_if_not Function
- Find Elements With The Linear Search Approach
- Conclusion
- Frequently Asked Questions
- What Is Sort() Function In C++?
- Sort() Function In C++ From Standard Template Library
- Exceptions Of Sort() Function/ Algorithm In C++
- The Stable Sort() Function In C++
- Partial Sort() Function In C++
- Sorting In Ascending Order With Sort() Function In C++
- Sorting In Descending Order With Sort Function In C++
- Sorting In Desired Order With Custom Comparator Function & Sort Function In C++
- Sorting Elements In Desired Order Using Lambda Expression & Sort Function In C++
- Types of Sorting Algorithms In C++
- Advanced Sorting Algorithms In C++
- How Does the Sort() Function Algorithm Work In C++?
- Conclusion
- Frequently Asked Questions
- What Is Function Overloading In C++?
- Ways Of Function Overloading In C++
- Function Overloading In C++ Using Different Types Of Parameters
- Function Overloading In C++ With Different Number Of Parameters
- Function Overloading In C++ Using Different Sequence Of Parameters
- How Does Function Overloading In C++ Work?
- Rules Of Function Overloading In C++
- Why Is Function Overloading Used?
- Types Of Function Overloading Based On Time Of Resolution
- Causes Of Function Overloading In C++
- Ambiguity & Function Overloading In C++
- Advantages Of Function Overloading In C++
- Disadvantages Of Function Overloading In C++
- Operator Overloading In C++
- Function Overriding In C++
- Difference Between Function Overriding & Function Overloading In C++
- Conclusion
- Frequently Asked Questions
- What Is An Inline Function In C++?
- How To Define The Inline Function In C++?
- How Does Inline Function In C++ Work?
- The Need For An Inline Function In C++
- Can The Compiler Ignore/ Reject Inline Function In C++ Programs?
- Normal Function Vs. Inline Function In C++
- Classes & Inline Function In C++
- Understanding Inline, __inline, And __forceinline Functions In C++
- When To Use An Inline Function In C++?
- Advantages Of Inline Function In C++
- Disadvantages Of Inline Function In C++
- Why Not Use Macros Instead Of An Inline Function In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Static Data Member In C++?
- How To Declare Static Data Members In C++?
- How To Initialize/ Define Static Data Member In C++?
- Ways To Access A Static Data Member In C++
- What Are Static Member Functions In C++?
- Example Of Member Function & Static Data Member In C++
- Practical Applications Of Static Data Member In C++
- Conclusion
- Frequently Asked Questions
- What Is A Constant In C++?
- Ways To Define Constant In C++
- What Are Literals In C++?
- Pointer To A Constant In C++
- Constant Function Arguments In C++
- Constant Member Function Of Class In C++
- Constant Data Members In C++
- Object Constant In C++
- Conclusion
- Frequently Asked Questions(FAQ)
- What Is Friend Function In C++?
- Declaration Of Friend Function In C++ With Example
- Characteristics Of Friend Function In C++
- Global Friend Function In C++ (Global Function As Friend Function )
- Member Function Of Another Class As Friend Function In C++
- Function Overloading Using Friend Function In C++
- Advantages & Disadvantages Of Friend Function in C++
- What Is A C++ Friend Class?
- A Function Friendly To Multiple Classes
- C++ Friend Class Vs. Friend Function In C++
- Some Important Points About Friend Functions And Classes In C++
- Conclusion
- Frequently Asked Questions
- What Is Function Overriding In C++?
- The Working Mechanism Of Function Overriding In C++
- Real-Life Example Of Function Overriding In C++
- Accessing Overriding Function In C++
- Accessing Overridden Function In C++
- Function Call Binding With Class Objects | Function Overriding In C++
- Function Call Binding With Base Class Pointers | Function Overriding In C++
- Advantages Of Function Overriding In C++
- Variations In Function Overriding In C++
- Function Overloading In C++
- Function Overloading Vs Function Overriding In C++
- Conclusion
- Frequently Asked Questions
- Errors In C++
- What Is Exception Handling In C++?
- Exception Handling In C++ Program Example
- C++ Exception Handling: Basic Keywords
- The Need For C++ Exception Handling
- C++ Standard Exceptions
- C++ Exception Classes
- User-Defined Exceptions In C++
- Advantages & Disadvantages Of C++ Exception Handling
- Conclusion
- Frequently Asked Questions
- What Are Templates In C++ & How Do They Work?
- Types Of Templates In C++
- What Are Function Templates In C++?
- C++ Template Functions With Multiple Parameters
- C++ Template Function Overloading
- What Are Class Templates In C++?
- Defining A Class Member Outside C++ Template Class
- C++ Template Class With Multiple Parameters
- What Is C++ Template Specialization?
- How To Specify Default Arguments For Templates In C++?
- Advantages Of C++ Templates
- Disadvantages Of C++ Templates
- Difference Between Function Overloading And Templates In C++
- Conclusion
- Frequently Asked Questions
- Structure
- Structure Declaration
- Initialization of Structure
- Copying and Comparing Structures
- Array of Structures
- Nested Structures
- Pointer to a Structure
- Structure as Function Argument
- Self Referential Structures
- Class
- Object Declaration
- Accessing Class Members
- Similarities between Structure and Class
- Which One Should You Choose?
- Key Difference Between a Structure and Class
- Summing Up
- Test Your Skills: Quiz Time
- What Is A Class And Object In C++?
- What Is An Object In C++?
- How To Create A Class & Object In C++? With Example
- Access Modifiers & Class/ Object In C++
- Member Functions Of A Class In C++
- How To Access Data Members And Member Functions?
- Significance Of Class & Object In C++
- What Are Constructors In C++ & Its Types?
- What Is A Destructor Of Class In C++?
- An Array Of Objects In C++
- Object In C++ As Function Arguments
- The this (->) Pointer & Classes In C++
- The Need For Semicolons At The End Of A Class In C++
- Difference Between Structure & Class In C++
- Conclusion
- Frequently Asked Questions
- What Are Static Members In C++?
- Static Member Functions in C++
- Ways To Call Static Member Function In C++
- Properties Of Static Member Function In C++
- Need Of Static Member Functions In C++
- Regular Member Function Vs. Static Member Function In C++
- Limitations Of Static Member Functions In C++
- Conclusion
- Frequently Asked Questions
- What Is Constructor In C++?
- Characteristics Of A Constructor In C++
- Types Of Constructors In C++
- Default Constructor In C++
- Parameterized Constructor In C++
- Copy Constructor In C++
- Dynamic Constructor In C++
- Benefits Of Using Constructor In C++
- How Does Constructor In C++ Differ From Normal Member Function?
- Constructor Overloading In C++
- Constructor For Array Of Objects In C++
- Constructor In C++ With Default Arguments
- Initializer List For Constructor In C++
- Dynamic Initialization Using Constructor In C++
- Conclusion
- Frequently Asked Questions
- What Is A Constructor In C++?
- What Is Constructor Overloading In C++?
- Dеclaration Of Constructor Ovеrloading In C++
- Condition For Constructor Overloading In C++
- How Constructor Ovеrloading In C++ Works?
- Examples Of Constructor Overloading In C++
- Lеgal & Illеgal Constructor Ovеrloading In C++
- Types Of Constructors In C++
- Characteristics Of Constructors In C++
- Advantage Of Constructor Overloading In C++
- Disadvantage Of Constructor Overloading In C++
- Conclusion
- Frеquеntly Askеd Quеstions
- What Is A Destructor In C++?
- Rules For Defining A Destructor In C++
- When Is A Destructor in C++ Called?
- Order Of Destruction In C++
- Default Destructor & User-Defined Destructor In C++
- Virtual Destructor In C++
- Pure Virtual Destructor In C++
- Key Properties Of Destructor In C++ You Must Know
- Explicit Destructor Calls In C++
- Destructor Overloading In C++
- Difference Between Normal Member Function & Destructor In C++
- Important Uses Of Destructor In C++
- Conclusion
- Frequently Asked Questions
- What Is A Constructor In C++?
- What Is A Destructor In C++?
- Difference Between Constructor And Destructor In C++
- Constructor In C++ | A Brief Explanation
- Destructor In C++ | A Brief Explanation
- Difference Between Constructor And Destructor In C++ Explained
- Order Of Calling Constructor And Destructor In C++ Classes
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
- What Is Type Conversion In C++?
- What Is Type Casting In C++?
- Types Of Type Conversion In C++
- Implicit Type Conversion (Coercion) In C++
- Explicit Type Conversion (Casting) In C++
- Advantages Of Type Conversion In C++
- Disadvantages Of Type Conversion In C++
- Difference Between Type Casting & Type Conversion In C++
- Application Of Type Casting In C++
- Conclusion
- Frequently Asked Questions
- What Is A Copy Constructor In C++?
- Characteristics Of Copy Constructors In C++
- Types Of Copy Constructors In C++
- When Do We Call The Copy Constructor In C++?
- When Is A User-Defined Copy Constructor Needed In C++?
- Types Of Constructor Copies In C++
- Can We Make The Copy Constructor In C++ Private?
- Assignment Operator Vs Copy Constructor In C++
- Example Of Class Where A Copy Constructor Is Essential
- Uses Of Copy Constructors In C++
- Conclusion
- Frequently Asked Questions
- Why Do You Need Object-Oriented Programming (OOP) In C++?
- OOPs Concepts In C++ With Examples
- The Class OOPs Concept In C++
- The Object OOPs Concept In C++
- The Inheritance OOPs Concept In C++
- Polymorphism OOPs Concept In C++
- Abstraction OOPs Concept In C++
- Encapsulation OOPs Concept In C++
- Other Features Of OOPs In C++
- Benefits Of OOP In C++ Over Procedural-Oriented Programming
- Disadvantages Of OOPS Concept In C++
- Why Is C++ A Partial OOP Language?
- Conclusion
- Frequently Asked Questions
- Introduction To Abstraction In C++
- Types Of Abstraction In C++
- What Is Data Abstraction In C++?
- Understanding Data Abstraction In C++ Using Real Life Example
- Ways Of Achieving Data Abstraction In C++
- What Is An Abstract Class?
- Advantages Of Data Abstraction In C++
- Use Cases Of Data Abstraction In C++
- Encapsulation Vs. Abstraction In C++
- Conclusion
- Frequently Asked Questions
- What Is Encapsulation In C++?
- How Does Encapsulation Work In C++?
- Types Of Encapsulation In C++
- Why Do We Need Encapsulation In C++?
- Implementation Of Encapsulation In C++
- Access Specifiers & Encapsulation In C++
- Role Of Access Specifiers In Encapsulation In C++
- Member Functions & Encapsulation In C++
- Data Hiding & Encapsulation In C++
- Features Of Encapsulation In C++
- Advantages & Disadvantages Of Encapsulation In C++
- Difference Between Abstraction and Encapsulation In C++
- Conclusion
- Frequently Asked Questions
- What Is Inheritance In C++?
- What Are Child And Parent Classes?
- Syntax And Structure Of Inheritance In C++
- Implementing Inheritance In C++
- Importance Of Inheritance In C++
- Types Of Inheritance In C++
- Visibility Modes Of Inheritance In C++
- Access Modifiers & Inheritance In C++
- How To Make A Private Member Inheritable?
- Member Function Overriding In Inheritance In C++
- The Diamond Problem | Inheritance In C++ & Ambiguity
- Ways To Avoid Ambiguity Inheritance In C++
- Why & When To Use Inheritance In C++?
- Advantages Of Inheritance In C++
- The Disadvantages Of Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Hybrid Inheritance In C++?
- Importance Of Hybrid Inheritance In Object Oriented Programming
- Example Of Hybrid Inheritance In C++: Using Single and Multiple Inheritance
- Example Of Hybrid Inheritance In C++: Using Multilevel and Hierarchical Inheritance
- Real-World Applications Of Hybrid Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Multiple Inheritance In C++?
- Examples Of Multiple Inheritance In C++
- Ambiguity Problem In Multiple Inheritance In C++
- Ambiguity Resolution In Multiple Inheritance In C++
- The Diamond Problem In Multiple Inheritance In C++
- Visibility Modes In Multiple Inheritance In C++
- Advantages & Disadvantages Of Multiple Inheritance In C++
- Multiple Inheritance Vs. Multilevel Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Multilevel Inheritance In C++?
- Block Diagram For Multilevel Inheritance In C++
- Multilevel Inheritance In C++ Example
- Constructor & Multilevel Inheritance In C++
- Use Cases Of Multilevel Inheritance In C++
- Multiple Vs Multilevel Inheritance In C++
- Advantages & Disadvantages Of Multilevel Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Is Hierarchical Inheritance In C++?
- Example 1: Hierarchical Inheritance In C++
- Example 2: Hierarchical Inheritance In C++
- Impact of Visibility Modes In Hierarchical Inheritance In C++
- Advantages And Disadvantages Of Hierarchical Inheritance In C++
- Use Cases Of Hierarchical Inheritance In C++
- Conclusion
- Frequently Asked Questions
- What Are Access Specifiers In C++?
- Types Of Access Specifiers In C++
- Public Access Specifiers In C++
- Private Access Specifier In C++
- Protected Access Specifier In C++
- The Need For Access Specifiers In C++
- Combined Example For All Access Specifiers In C++
- Best Practices For Using Access Specifiers In C++
- Why Can't Private Members Be Accessed From Outside A Class?
- Conclusion
- Frequently Asked Questions
- What Is The Diamond Problem In C++?
- Example Of The Diamond Problem In C++
- Resolution Of The Diamond Problem In C++
- Virtual Inheritance To Resolve Diamond Problem In C++
- Scope Resolution Operator To Resolve Diamond Problem In C++
- Conclusion
- Frequently Asked Questions
OOPs Concept In C++ | A Detailed Guide With Codes & Explanations

Object-oriented programming (OOP) is a fundamental paradigm in computer science that enables developers to design and structure their code more efficiently and intuitively. C++, a powerful and versatile programming language, is known for its strong support of OOP concepts. In a nutshell, the OOPs concept in C++ enables you to programmatically design an environment where objects interact just like they would in the actual world. It's a way of writing software that reflects the natural way of understanding and describing things and actions.
In this article, we'll explore the key OOP concepts in C++ and understand how they enhance code organization, code readability, code reusability, and code maintainability.
Why Do You Need Object-Oriented Programming (OOP) In C++?
In software development, object-oriented programming (OOP) is a vital paradigm utilized extensively, given the number of advantages. Here are a few reasons why OOP in C++ programming is a crucial tool for creating flexible programs and complex applications.
Code Reuse: One of the main benefits of using object-oriented features in programming is its ability to encourage code reuse.
- Developers can utilize the OOPs concept in C++ to build reusable classes and objects that can be applied to several projects.
- This reusability further promotes the ability to develop code more quickly and with less code duplication, i.e., efficient development.
For example: Think of a software development company that creates numerous applications. Using the class OOPs concept in C++ (and other languages), they can develop a library of classes to carry out common/ repetitive tasks like managing user authentication or database connectivity. They can then reuse these classes in numerous projects, saving a lot of time and effort.
Code Organization: The OOPS concept in C++ offers a systematic method of arranging code. Since classes and objects reflect actual entities and their relationships, the codebase is easier to understand and manage even as projects get bigger and more complicated.
- For example, the OOPS concept in C++ is used in modern video games where game elements like characters, weapons, locations, etc., are modelled in the form of objects.
- This strategy encourages collaborative creation, improves code organization, and makes code maintenance simple.
There are many other benefits of OOP in C++ that make it essential for creating modern software.
- It encourages modularity, makes code administration easier, and encourages code reuse.
- Inheritance, polymorphism, encapsulation and other OOPS concepts in C++ are the tools programmers need to build adaptable, scalable, and maintainable software systems.
- Programmers can effectively solve complicated problems and create reliable applications for a variety of industries, such as gaming, banking, healthcare, etc.
OOPs Concepts In C++ With Examples
Objects and classes are the building blocks for object-oriented programming that help implement real-world entities in programming languages. The five main pillars of OOPs in C++ are:
- Class and object
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
In the sections ahead, we will learn about these and a few other important concepts of OOP in C++ programming.
The Class OOPs Concept In C++
A class can be referred to as a collection of objects. It is the key idea behind the OOPS concept in C++ that enables you to create various user-defined data types.
- A class acts as a blueprint for objects by specifying how these objects should be constructed and how they should behave.
- A class encapsulates data members (attributes or properties) and member functions (methods or operations) that manipulate the data.
In other words, one of the primary benefits of classes is that they act like a template for building objects, where objects are instances of that class. Each object has a unique set of data members and is capable of carrying out operations.
Let's understand this OOPs concept in C++ with a real-life analogy-
Take a Student class as a template for the representation of real students who are its objects. The attributes would include name, age, and major. Methods like get_gpa and enroll_in_course may be used to enable instances of the class to enrol in courses and check their GPAs, respectively.
This class collects information on the activities of students, encouraging well-organized and reusable code in educational systems. Here, we can establish a single class containing common characteristics rather than defining a new class for each type of student. This way, classes act as user-defined data types for creating objects with similar features.
Syntax to Declare a Class in C++
Class className{
Access_specifier
Data members
Member functions
}
Here,
- Class: It is the keyword to define the class in C++
- Access_specifier: An access controller is used to control the visibility of the class data, which can be private, public, and protected. We will discuss these in a section later.
- Data members: These are variables that hold the data related to the object of the class
- Member functions: These are methods that operate on data members and specify specific behaviour of the object. (They are defined just like regular functions in C++, but since they are a part of a specific class and work on that class's objects, they are referred to as member functions.)
Code Example:
#include <iostream>
using namespace std;
class Student {
public:
// Class data members or attributes
string name;
int age;
string major;
// Class member function or methods
void enroll_in_course(string course) {
cout << name << " has enrolled in " << course << endl;}
void get_gpa() {
cout << name << "'s GPA is 3.8" << endl;}
};
int main() {
// Creating an object of the class
Student aayush;
// Assigning values to object's attributes
aayush.name = "Aayush";
aayush.age = 20;
aayush.major = "Computer Science";
// Using methods on the object
aayush.enroll_in_course("Programming 101");
aayush.get_gpa();
return 0;
}
Output:
Aayush has enrolled in Programming 101
Aayush's GPA is 3.8
Explanation:
- We begin this example by including the <iostream> library for input/output operations and the namespace std directive to avoid needing std:: prefixes.
- Then, we declare a class named Student, containing 3 data members (i.e., attributes), name, age, and major, which are of data type string, int, and string, respectively.
- As mentioned in code comments, we also define two member functions (or methods) in the class, i.e., enroll_in_course() and get_gpa(), both of which print attributes of an object using the cout command.
- In the main() function, we create an object of the Student class, called aayush, representing a student entity.
- Next, we assign values to the attributes of the aayush object using the dot operator, initializing name with Aayush and age with 20.
- After that, we call the enroll_in_course() method on the object aayush with the argument (string value)- "Programming 101", resulting in a message indicating the enrollment.
- Similarly, we call the get_gpa() method, thus printing a message about the GPA.
- Finally, the main() function terminates with a return 0 statement indicating successful execution without any errors.
NOTE- Here, the class defines the structure and behaviour for creating student objects. It bundles related data and actions together. In the main function, we created the object of the Student class and assigned values to it. Then, we invoked the methods of the class using an object and printed to the console.
The Object OOPs Concept In C++
The concept of object is a core idea in object-oriented programming (OOP), which is a self-contained element with certain attributes and behaviours. It is an instance of a class that serves as a model or template for building multiple objects of similar nature.
In other words, an object combines data (attributes) and the functions that operate on that data into a single unit. These functions, often called methods, will allow the object to perform actions and manipulate its data. This bundling of data and methods within an object in large software systems supports code reuse and simpler maintenance.
Let's understand this OOPs concept in C++ with a real-life analogy-
Consider creating a class called BankAccount to represent a bank account. Each bank account will have attributes like an account number, account holder name, and balance. It will also have behaviours like depositing and withdrawing money. This clarifies the data/ attribute elements and methods of an object (bank account).
Syntax to Define Object in C++
Class_name Object_name;
Here,
- Class_name: It is the name of the class for which the object is created
- Object_name: It is the name of the object, which should be meaningful and enhance the code's readability.
Code Example:
#include <iostream>
#include <string>
class BankAccount {
private:
int accountNumber;
std::string accountHolderName;
double balance;
public:
// Constructor to initialize the account
BankAccount(int number, const std::string& name, double initialBalance) {
accountNumber = number;
accountHolderName = name;
balance = initialBalance;
}
// Member function to deposit money
void deposit(double amount) {
if (amount > 0) {
balance += amount;
std::cout << "Deposited INR" << amount << " into the account. New balance: INR" << balance << std::endl;
} else {
std::cout << "Invalid deposit amount." << std::endl;
}
}
// Member function to withdraw money
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
std::cout << "Withdrawn INR" << amount << " from the account. New balance: INR" << balance << std::endl;
} else {
std::cout << "Invalid withdrawal amount or insufficient balance." << std::endl;
}
}
// Member function to check the balance
double getBalance() const {
return balance;
}
};
int main() {
// Create two bank accounts
BankAccount account1(1001, "Virat Kohli", 1000.0);
BankAccount account2(1002, "Rohit Sharma", 500.0);
// Deposit and withdraw money from account1
account1.deposit(500.0);
account1.withdraw(200.0);
// Deposit and withdraw money from account2
account2.deposit(100.0);
account2.withdraw(700.0);
// Check balances
std::cout << "Account 1 balance: INR" << account1.getBalance() << std::endl;
std::cout << "Account 2 balance: INR" << account2.getBalance() << std::endl;
return 0;
}
Output:
Deposited INR500 into the account. New balance: INR1500
Withdrawn INR200 from the account. New balance: INR1300
Deposited INR100 into the account. New balance: INR600
Invalid withdrawal amount or insufficient balance.
Account 1 balance: INR1300
Account 2 balance: INR600
Explanation:
- In the C++ program above, we define a class called BankAccount to represent a bank account.
- This class has three data members: the accountNumber (int type), accountHolderName (string type), and balance (double type), which represent different attributes of the account.
- The constructor BankAccount is defined to initialize the account when an object is created. It takes initial values for account number, account holder name, and balance as parameters.
- There are two member functions with void return type, i.e., deposit() and withdraw() allow us to interact with the bank account by depositing and withdrawing money.
- These functions use an if-else statement to perform validation checks on account balance and the addition/ subtraction assignment operator to add or subtract amounts, respectively.
- Lastly, we have a getBalance() member function that allows us to retrieve the balance of the account.
- In the main() function, we create two BankAccount objects, account1 and account2, representing two bank accounts for Virat Kohli and Rohit Sharma, respectively.
- We also provide the account number and the account balance for these two accounts.
- Next, we deposit and withdraw money from both accounts by calling deposit() and withdraw() functions on each account separately.
- After that, we call the getBalance() function on both the accounts individually, inside a cout command to access and print the balances.
Read More: Class & Object In C++ | All Related Concepts Explained (With Examples)
The Inheritance OOPs Concept In C++
Inheritance is another fundamental OOPS concept in C++ that allows users to create a new class (called a derived or child class) that inherits properties and behaviours from an existing class (called a base or parent class). Inheritance creates a hierarchical link between classes, facilitating code reuse, extensibility, and the organizing of related classes. It's just like a parent-child relationship in real life.
Let's understand this OOPs concept in C++ with a real-life analogy-
Consider creating a student management system for a university. There are various kinds of students, including postgraduate and undergraduate students. Both types of students have different characteristics and habits, but they also have some common attributes like name, age, and major.
In such a case, say we have a base class that consists of all common characteristics these students have. Then, a derived class (inherited) can consist of all unique characteristics based on student categories like researchTopic, etc.
Syntax:
class BaseClass {
// Base class members
};
class DerivedClass : access_modifier BaseClass {
// Derived class members
};
Here,
- Class: It is a keyword used to define a class.
- BaseClass: It is the name of the base class or parent class
- DerivedClass: It is the name of the class that is derived or inherited from the parent class.
- access_modifier: It is used to represent the inheritance relationship, which could be public, protected, or private.
Let's look at a sample C++ program to better understand this concept.
Code Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
string major;
void enroll_in_course(string course) {
cout << name << " has enrolled in " << course << endl;}
void get_gpa() {
cout << name << "'s GPA is 3.8" << endl;}
};
class GraduateStudent : public Student {
public:
string researchTopic;
void set_research_topic(string topic) {
researchTopic = topic;}
void present_research() {
cout << name << " is presenting research on " << researchTopic << endl;}
};
int main() {
GraduateStudent sana;
sana.name = "Sana";
sana.age = 25;
sana.major = "Computer Science";
sana.researchTopic = "Machine Learning";
sana.enroll_in_course("Advanced Algorithms");
sana.get_gpa();
sana.present_research();
return 0;
}
Output:
Sana has enrolled in Advanced Algorithms
Sana's GPA is 3.8
Sana is presenting research on Machine Learning
Explanation:
In the code above, we define a base class called Student, with three public data members- name (string), age (integer), and major (string).
- The class also contains two public member functions. An enroll_in_course() method that uses the cout command to print a string message indicating the course a student has enrolled in. And a get_gpa() function, which prints a message indicating the student's GPA (hardcoded as 3.8).
- Next, we define a derived class, GraduateStudent, using the public inheritance from the Student class. This means that GraduateStudent inherits all the member variables and member functions of the Student class.
- The derived class with an additional data member researchTopic, which represents the topic of research for a graduate student.
- It also contains two additional member functions, i.e., set_research_topic(), which initiates the research topic for the graduate student. And present_research(), which uses the cout to print a message indicating that the graduate student is presenting research on a specific topic.
- In the main() function, we create an instance of the GraduateStudent class called sana.
- After that, we use the dot operator to initialize the variable data members of the object. We assign values Sana, 25, Computer Science and Machine Learning to the name, age, major, and researchTopic attributes of the object.
- We then use the dot operator and the object sana to call the enroll_in_course() method, indicating that Sana is enrolling in the "Advanced Algorithms" course.
- Similarly, we call the get_gpa() and the present_research() methods to print the values to the console.
Important Note: There are multiple subtypes in inheritance itself, which are defined based on the relationship between and the number of parent and child classes. This includes single inheritance, multi-level inheritance, multiple inheritance, hybrid inheritance, and hierarchical inheritance.
Read More: Inheritance In C++ & Its 5 Types Explained With Multiple Examples
Polymorphism OOPs Concept In C++
As the name suggests, Polymorphism (poly=many morphism=forms) is the ability of a variable or a function to exist in multiple forms. Polymorphism allows the programmer to perform different tasks using the same variable or function. It allows a member function of a class to behave differently based on the object that is used to call it.
Let's understand this OOPs concept in C++ with a real-life analogy-
An example of polymorphism could be human behaviour. Humans typically behave differently with different persons. For example, a person who acts informally with his friends can act formally with his superiors and colleagues. As a result, the person exhibits different behaviour depending on the situation.
There are two types of this OOPs concept in C++ and in programming generally. They are compile-time polymorphism and run-time polymorphism, both of which we will discuss in detail here.
Compile-Time Polymorphism | OOPs Concept In C++
Compilation-time polymorphism is also sometimes referred to as static polymorphism or overloading. In this form of polymorphism, the decision to implement a certain function, behaviour, manipulation operation on objects or variables is made by the compiler at the time of compilation of the C++ code.
Overloading | OOPs Concept In C++
The concept of overloading refers to the practice of giving the same name to multiple objects, such as operators or functions, that behave differently depending on the situation. For example, in function overloading the compiler decides which function to call depending on the function's signature (i.e., the quantity and nature of its parameters) at compile time. The appropriate function is chosen based on the context in which it is called, allowing numerous functions with the same name but with distinct parameter lists to coexist within a class.
There are two types of overloading in C++, which are described in the sections below.
Operator Overloading | OOPs Concept In C++
The way operators behave with primitive data types is already defined. But what about user-defined/non-primitive data types? Well, we can define how common operators (+, -, *, /, etc.) behave or interact with interact user-defined data types and thus alter their behaviour. This ability is known as operator overloading. In reference to classes, you can provide specific actions for these operators when they are applied to objects of your own classes by overloading the operators.
Code Example:
#include <iostream>
using namespace::std;
class Vector {
public:
Vector(double x, double y) : x(x), y(y) {}
Vector operator+(const Vector& other) const {
return Vector(x + other.x, y + other.y);
}
void display() const {
cout << "Vector(" << x << ", " << y << ")" << endl;}
private:
double x;
double y;
};
int main() {
Vector v1(2.0, 3.0);
Vector v2(1.5, 2.5);
Vector sum = v1 + v2; // This uses the overloaded + operator
v1.display();
v2.display();
sum.display();
return 0;
}
Output:
Vector(2, 3)
Vector(1.5, 2.5)
Vector(3.5, 5.5)
Explanation:
- We define a class Vector to represent two-dimensional vectors with x and y components.
- The class has a constructor of the same name that initializes these double-type elements.
- Next, we define an operator+() function, which takes a constant reference to vectors as an argument. It overloads the addition arithmetic operator by defining how to add two Vector objects together. The function returns a new Vector object with the sum of its components.
- We then define the display() method to print the x and y components of a vector using the cout command.
- In the main() function, we create two Vector objects, v1 and v2 and initialize the component values for x and y.
- We then call the operator+() overloaded function to add these vectors together and store the result in the sum variable.
- Finally, we call the display() function to print the components of v1, v2, and sum.
Read More: Operator Overloading In C++ And Related Concepts (With Examples)
Function Overloading | OOPs Concept In C++
Function overloading, as mentioned before, allows you to define multiple functions with the same name in the same scope but different input parameters, return type, or no of parameters. These help the compiler differentiate between similar functions. This compile-time polymorphism provides us with a way to create functions that perform similar operations. You can use function overloading to make your code more expressive and user-friendly by using the same function name for various behaviours.
Syntax:
return_type function_name(parameters1) {
// Code for the first version of the function
}
return_type function_name(parameters2) {
// Code for the second version of the function
}
Here,
- return_type: It is the data type of the function's return value.
- function_name: It is the name of the function that is being overloaded.
- parameters1: It denotes a list of arguments accepted by the overloaded function.
Let's understand this OOPs concept in C++ with a real-life analogy:
Consider a situation where a student named Amir is interested in displaying his academic background. We can use compile-time polymorphism to provide various information display alternatives using the same method name to satisfy Amir's requirement.
His curiosity is satisfied using two versions of the display method provided by the Student class. The first version presents his basic details like his name, age, and major. The second version, however, takes things a step further by showing his grade point average (GPA), which is a measure of his academic ability, in addition to the same fundamental information.
Code Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
string major;
// Method for displaying student information (overloaded)
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Major: " << major << endl;
}
// Overloaded display method with an additional parameter
void display(bool includeGPA) {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Major: " << major << endl;
if (includeGPA) {
cout << "GPA: 3.8" << endl;}
}
};
int main() {
Student amir;
amir.name = "Amir";
amir.age = 20;
amir.major = "Computer Science";
cout << "Displaying student information:" << endl;
amir.display();
cout << "\nDisplaying student information with GPA:" << endl;
amir.display(true);
return 0;
}
Output:
Displaying student information:
Name: Amir
Age: 20
Major: Computer ScienceDisplaying student information with GPA:
Name: Amir
Age: 20
Major: Computer Science
GPA: 3.8
Read More: Function Overloading In C++ With Code Examples & Explanation
Run-Time Polymorphism | OOPs Concept In C++
Run-time polymorphism, as the name suggests, is the kind of polymorphism that occurs at run time rather than compile time. Virtual functions and method overriding are used to implement this kind of polymorphism.
- That is, a derived class's ability to provide a particular implementation of a function already specified in its base class is known as method overriding.
- Late binding is made possible via virtual functions, which means that the function call is determined at runtime based on the type of the real object.
- This allows a base class pointer to call the appropriate function in a derived class, even if the pointer is pointing to an object of the child class.
Let's understand this OOPs concept in C++ with a real-life analogy:
Think about a situation where there is a base class called Shape, and classes that are derived from it are Circle and Rectangle.
- Every one of these classes shares a method, say draw(), which prints a message to the effect of the shape being drawn.
- Runtime polymorphism can be seen when the draw() method is called on an instance of a derived class, and the appropriate implementation of the method for that particular derived class is used.
This flexibility enables you to interact with a variety of forms using a standardized interface while taking advantage of the unique characteristics of each shape type.
Function Overriding | OOPs Concept In C++
It is one of the features in object-oriented programming (OOP) wherein a derived class redefines a function of the base class with the same name and signature but a different implementation. Function overriding in C++ facilitates runtime polymorphism by resolving the function call while a program is running. Late binding and dynamic polymorphism are other names for runtime polymorphism.
Syntax:
public class Base{
access_modifier:
// overridden function
return_type function_name(){}
};
}
public class Derived: public Base {
access_modifier:
// overriding function
return_type function_name(){}
};
}
Here,
- Base and Derived refer to the names of the parent and the child class, respectively.
- access_modifier represents the scope of class members, which could be public, protected, or private.
Note that the functions in both classes should have the same signature or function prototype.
Code Example:
#include <bits/stdc++.h>
using namespace std;
class Shape {
public:
void draw() {
cout << "Drawing a shape" << endl;}
};
class Circle: public Shape {
public:
void draw() {
cout << "Drawing a circle" << endl;}
};
class Rectangle: public Shape {
public:
void draw() {
cout<< "Drawing a rectangle" << endl;}
};
int main() {
Shape* shapePtr = new Shape();
shapePtr->draw();
Circle* circlePtr = new Circle();
circlePtr->draw();
Rectangle* rectanglePtr = new Rectangle();
rectanglePtr->draw();
delete shapePtr;
delete circlePtr;
delete rectanglePtr;
return 0;
}
Output:
Drawing a shape
Drawing a circle
Drawing a rectangle
Read More: Function Overriding In C++ | Examples, Working Mechanism & More!
Abstraction OOPs Concept In C++
Abstraction is the process of hiding certain data from users and showing only the required information to them. In other words, this OOPs concept in C++ refers to the process of creating simplified models of real-world entities by capturing their essential attributes and behaviours while hiding the complex implementation details. Abstraction makes it easier for the user to use the software as complex code is hidden from them and does not interfere with the interface/experience.
Let's understand this OOPs concept in C++ with a real-life analogy:
Let's consider a scientific calculator (or even a regular calculator) as an example. Whenever we enter data and perform tasks like addition, subtraction, multiplication, percentage, trigonometric functions, etc., the calculator provides us with quick results. All the mechanisms for how the actual calculations are done are hidden from our view as a user. This is one of the simplest real-life examples of abstraction.
Take another example. Say we want to develop software that determines the areas of various geometric objects, such as circles and squares. In this case, we can use abstraction to accomplish this without revealing internal calculations. We start by creating an abstract base class, Shape, with an undefined calculateArea() method. Derived classes like Circle and Square inherit from Shape and provide specific area calculation implementations. By using base class references, instances of different shapes can be created and their areas calculated without directly interacting with their unique logic, promoting code flexibility and maintenance.
This serves as an example of how abstraction streamlines the management of various things under a single umbrella and helps simplify complex systems.
Code Example:
#include <iostream>
using namespace std;
// Abstract class representing a shape
class Shape {
public:
// Pure virtual function for calculating area
virtual double calculateArea() = 0;
};
// Concrete classes inheriting from Shape
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() override {
return 3.14159 * radius * radius;}
};
class Square : public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
double calculateArea() override {
return side * side;}
};
int main() {
Circle circle(5.0);
Square square(4.0);
Shape* shape1 = &circle;
Shape* shape2 = □
cout << "Circle Area: " << shape1->calculateArea() << endl;
cout << "Square Area: " << shape2->calculateArea() << endl;
return 0;
}
Output:
Circle Area: 78.5397
Square Area: 16
Explanation:
- In the C++ sample code above, we define an abstract class, Shape, which serves as the base class. It contains a pure virtual function calculateArea(), which makes the class abstract. This means that the function is meant to be overridden by derived classes.
- We then create two derived classes, Circle and Square, which inherit from the Shape class. Both classes provide their own implementations for the calculateArea() function.
- The Circle class calculates the area of a circle based on its radius, and the Square class calculates the area of a square based on its side length.
- In the main() function, we create an instance of both the classes, i.e., circle and square. They utilize their respective constructors to set the necessary attributes.
- We then create two pointers for the Shape class (shape1 and shape2) and pass the instances of the derived class as a reference, using the address-of operator (&).
- Essentially, these objects are treated as pointers to the base class Shape, showcasing the concept of abstraction. This allows us to create an array of different shapes without dealing with their specific implementations.
- Next, we call the calculateArea() method on the base class pointers shape1 and shape2, using the arrow/ 'this' pointer, and print the outcomes using the cout command.
- As is evident from the output, appropriate implementations in the derived classes are invoked.
Read More: Data Abstraction In C++ | Types, Use-Cases & More (With Examples)
Encapsulation OOPs Concept In C++
Encapsulation is another fundamental OOPS concept in C++, which entails grouping data (attributes) and the methods (functions) that operate on the data into a single entity, frequently referred to as a class. Encapsulation limits direct access to certain of the object's parts, enabling management and data security.
Let's understand this OOPs concept in C++ with a real-life analogy-
Take a scenario where we need to develop a program for controlling access and guaranteeing data security while handling bank accounts. Here, we create a BankAccount class that has private attributes for the delicate account information, such as account number and amount. The class offers well-constructed methods that act as entry points to interact with the account's data, including deposit, withdraw, and getBalance.
We establish data integrity and security by encapsulating the account data and enforcing actions through methods. The methods provide a trustworthy means to access the account's balance while validating deposits and withdrawals, preventing direct manipulation of properties and other features.
Code Example:
#include <iostream>
using namespace std;
class BankAccount {
private:
string accountNumber;
double balance;
public:
// Constructor to initialize account details
BankAccount(string accNum, double initialBalance) : accountNumber(accNum), balance(initialBalance) {}
// Method to deposit money
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposit of INR " << amount << " successful. New balance: INR " << balance << endl;}
}
// Method to withdraw money
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrawal of INR " << amount << " successful. New balance: INR " << balance << endl;
} else {
cout << "Insufficient funds or invalid amount." << endl;}
}
// Method to get current balance
double getBalance() {
return balance;}
};
int main() {
BankAccount account("12345", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.withdraw(1500.0); // Invalid withdrawal
cout << "Current balance: INR " << account.getBalance() << endl;
return 0;
}
Output:
Deposit of INR 500 successful. New balance: INR 1500
Withdrawal of INR 200 successful. New balance: INR 1300
Insufficient funds or invalid amount.
Current balance: INR 1300
Explanation:
In the C++ example program-
- We define a BankAccount class that encapsulates the attributes of an account, like the account number, balance, and methods to interact with the account.
- There are two private data members, i.e., accountNumber and balance.
- We define a member function deposit() to add a positive amount to the account balance and print the updated balance when called.
- Another member function, withdraw(), takes an amount as an argument and checks if the amount is positive and less than the current balance using the logical AND operator and relational equality operator.
- If the conditions are satisfied, it subtracts the amount from the balance data member and prints the updated balance to the console.
- Or else, it prints a string message stating that the amount is invalid or the balance is insufficient when called.
- Lastly, we have the getBalance() method, which returns the current balance.
- In the main() function, we create an object of the BankAccount class called account, with an account number of "12345" and an initial balance of 1000.0.
- We then use the dot operator to call the deposit() method on the account object, with an amount of 500.0, which increases the balance to 1500.0.
- Next, we call the withdraw() method twice. First, with an amount of 200.0, which decreases the balance to 1300.0. And second, with an amount of 1500.0, which is an invalid withdrawal and prints an error message.
- Finally, we call the getBalance() method and print the current balance of 1300.0 using the cout command.
Read More: Encapsulation In C++ | Getter/ Setter Methods & More (+Examples)
Other Features Of OOPs In C++
The five concepts mentioned above form the core of the OOPs concepts in C++. However, there are a few other features of OOPs/ concepts in OOPs that are worth mentioning. They are discussed in this section.
Exception Handling | OOPs Concept In C++
Exception handling is a programming concept that enables you to handle and react to unexpected or exceptional situations that could arise while a program is being executed. These circumstances, which are sometimes referred to as exceptions, may include mistakes, issues, or unexpected events that block the normal process of code execution.
The purpose of exception handling is to provide a structured method of managing errors gracefully while preventing sudden program termination. With exception handling, you can detect, deal with, and maybe recover from unusual circumstances without having the program crash.
Syntax:
try {
// Code that might throw an exception
// ...
if (/* Some condition that indicates an error */) {
throw exception;}
} catch () {
// Code to handle exception
// ...
}
Here,
- try: It is a keyword that allows you to define a block of code to be tested for errors while it is being executed.
- throw: It is a keyword that throws an exception when a problem is detected,
- catch: It allows you to define a block of code to be executed if an error occurs in the try block.
- exception: It specifies the type of the error generated while executing the try block
Let's understand this related OOPs concept in C++ with a real-life analogy
Consider an online payment processing system. In this scenario, exception handling is essential to properly handle circumstances like unsuccessful transactions caused by network issues, insufficient cash, or incorrect card information. The system can deliver user-friendly error notifications, undo incomplete transactions, and guarantee the dependability and integrity of financial activities by implementing exception handling. This improves user experience and guards against any financial losses brought on by unanticipated mistakes made during payment processing.
Code Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
try {
int numerator = 10;
int denominator = 0;
if (denominator == 0) {
throw runtime_error("Division by zero");
}
int result = numerator / denominator;
cout << "Result: " << result << endl;
} catch (const runtime_error& e) {
cout << "Exception caught: " << e.what() << endl;}
return 0;
}
Output:
Exception caught: Division by zero
Read More: C++ Exception Handling | Use Try, Catch, & Throw (+Examples)
Constructors in OOPS in C++ | OOPs Concept In C++
Constructors in C++ are special member functions that are called automatically when an object belonging to a class is created. The constructors are responsible for the initialization of the object's data members and the establishment of its initial state. They make sure that an object is created with valid and consistent values, establishing the foundation for the object's functionality and behaviour.
Constructors are particularly helpful for allocating resources, setting initial values, and setting up the object for use immediately after creation. They have the same name as the class name and don't have the return type, not even void. Here is the standard syntax of the constructor in C++.
Syntax:
class ClassName {
Access_specifier:
ClassName() {
}
};
Here,
- class: It is the keyword used to define the class
- ClassName: It is the name of the class in which the constructor is defined
- Access_specifier: It is a data visibility controller which could be public, private, protected
- ClassName(): It is a constructor (It has the same name as the class name)
Let's understand by a real-life analogy
Consider that we are developing a program to handle student data. In this case, we've created a class called Student with the characteristics of name, age, and major. We've included a default constructor to simplify object creation.
The default constructor automatically activates when we create a Student object without specifying any special values. The attributes of the object are initialized with predetermined default values in this constructor, such as Unknown for the name, 0 for the age, and Undeclared for the major.
Code Example:
#include <bits/stdc++.h>
using namespace std;
class Student {
public:
string name;
int age;
string major;
// Default constructor
Student() {
name = "Unknown";
age = 0;
major = "Undeclared";}
// Method to display student details
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Major: " << major << endl;}
};
int main() {
Student student1; // Object created using the default constructor
cout << "Student 1 details:" << endl;
student1.display();
return 0;
}
Output:
Student 1 details:
Name: Unknown
Age: 0
Major: Undeclared
Explanation:
In the example, we create a class, Student, with three attributes- name, age, and major. We also define a default constructor, Student(), inside the class, which initializes the attributes to default values ("Unknown" for name, 0 for age, and "Undeclared" for major). Then, we create an object student1 of the Student class using this default constructor inside the main() function. Since no arguments are provided, the default constructor is automatically called.
Read More: Constructor In C++ | Types, Benefits, Uses & More (With Examples)
There are multiple types of constructors, and they are classified based on their basic nature, parameters, and behaviour. This includes:
Parameterized Constructor: In C++, the parameterized constructor is a particular kind of constructor that accepts one or more parameters as arguments when creating an object. It enables you to supply unique values when initializing an object's data members. It is especially helpful when you want to create objects with specific beginning states based on the supplied inputs.
Syntax:
class ClassName {
Access_specifier:
ClassName(parameter_type parameter1, parameter_type parameter2, ...){
}
};
Here,
- The class keyword indicates that we are defining a class by the name ClassName.
- Access_specifier is the data visibility controller which determines who can access the data.
- The ClassName() is the parameterized constructor which takes a parameter list consisting of paramater1, parameter 2, and so on.
- parameter _type is the data type of the respective parameter.
Let's understand by a real-life analogy:
Let's consider a scenario where we need to model rectangles in a program. To achieve this, we define a Rectangle class with attributes width and height. In this scenario, we utilize the parameterized constructor to create a Rectangle object named rect with specific width and height values. The constructor initializes these values when the object is created.
Copy Constructor: It is a special constructor in C++ that makes a copy of an existing object while creating a new one. It is utilized whenever an object is passed as a value to a function, returned as a value from a function, or when an object is given the value of another object belonging to the same class. The copy constructor in C++ makes sure that the new object's data members are set to the same values as those of the source object.
Syntax:
class ClassName {
Access_specifier:
ClassName( ClassName &source){
// Copy data members from the source object
}
};
Here, the syntax is the same as that of the constructor, with two differences. First, the ClassName() here refers to the copy constructor. Second, the ClassName &source is a parameter of the copy constructor that accepts objects of the same class.
Benefits Of OOP In C++ Over Procedural-Oriented Programming
Procedural programming languages encompass techniques that revolve around the concept of procedures or functions. Here, the whole program is divided into smaller single functions or modules that perform specific tasks. On the other hand, OOP revolves around creating models that mimic real-world entities through the use of objects and the encapsulation of data and behaviour.
Object-oriented programming offers several advantages over procedure-oriented programming. They are as follows:
- Modularity and Reusability: The OOPS Concept in C++ allows you to create classes and objects, promoting modularity and reusability. This enables you to build components that can be used across different parts of your program, reducing redundancy and making maintenance easier.
- Encapsulation: This feature of OOP in C++ helps hide implementation details and safeguards data from unwanted access by encapsulating it in a class containing data and functions. This improves data security and stops unauthorized changes.
- Abstraction: It is an OOP concept or technique where you focus on essential features and ignore unnecessary details. This simplifies program design, making it easier to comprehend and manage complex systems.
- Inheritance: This OOPs concept in C++ facilitates the creation of new classes that inherit attributes and behaviors from existing classes. This promotes code reuse and allows you to create specialized classes that build upon the features of a base class.
- Polymorphism: This feature of OOPs in C++ enables the use of a single interface to represent different data types or objects. This promotes code flexibility and simplifies code maintenance by allowing you to extend or modify behaviour without altering existing code.
- Code Maintenance: The modular and organized structure provided by many features of OOPs in C++ simplifies code maintenance. For example, changes made to a class are localized, reducing the risk of unintended side effects in other parts of the program.
- Collaborative Development: The class OOPs concept in C++ enables multiple programmers to work on several classes simultaneously. This promotes collaborative development, which makes large-scale software projects more manageable and increases productivity.
- Real-World Modeling: The entities and connections in OOP are very similar to those in reality. This improves the overall understanding of the function and behaviour of the program by making it simpler to translate real-world circumstances into code.
- Error Handling: The features of OOP in C++ promote better error handling through mechanisms like exceptions, making it easier to identify and manage errors during runtime
- Scalability: Both small and large-scale initiatives can benefit from the OOPs concept in C++ programming. For example, as a project scales in size and becomes more complex, characteristics of OOP, like encapsulation, inheritance, and modularity, make it simpler to scale and manage complexity.
Disadvantages Of OOPS Concept In C++
Here are some of the disadvantages of the OOPS concept in C++ that limit its usage-
- Overhead: The necessity to handle objects, inheritance hierarchies, and dynamic dispatching in OOP might result in significant overhead. Compared to more procedural approaches, these features of OOP in C++ programs may result in slightly slower program execution.
- Complexity: Applying multiple OOPs concepts in C++ programs can result in complicated code architectures, particularly in large projects with intricate class interactions. The complexity of the code may make it more challenging to comprehend and update.
- Learning Curve: Compared to more procedural programming, OOPS concepts in C++, like classes, objects, inheritance, and polymorphism, may have a longer learning period for newcomers.
- Run-time Overhead: For some features of OOP in C++ where the proper technique must be determined at runtime, for example, dynamic binding and virtual functions, there may be run-time overhead.
- Efficiency: When dealing with small and numerous objects, OOP may not always be as memory-efficient as procedural programming. Storage of more object-related data can result in memory overhead that can cause problems.
- Performance: While modern compilers do a good job of optimizing OOP code, there may be some situations in which direct procedural code is more performance-efficient, particularly in some low-level programming contexts.
- Encapsulation trade-offs: Although the encapsulation OOPs concept in C++ protects data, it can occasionally result in an excessive number of getter and setter methods. This could result in more complex code and poorer performance.
- Design Difficulty: Complex class hierarchies due to inheritance OOPs concept in C++ can be difficult to design correctly. And bad design decisions might cause architectural issues later in the development process.
Why Is C++ A Partial OOP Language?
The fundamental elements that make up an object-oriented programming language are inheritance, polymorphism, and encapsulation. Any programming language that fully supports these features is considered to be an object-oriented programming language, but a language that partially supports all three features is considered to be partial object-oriented programming.
Here are some of the OOP concepts that are supported by C++:
- Classes: Classes are the basic building blocks of OOP. They are used to represent real-world objects and their properties.
- Objects: Objects are instances of classes. They have the properties and behaviours defined by their class.
- Inheritance: This OOPs concept in C++ refers to the ability of one class to inherit the properties and behaviors of another class.
- Polymorphism: This feature of OOP in C++ represents the ability of objects to take on different forms. It is achieved through overloading and overriding.
- Encapsulation: This OOPs concept entails hiding data and implementation details from the user.
While C++ programming language supports some of the most prominent OOPs concepts, there are some concepts that are not supported by C++. These include:
- Garbage collection: Garbage collection is the automatic management of memory. C++ does not have garbage collection, so the programmer is responsible for managing memory manually.
- Multiple Dispatch: C++ primarily uses single dispatch, which means that the method called is determined by the type of object it is called on (static dispatch). Multiple dispatch, where the method is chosen based on the types of multiple objects involved, is not directly supported.
- Final classes: Final classes cannot be subclassed in C++ since it does not have final classes.
- It is possible to write C++ code without using classes: This means that you can create programs in C++ that do not use the OOP concepts of encapsulation, inheritance, and polymorphism.
- C++ has a global namespace: This means that all variables and functions are accessible from anywhere in the program, even if they are declared inside a class. This can make it difficult to maintain the privacy of data and the modularity of code.
- C++ has a manual memory management system: This means that the programmer is responsible for allocating and freeing memory. This can be a source of errors and can make it difficult to write reliable code.
Because of these limitations, C++ is considered a partial OOP language. However, it is still a powerful language that can be used to create complex and sophisticated programs. Despite these limitations, C++ is a versatile language that is used to create a wide variety of software, including operating systems, compilers, and games.
Code Example:
#include <iostream>
int main() {
int age = 25;
std::string name = "Ashish Jain";
std::cout << "My name is " << name << " and I am " << age << " years old." << std::endl;
return 0;
}
Output:
My name is Ashish Jain and I am 25 years old.
Explanation:
- We begin by including the <iostream> header file and start with the int main() function.
- Then, we declare and initialize an integer variable age with the value of 25. The int keyword specifies that the variable age is an integer.
- Next, we declare a string variable, name, using std::string and initialize it with the value Ashish Jain. The std::string keyword specifies that the variable name is a string.
- Then, we use the std::cout statements to print the name and age variable along with a phrase.
- Finally, the return 0 statement indicates that the program has terminated successfully. The return keyword tells the compiler to return the value 0 from the function.
This code does not use any classes. It simply declares two variables, age and name, and then prints their values to the console. This is perfectly valid C++ code even though it does not take advantage of the OOP concepts in C++.
Conclusion
In conclusion, object-oriented programming (OOP) is a foundational paradigm in C++ that empowers developers to create well-organized, modular, and maintainable code. Through the use of OOPs concepts in C++, such as classes and objects, encapsulation, inheritance, polymorphism, and abstraction, programmers can design software systems that are both efficient and adaptable.
By mastering these OOPs concepts in C++, developers can create code that is not only more organized and readable but also easier to maintain and extend. This approach enhances collaboration, reduces bugs, and promotes the development of robust and scalable software solutions. As such, OOP remains a cornerstone of modern C++ programming, ensuring that codebases remain agile and adaptable to evolving requirements and challenges.
Also read- 51 C++ Interview Questions For Freshers & Experienced (With Answers)
Frequently Asked Questions
Q. What is the OOPS concept in C++?
Object-oriented programming (OOP) is a programming paradigm that emphasizes using classes and objects to structure code and model real-world entities and their relationships. There is a set of OOPs concept in C++ that help in designing, organizing, and managing code. Some of the key features of OOP in C++ are:
- Classes and Objects: Classes are blueprints that define attributes and methods, while objects are instances of classes. They encapsulate data and behaviour.
- Inheritance: This OOPs concept in C++ allows one class (derived or subclass) to inherit attributes and behaviors from another class (base or superclass). It promotes code reuse and hierarchy formation. Multilevel inheritance and hierarchical inheritance are some of the examples.
- Polymorphism: This OOPs concept in C++ enables objects of different classes to be treated uniformly through a common interface. It involves overloading and method overriding.
- Encapsulation: This OOPs concept in C++ entails bundling data and methods within a class and hiding implementation details. Access to data is controlled through access specifiers.
- Abstraction: This OOPs concept in C++ focuses on essential features while ignoring unnecessary details. Classes provide an abstract internal representation of real-world entities.
Q. Why is the use of abstraction?
Abstraction is the practice of hiding complex details from the end user and only showing the most essential information. Here are some of the uses of abstraction-
- Simplification: The feature of OOP in C++ reduces complexity by breaking down a system into manageable components and interactions. It allows developers to work with high-level concepts without getting lost in implementation specifics.
- Security and Data Hiding: The abstraction OOPs concept in C++ facilitates data encapsulation and access control, enhancing data security and preventing unauthorized access to sensitive information.
- Clarity and Readability: By highlighting the most important information and hiding complex details, the abstraction OOPs concept in C++ helps write code that is simpler to understand. This makes the codebase's overall readability better.
- Maintenance and Updates: Abstracting complex details isolates them from the rest of the system. When changes or updates are needed, modifications can be made within the abstraction without affecting the external interface, minimizing the risk of introducing errors.
Q. What is the real-life example of the class OOPs concept in C++?
One real-world example of a class in object-oriented programming (OOP) is the concept of a bank account. Here, we can say that the Bank Account class defines attributes (like account number and amount) and methods (like deposit and withdrawal), just as in OOP.
These aspects of the object would stand in for individual bank accounts by encapsulating data and operations in a systematic way. This abstraction simulates the interactions of real-world entities, which makes it simpler to store and manipulate financial data in a consistent and structured manner.
Q. What are some major object-oriented programming languages?
The powerful programming languages that follow the object-oriented programming paradigm or design are called object-oriented programming languages. Following are some such languages:
- Java
- Javascript
- Python
- PHP
- C#
- Ruby
- CPP
Q. In C++, is a class object stored in a heap or stack?
The memory allocated to the object depends upon how the object is created. Objects in C++ can be allocated either in stack or heap memory, depending on creation and scope. Let’s elaborate a little more on this.
Stack: A class object is kept in the stack memory when it is created as a local variable inside a function or a block. The program automatically allocates and deallocates the object's memory when it enters and leaves the defined scope of the object.
Example:
void someFunction() {
MyClass obj; // Object 'obj' is stored in the stack
}
Here, Object 'obj' is automatically deallocated when the function exits
Heap: A class object created using dynamic memory allocation (with the new operator) is kept in the heap memory. To prevent memory leaks, the object's memory must be manually deallocated using delete.
Example:
void someFunction() {
MyClass* objPtr = new MyClass(); // Object is created in the heap
delete objPtr;
}
Here, Object ‘objPtr’ must be manually deallocated using the delete keyword.
Q. What are different types of polymorphism?
There are two primary types of the polymorphism OOPs concept in C++, which are further divided into sub-types or use cases. They are-
Compile time polymorphism: This type of polymorphism is also known as early binding. Usually, function overloading and operator overloading are used to achieve compile time polymorphism.
- Function overloading enables the definition of numerous special functions in a class with the same name but distinct argument lists.
- The correct function is determined at compile-time based on the arguments supplied.
- Using operator overloading, you can also specify how operators interact with user-defined data types. This is decided at compile-time based on the kinds of operands and the operators employed.
Runtime polymorphism: Runtime polymorphism is when the choice of which function is to be called is based on the object's actual type, determined at run time. Examples of polymorphism at runtime include method overriding and virtual function. This is also referred to as dynamic polymorphism, and it involves the use of virtual functions and different types of inheritance to achieve flexible and adaptable behaviour in object-oriented programming languages.
Q. What is meant by exception handling?
Exception handling is a programming method used to handle and react to unexpected or unusual situations that may arise while a program is being executed. These unusual occurrences are frequently referred to as exceptions. Instead of allowing mistakes, anomalies, and other uncommon circumstances to cause the program to crash or deliver unpredictable consequences, exception handling offers a structured technique to handle them in a controlled manner.
Quiz Time!!!
This compiles our discussion on OOPs concept in C++. Here are a few more C++ topics you should explore:
- Friend Function In C++ | Class, Types, Uses & More (+Examples)
- Static Member Function In C++ Explained With Proper Examples
- Inline Function In C++ | Declare, Working, Examples & More!
- C++ Type Conversion & Type Casting Demystified (With Examples)
- Destructor In C++ | Understanding The Key To Cleanups (+ Examples)
- Constructor Overloading In C++ Explained With Real Examples
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Samruddhi Deshmukh 2 weeks ago
Ujjwal Sharma 2 weeks ago
Kartik Deshmukh 3 weeks ago
P Varun Kudva 3 weeks ago
Sanchit Dhale 1 month ago
Jyoti Kumari 2 months ago