C++ Programming Language
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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)
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Table of content:
- 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
Operators In C++ | Types, Precedence & Associativity (+ Examples)

Operators in C++ are essential basic components for carrying out manipulations and modifications on data in a C++ program. That is, they provide outcomes by performing operations on the input parameters, much like functions, and solving complex algorithms. There are multiple types of operators in C++ programming language, each with its own purpose. In this article, we discuss all these operators and how to use them with the help of symbols/ syntax and code examples.
What Is Operator In C++?
An operator in programming is a symbol (or a set of symbols) that designates a particular operation or action to be carried out on data. Programmers can execute mathematical calculations, value comparisons, value assignments, modifications and other operations by manipulating variables and values with operators.
- Based on their usefulness, operators in programming languages may be divided into many categories.
- For example, mathematical operators (such as addition, subtraction, multiplication, and division) that carry out arithmetic operations on the operands.
- We also have comparison operators that help compare two or more operands, logical operators to perform logical operations/ logical comparisons, assignment operators that are used to assign values to variables, and more.
These different types of operators in C++ language help programmers develop clear, effective code that gives desired results by executing complicated operations on data. Programming in many languages, including C++, Python, Java, and others, requires a fundamental understanding of operators and the ability to use them effectively.
Types Of Operators In C++ With Examples
Operators are a key component of the C++ language that enable us to execute a variety of operations on data. Listed in the table below are the different types of operators most commonly used in C++ programs, along with their kinds.
Arithmetic Operators | Addition (+): int result = 5 + 3; // result is 8 Subtraction (-): int result = 7 - 2; // result is 5 Multiplication (*): int result = 4 * 6; // result is 24 Division (/): float result = 10 / 3; // result is 3.333 Modulus (%): int result = 10 % 3; // result is 1 |
Relational Operators | Equal to (==): bool result = (5 == 5); // result is true Not equal to (!=): bool result = (6 != 3); // result is true Greater than (>): bool result = (7 > 4); // result is true Less than (<): bool result = (2 < 8); // result is true Greater than or equal to (>=): bool result = (5 >= 5); // result is true Less than or equal to (<=): bool result = (3 <= 4); // result is true |
Logical Operators | Logical AND (&&): bool result = (true && false); // result is false Logical OR (||): bool result = (true || false); // result is true Logical NOT (!): bool result = !true; // result is false |
Assignment Operators | Simple assignment (=): int x = 5; Addition assignment (+=): int x = 5; x += 3; // x is now 8 Subtraction assignment (-=): int x = 5; x -= 2; // x is now 3 Multiplication assignment (*=): int x = 5; x *= 4; // x is now 20 Division assignment (/=): int x = 10; x /= 2; // x is now 5 |
Increment and Decrement Operators | Increment (++): int x = 5; x++; // x is now 6 Decrement (--): int x = 5; x--; // x is now 4 |
Bitwise Operators |
Bitwise AND (&) |
You may execute a variety of actions on variables and values in your C++ programs by skillfully utilizing these operators. We will discuss these and a few other operators in C++ in the sections ahead.
What Are Arithmetic Operators In C++?
We use arithmetic operators in C++ to execute mathematical operations/ arithmetic manipulations on numerical quantities. These operations include addition, subtraction, multiplication, and division.
Here is a table listing all the C++ arithmetic operators and their associated syntax:
Symbol |
Operator Name |
Syntax |
+ |
Addition |
result = operand1 + operand2; |
- |
Subtraction |
result = operand1 - operand2; |
* |
Multiplication |
result = operand1 * operand2; |
/ |
Division |
result = operand1 / operand2; |
% |
Modulus (Remainder) |
result = operand1 % operand2; |
Now, let's take a look at a simple C++ program that illustrates how to use these operators in action.
Code Example:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 3;
int result;
result = num1 + num2; // Addition Operator
std::cout << "Addition: " << result << std::endl;
result = num1 - num2; // Subtraction Operator
std::cout << "Subtraction: " << result << std::endl;
result = num1 * num2; // Multiplication Operator
std::cout << "Multiplication: " << result << std::endl;
result = num1 / num2; // Division Operator
std::cout << "Division: " << result << std::endl;
result = num1 % num2; // Modulus (Remainder) Operator
std::cout << "Modulus: " << result << std::endl;
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Explanation:
In the simple C++ code above, we begin by including the <iostream> header file essential for the input/ output operations.
- We then initiate the main() function, which is the program's entry point for execution.
- In main(), declare three variables, num1, num2, and result, all of integer data type. We also assign values 10 and 3 to variables num1 and num2, respectively.
- Then, we carry out various operations on these variables using arithmetic operators.
- First, we use the addition operator to calculate the total of num1 and num2.
- Next, we calculate the difference between num1 and num2, which is 7, using the subtraction operator.
- Then, we calculate the product of num1 and num2 (which is 30) using the multiplication operator.
- After that, we use the division operator to carry out the division operation, yielding the value 3 as the result of dividing num1 by num2.
- Lastly, we directly calculate the remainder when num1 is divided by num2 (which is 1), using the modulus operator (remainder).
- All the outcomes are stored in the result variable one by one and printed to the output console, side by side, using the cout command.
- Finally, the main() function successfully terminates with a return 0 statement.
What Are Assignment Operators In C++?
Variables can have values assigned to them using assignment operators. They let you carry out an action and save a value in a variable or change its current value concurrently. Depending on the activity they carry out, assignment operators can be further classified into several categories.
The many assignment operators available in C++ are shown in the following table:
Symbol |
Operator Name |
Syntax |
= |
Simple assignment |
variable = expression |
+= |
Addition assignment |
variable += expression |
-= |
Subtraction assignment |
variable -= expression |
*= |
Multiplication assignment |
variable *= expression |
/= |
Division assignment |
variable /= expression |
%= |
Modulo assignment |
variable %= expression |
Let's examine some code to illustrate how assignment operators in C++ are used.
Code Example:
#include <iostream>
int main() {
int x = 5; // Simple assignment
x += 3; // Addition assignment
std::cout << "x += 3: " << x << std::endl;
x -= 2; // Subtraction assignment
std::cout << "x -= 2: " << x << std::endl;
x *= 4; // Multiplication assignment
std::cout << "x *= 4: " << x << std::endl;
x /= 2; // Division assignment
std::cout << "x /= 2: " << x << std::endl;
x %= 3; // Modulo assignment
std::cout << "x %= 3: " << x << std::endl;
return 0;
}
Output:
x += 3: 8
x -= 2: 6
x *= 4: 24
x /= 2: 12
x %= 3: 0
Explanation:
In the C++ code example-
- Inside the main() function, we declare an integer variable x and assign the value 5 to it using the basic assignment operator (=).
- Then, we use other assignment/ compound assignment operators to carry out various operations on this variable and assign the value back to the variable x.
- As mentioned in the code comments, we first use the addition assignment operator (+=) to add the right-hand operand/ value to the initial value of x (5) and assign the result back to x.
- We then use the cout command to print the value to the console. Note that at this point, the updated value of x is 8, so the next operation will be carried out on this value and not the initial value of x.
- Then, we use the subtraction assignment operator (-=) to subtract the value on the right-hand side (2) from the current value of x (8). The updated value of x (6) is then printed to the console.
- After that, we use the multiplication assignment operator (*=) to multiply the value of x with the right-hand operand (4) and assign the updated value (24) back to x.
- Next, we use the division assignment operator (/=) and divide the value of x (24) by 2, then assign the updated value back to x (12).
- Lastly, we use the modulo assignment operator (%=) to find the remainder of the division of x by 3. This value is then assigned back to x (0), as shown in the output console.
What Are Relational Operators In C++?
The relational operators are employed to assess the connection between two values by comparison. These operators return boolean values, that is, true or false, after they have carried out a relational comparison between the left operand and the right operand. The table below lists the various relational operators in C++ along with the corresponding syntax.
Symbol |
Operator Name |
Syntax |
== |
Equal to |
operand1 == operand2 |
!= |
Not equal to |
operand1 != operand2 |
> |
Greater than |
operand1 > operand2 |
< |
Less than |
operand1 < operand2 |
>= |
Greater than or equal to |
operand1 >= operand2 |
<= |
Less than or equal to |
operand1 <= operand2 |
Now, let's look at an example showcasing the implementation of these relational operators in C++.
Code Example:
#include <iostream>
int main() {
int x = 5;
int y = 3;
bool result1 = (x == y);
bool result2 = (x != y);
bool result3 = (x > y);
bool result4 = (x < y);
bool result5 = (x >= y);
bool result6 = (x <= y);
std::cout << "Result 1: " << result1 << std::endl;
std::cout << "Result 2: " << result2 << std::endl;
std::cout << "Result 3: " << result3 << std::endl;
std::cout << "Result 4: " << result4 << std::endl;
std::cout << "Result 5: " << result5 << std::endl;
std::cout << "Result 6: " << result6 << std::endl;
return 0;
}
Output:
Result 1: 0
Result 2: 1
Result 3: 1
Result 4: 0
Result 5: 1
Result 6: 0
Explanation:
In the example C++ code above-
- We initialize two integer variables, x and y, with values 5 and 3 (respectively), inside the main() function.
- Then, we compare these values using various relational operators, and the outcomes are stored in boolean variables (result1 to result6).
- Using the equal to relational operator (==), we check if x is equal to y, and the outcome is stored in result1. This turns out to be false because 5 is not equal to 3.
- The outcome of not equal to relational operator (!=) is stored in variable result2, which is true because 5 is not equal to 3.
- Next, we declare a variable result3 and use the greater than relational operator (>) to define it. Its value is true because 5 is greater than 3.
- Then, using the less than relational operator (<), we define result4 which is false because 5 is not less than 3.
- After that, we define result5 using the greater than or equal to relational operator (>=), and its value is true because 5 is greater than or equal to 3.
- Next, we use the less than or equal to relational operator (<=) to get the value of result6, which is false because 5 is not less than or equal to 3.
- We then use a set of cout commands to display the values of all result variables along with a formatted string.
- The program shows how to compare values and assess conditions in C++ using relational operators.
Also Read: 6 Relational Operators In C & Precedence Explained (+Examples)
What Are Logical Operators In C++?
C++ logical operators are utilized when working with boolean values (true or false). These operators let you combine many conditions and determine the truth value of each one using a truth table. The three types of logical operators in C++ are logical AND, logical OR, and logical NOT. The result of the logical operators is defined in the truth tables for each operator. They define how the outcome of the whole expression depends on the outcomes of the individual expressions on the left-hand side and the right-hand side.
The table below shows the three logical operators in C++, along with the symbol and syntax.
Symbol |
Operator Name |
Syntax |
&& |
Logical AND |
condition1 && condition2 |
|| |
Logical OR |
condition1 || condition2 |
! |
Logical NOT |
!condition |
Let's take a code sample to show how logical operators are used in C++ programming language.
Code Example:
#include <iostream>
int main() {
int x = 5;
int y = 10;
bool condition1 = (x > 0) && (y < 20);
bool condition2 = (x == 5) || (y > 15);
bool condition3 = !(x > 0);
std::cout << "Condition 1: " << condition1 << std::endl;
std::cout << "Condition 2: " << condition2 << std::endl;
std::cout << "Condition 3: " << condition3 << std::endl;
return 0;
}
Output:
Condition 1: 1
Condition 2: 1
Condition 3: 0
Explanation:
- We begin the C++ example by initializing two integer variables, x and y, with values 5 and 10 (respectively) inside main().
- Next, we use logical operators to connect expressions that are made up of other operators and compare the values as stipulated.
- We use the AND logical operator in the first operation, i.e., (x > 0) && (y < 20). It checks if x is greater than 0 and y is less than 20.
- If both the expressions return true (yes in this case), then the AND operation also returns true, represented by the value 1. The outcome is stored in the condition1 variable.
- After that, we use the OR logical operator, which returns true even if one of the expressions is true. The second operation, i.e., (x == 5) || (y > 15) we determines if x equals 5 OR y exceeds 15.
- The result is stored in the condition2 variable, whose value is true (i.e., 1) since x is actually equal to 5, meaning at least one of the requirements is met.
- Next, we use the NOT logical operator, which inverses the value of the initial condition. Here, in the expression !(x > 0), we check if x is greater than 0, and the outcome of this logical operation will be the opposite.
- The initial condition (x>0) is true since x is bigger than 0 and 5 in number. The outcome of the logical NOT is stored in the condition3 variable, which is false, denoted by 0.
- Finally, we use a set of cout commands to print the outcome of each operation to the output console.
Learn More: Logical Operators In C++ | Use, Precedence & More (With Examples)
What Are Bitwise Operators In C++?
As the name suggests, the bitwise operators in C++ are used to modify the value of operands at the bit level. They compare, shift, or manipulate the bits of operand values and carry out required operations on the binary representation of the data values.
The C++ bitwise operators are shown in the following table:
Symbol |
Operator Name |
Syntax |
& |
Bitwise AND operator |
result = operand1 & operand2 |
| |
Bitwise OR operator |
result = operand1 | operand2 |
^ |
Bitwise XOR operator (exclusive OR) |
result = operand1 ^ operand2 |
~ |
Bitwise NOT operator (complement) |
result = ~operand |
<< |
Bitwise left shift operator |
result = operand << n |
>> |
Bitwise right shift operator |
result = operand >> n |
Here is a sample code that shows how to use binary bitwise operators in C++.
Code Example:
#include <iostream>
using namespace std;
int main() {
int x = 5; // Binary: 0101
int y = 3; // Binary: 0011
int result;
// Bitwise AND
result = x & y; // Binary: 0001
cout << "Bitwise AND: " << result << endl;
// Bitwise OR
result = x | y; // Binary: 0111
cout << "Bitwise OR: " << result << endl;
// Bitwise XOR
result = x ^ y; // Binary: 0110
cout << "Bitwise XOR: " << result << endl;
// Bitwise NOT
result = ~x; // Binary: 1010 (2's complement representation)
cout << "Bitwise NOT: " << result << endl;
// Bitwise left shift
result = x << 2; // Binary: 10100
cout << "Bitwise left shift: " << result << endl;
// Bitwise right shift
result = y >> 1; // Binary: 0001
cout << "Bitwise right shift: " << result << endl;
return 0;
}
Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Bitwise left shift: 20
Bitwise right shift: 1
Explanation:
In the C++ code example-
- Inside the main() function, we declare and initialize two integer variables, x and y, with values of 5 (binary equivalent 0101) and 3 (binary equivalent 0011), respectively.
- We then use bitwise operators to carry out bitwise operations on these variables.
- First, we use the bitwise AND (&) to compare individual bits of the right and left operand values. The operator returns true if either both the bits are 1 or 0.
- The outcome of the operation, i.e., 0001 or integer 1, is stored in the result variable and printed to the console using the cout command.
- Next, we use the bitwise OR (|) to compare the bits of operands and return 1 even if either of the bits is 1 and 0 otherwise.
- The outcome of this operation is binary number 0111 with integer representation 7, which is stored in the result variable and printed to the console.
- After that, we use the bitwise XOR (^), which returns 1 only if the bits of the operands are different and 0 otherwise.
- The outcome of this operation is 0110, whose integer representation (6) is stored in the result variable and displayed in the output console.
- Then, we use the bitwise NOT (~), which reverses the bit values at every position and provides a complement.
- Because the two's complement representation of x is subjected to the bitwise NOT operation, the outcome is -6.
- Next, we use the bitwise Left Shift (<<), which shifts the bits of the operand by the specified number. Here, x is essentially increased by 2 to the power of the shift count (2). The outcome is 20.
- Lastly, we use bitwise Right Shift (>>) to move the bits of y one bit to the right. By doing this, y is divided by 2 increased to power of shift count (1). The outcome is 1.
Know More: Bitwise Operators In C++ Explained In Detail With Examples
What Is Ternary/ Conditional Operator In C++?
The ternary or conditional operator in C++ is a shorthand method for writing conditional control statements in a single line. It is denoted by the equation mark symbol followed by a colon (?:). The table below demonstrates the syntax of the ternary operator.
Symbol |
Operator Name |
Syntax |
?: |
Ternary conditional operator |
condition ? expression1 : expression2 |
Here, the expression that evaluates to true or false is represented by the term condition. If the outcome of the condition expression is true, then expression1 is executed; otherwise, expression2 is carried out. Let's look at an example to see how the ternary operator is used.
Code Example:
#include <iostream>
int main() {
int num = 10;
std::string result = (num > 5) ? "Greater than 5" : "Less than or equal to 5";
std::cout << result << std::endl;
return 0;
}
Output:
Greater than 5
Explanation:
In the code example-
- We first initialize an integer variable num with the value of 10.
- Then, we define a string variable result using the ternary operator in C++. It is given a value where the initial condition is to check if num is greater than 5, i.e., num > 5.
- If the condition is satisfied, which it is in this instance, the result variable is assigned the value in expression, i.e., "Greater than 5".
- Otherwise, the expression, i.e., "Less than or equal to 5", is assigned if the criterion is false.
- In this case, the result reads "Greater than 5" because 10 is, in fact, greater than 5.
Also Read: Ternary (Conditional) Operator In C Explained With Code Examples
Miscellaneous Operators In C++
In addition to the usual types of operators in C++, some other commonly used operators fulfil special functions. This includes the sizeof() operator, comma operator, and pointer operator. Let us take a closer look at these operators.
What Is The Comma Operator In C++?
The C++ comma operator enables the sequential evaluation of several expressions. It can also be used as a separator when we are defining or initializing variables in a C++ program. The syntax and symbol for this operator are given in the table below.
Symbol |
Description |
Syntax |
, |
The comma operator works both as an operator and as a separator. |
expr1, expr2 |
Code Example:
#include <iostream>
int main() {
int x = 5, y = 10, z = 15;
int result = (x++, y++, z++);
std::cout << "Result: " << result << std::endl;
std::cout << "x: " << x << ", y: " << y << ", z: " << z << std::endl;
return 0;
}
Output:
Result: 15
x: 6, y: 11, z: 16
Explanation:
- We first use the comma operator to initialize three variables, x, y, and x, with the values 5, 10, and 15, respectively.
- Then, we define another variable result and assign it the expression (x++, y++, z++). This evaluates each expression in order from left to right, separated by commas.
- The last expression's value, in this case, z++, determines the value of the entire expression.
- The value of z is increased by 1 following the evaluation of the equation. As a consequence, since z has a value of 16 following the increment operator, the outcome is 15.
- The values of x and y are likewise increased by 1, making them 6 and 11, respectively.
- We use the cout command to print these values, as shown in the output console.
Also Read: Comma Operator In C | Code Examples For Both Separator & Operator
What Is The Sizeof() Operator In C++?
In C++, the sizeof() operator is used to calculate the size in bytes, of a data type or variable. It gives the total amount of bytes that an item takes up, including its contents and any padding. It can be used with any data type, including basic kinds, user-defined types, arrays, and structures. The syntax and description of the sizeof() operator are shown in the following table.
Operator |
Description |
Syntax |
sizeof |
Returns the size, in bytes, of a data type or object |
sizeof(type) or sizeof object |
Now, let's look at an example demonstrating the use of the sizeof() operator in C++.
Code Example:
#include <iostream>
using namespace std;
int main() {
int number = 10;
char character = 'A';
double decimal = 3.14;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of number variable: " << sizeof(number) << " bytes" << endl;
cout << "Size of character variable: " << sizeof(character) << " byte" << endl;
cout << "Size of decimal variable: " << sizeof(decimal) << " bytes" << endl;
return 0;
}
Output:
Size of int: 4 bytes
Size of char: 1 byte
Size of double: 8 bytes
Size of number variable: 4 bytes
Size of character variable: 1 byte
Size of decimal variable: 8 bytes
Explanation:
In this example-
- We declare and initialize three variables: number (int type) with 10, character (char type) with A, and decimal (double type) with 3.14.
- Then, we use the sizeof operator inside the formatted string and cout commands to calculate and print the size in bytes of these variables.
- After that, we again use the sizeof operator to calculate the size of the data types instead of variables. The output shows that the size occupied by a variable is the size of the respective data type.
Also Read: The Sizeof() Operator In C | A Detailed Explanation (+Examples)
The Pointer Operator In C++
To begin with, pointer variables are special variables that store the memory addresses of other variables. In other words, they point to the location of other variables. When working with these variables, we often need to use specific operators, termed pointer operators in C++. We can indirectly access and alter the data stored in a location by using pointer variables with these operators. There are two most commonly used pointer operators in C++:
- The address-of operator (&): It provides access to a variable's memory location.
- The dereference operator (*): It is used to access the value stored at a memory address that a pointer is pointing to.
The C++ pointer operators are summarized in the following table:
Operator |
Description |
Syntax |
Address-of (&) |
Returns the memory address of a variable |
&variable |
Dereference (*) |
Accesses the value at a memory address |
*pointer |
Let's look at a code to demonstrate how to use these pointer operators in C++.
Code Example:
#include <iostream>
int main() {
int number = 42; // Declare an integer variable
int* pointer = &number; // Declare a pointer variable and assign the memory address of 'number' to it
std::cout << "Value of number: " << number << std::endl;
std::cout << "Memory address of number: " << &number << std::endl;
std::cout << "Value pointed to by pointer: " << *pointer << std::endl;
return 0;
}
Output:
Value of number: 42
Memory address of number: 0x7ffee414e9d4 (address may vary)
Value pointed to by pointer: 42
Explanation:
- We declare an integer variable number and assign the value 42 to it in the main() function.
- Next, we define an integer pointer variable (i.e., int*) called pointer and use the address-of operator (&) to assign the memory location/ address of the variable number to it.
- Then, we use the dereference operator (*) to access the value kept at the memory address pointed to the pointer variable.
- As a result, we indirectly access the value of the integer through the pointer by writing the *pointer in the third cout command.
- The value of the number, its memory location, and the value referred to by the pointer—which is the same as the value of the number—are all displayed in the output.
- This shows how to use pointer operators in C++ to work with pointers to access and modify data in an indirect manner.
Precedence & Associativity Of Operators In C++
The order in which operators are evaluated in an expression depends on their level of precedence in C++ language. Operators with higher precedence levels are evaluated before those with comparatively lower precedence. The evaluation order of operators with the same precedence is governed by their associativity, which can be either left-to-right or right-to-left.
Listed in the table below are the precedence and associativity levels of several popular operators in C++.
Operators In Descending Order Of Precedence |
Associativity |
() [] -> . (Highest precedence) |
Left-to-right
|
! ~ ++ -- + - * & sizeof |
|
new delete |
|
Multiplicative operators (* / %) |
|
Additive operators (+ -) |
|
Shift operators (<< >>) |
|
Relational operators (< <= > >=) |
|
Equality operators (== !=) |
|
Bitwise AND (&) |
|
Bitwise XOR (^) |
|
Bitwise OR () |
|
Logical AND (&&) |
|
Logical OR () |
|
Conditional operator (?:) |
|
Assignment operators (= += -= *= /= %=) |
Right-to-left |
Comma operator (,) (Lowest precedence) |
Important note- It's vital to remember that parentheses can be used to impose the preferred evaluation order and override the default precedence.
Operators at the top of the table are given priority, while those at the bottom are given less priority. Look at the example below that illustrates a simple case of how operator precedence works in C++.
Code Example:
#include <iostream>
int main() {
int result = 10 + 5 * 2; // 10 + (5 * 2) = 20
std::cout << "Result: " << result << std::endl;
return 0;
}
Output:
Result: 20
Explanation:
In the example above, we want to evaluate the equation 10 + 5 * 2.
- Note that the addition operator + is not evaluated until the multiplication operator * has been evaluated since it has higher precedence.
- So, the operation, 5 + 2, yields the number 10.
- The ultimate value of the result is 20 as a consequence of performing the addition operation using the multiplication result and the number 10.
Understanding what operators are and their precedence is essential for designing proper and predictable code, especially when expressions contain numerous operators. You may regulate the order of operations and ensure the necessary computations are done correctly.
Precedence Chart For Operators In C++
Here is a complete summary of the relative precedence of operators in the C++ language using an operator precedence chart:
Order Of Precedence:
|
Arithmetic Operators Precedence:
|
Shift Operators Precedence:
|
|
Relational and Equality Operators Precedence:
|
|
Precedence in Bitwise Operators:
|
|
Logical Operators Precedence:
|
|
Assignment Operators Precedence:
|
Conclusion
We have discussed all relevant operators in C++ in this article. This includes the different operator types, their functions, precedence rules, and typical use cases. It is extremely important to comprehend operators and their appropriate application to write clear and effective code. One must also understand the precedence of operators in C++ and their associativity so as to write expressions that meet the requirements. Programmers can create elegant solutions, optimize code, and create dependable software systems by mastering operators in C++.
Also Read: 51 C++ Interview Questions For Freshers & Experienced (With Answers)
Frequently Asked Questions
Q. What is a unary and binary operator in C++?
The operators in C++ can be classified into types based on the number of operands they work with. This means that we have binary and unary operators.
Unary Operator: An operator that only works with one operand is called a unary operator. It operates on the operand and then provides the outcome. Unary operators in C++ include:
- Unary minus (-): Negates the value of the operand.
- Unary plus (+): Represents the identity operation, indicating no change to the operand's value.
- Increment (++): Increases the value of the operand by one.
- Decrement (--): Decreases the value of the operand by one.
- Logical NOT (!): Flips the logical value of the operand.
For example, the unary minus operator returns the negative value for the operand. (Note that the subtraction operator is a binary operator).
int number = 5;
int result = -number; // Unary minus operator applied to 'number'
Binary Operator: An operator that uses two operands is called a binary operator. That is, it operates on the two operands (left operand and right operand) and then returns a result. Binary operators in C++ contain examples like these:
- Addition (+): Adds two operands together.
- Subtraction (-): This operator subtracts the second operand from the first.
- Multiplication (*): This operator multiplies two operands.
- Division (/): This operator divides the first operand by the second.
- Assignment (=): Assigns the value of the right operand to the left operand.
Let's look at an example for the addition operator where we have two integer values, and we want to calculate the sum.
int a = 5;
int b = 3;
int result = a + b; // Binary addition operator applied to 'a' and 'b'
Understanding the differences between unary and binary operators in C++ aids in accurately building expressions and carrying out required actions.
Q. What is the operator formula?
The phrase 'operator formula' is not a widely accepted idea in arithmetics or programming. It appears to be a synthesis of the ideas of operators and formulae.
- An operator in programming is a symbol that denotes an action to be taken on one or more operands. Data manipulation, computation, value comparison, and other tasks may all be accomplished with operators.
- On the other hand, a formula is a mathematical phrase that determines a value using variables, constants, and operators. Mathematical operations like addition, subtraction, multiplication, and division are frequently paired with variables and constants to form formulas.
In programming, the classifications of operators inside formulae are used to carry out calculations and produce output. Think about the following formula using C++ operators, for instance:
int result = (a + b) * c / d;
The values of a and b are added together in this formula using the addition operator (+), and the result is then multiplied by the value of c. The outcome is then divided by the value of d using the division operator (/). The structure variable result receives the outcome as its value.
It's vital to remember that a formula's specific operators and their sequence inside the formula affect how operations are performed and the outcome. In conclusion, even if the phrase "operator formula" may not be well defined, programming languages allow you to do computations and change data using operators included within formulae.
Q. What are logical operators in C++?
Programmers utilize logical operators to conduct logical comparisons/ operations and assess the relationship between one or more expressions. With the help of these operators, we may mix and work with boolean values—true or false—to reach conclusions and manage program flow. There are three logical operators in C++:
- Logical AND (&&): If both operands are true, the logical AND operator returns true; otherwise, it returns false. It is represented by the double ampersand symbol (&&).
- Logical OR (||): If at least one of the operands is true, the logical OR operator returns true; otherwise, it returns false. It is represented by the double pipe symbol (||).
- Logical NOT (!): It is a unary operator that negates the value of the single operand. The NOT operator returns false if the operand is true and returns true if the operand is false. It is recognizable by the exclamation symbol (!).
These logical operators are often used in loops, boolean expressions, and conditional statements to control the execution flow based on specific situations. Below is an example illustrating how to use logical operators in C++.
Code Example:
#include <iostream>
int main() {
int x = 5;
int y = 10;
bool condition1 = (x > 3) && (y < 20);
bool condition2 = (x > 7) || (y > 15);
std::cout << "Condition 1: " << std::boolalpha << condition1 << std::endl;
std::cout << "Condition 2: " << std::boolalpha << condition2 << std::endl;
return 0;
}
Output:
Condition 1: true
Condition 2: false
Q. What is operator overloading in C++?
The ability to apply the same operator with many types of operations on operands, including user-defined types, is known as operator overloading in the C++ programming language. It gives programmers the ability to change how an operator behaves when used with different kinds of data or objects.
- An operator's custom implementation, defining how it should act when applied to your user-defined types or existing kinds, can be provided by overloading the operator.
- Through this, you may effortlessly integrate your own classes with the functionality of a combination of operators.
- The arithmetic operators (+, -, *, and /), comparison operators (==,!=,, >, =, and >=), assignment operators (=), and more can all be overloaded in C++.
- You construct a custom function known as an operator function or an overloaded operator function to overload an operator.
- When an operator is used with objects belonging to the specified class, the associated operator's associated function is called.
Look at the example below, which illustrates operator overloading in a user-defined class named Vector for the addition operator (+).
Code Example:
#include <iostream>
class Vector {
private:
int x, y;
public:
Vector(int x, int y) : x(x), y(y) {}
Vector operator+(const Vector& other) {
int sumX = x + other.x;
int sumY = y + other.y;
return Vector(sumX, sumY);
}
void display() {
std::cout << "Vector(" << x << ", " << y << ")" << std::endl;}
};
int main() {
Vector v1(2, 3);
Vector v2(4, 5);
Vector sum = v1 + v2;
sum.display();
return 0;
}
Output:
Vector(6, 8)
Explanation:
The operator + method is used by the Vector class in the code above to overload the addition operator (+).
- The right-side single operand of the addition is represented by the const Vector& argument that is sent to this function.
- The matching member variables (x and y) are added within the function, and a new Vector object is created with the total of the values.
- Two Vector objects can be added to create a new Vector object that reflects the total of their individual components thanks to the overloaded built-in operator.
Test Your Skills: Quiz Time
This compiles the discussion on operators in C++. Here are some other articles you might be interested in reading:
- Storage Classes In C++ & Its Types Explained (With Examples)
- New Operator In C++ | Syntax, Usage, Working & More (With Examples)
- C++ Function | A Comprehensive Guide (With Code Examples)
- OOPs Concept In C++ | A Detailed Guide With Codes & Explanations
- Typedef In C++ | Syntax, Application & How To Use (+Code 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
Kartik Deshmukh 1 day ago
Ritik Mishra 1 month ago
YASH SRIVASTAVA 1 month ago
Sanchit Dhale 1 month ago
Divyansh Shrivastava 1 month ago