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
New Operator In C++ | Syntax, Working, Uses & More (+Examples)

The new operator in C++ programming plays a vital role in dynamic memory allocation and object construction. It enables programmers to allocate memory at runtime and create objects dynamically. Understanding the new operator is crucial for efficient memory management and building flexible applications. In this article, we will explore the usage, syntax, and important considerations when using the new operator in C++ programs.
What Is The New Operator In C++?
The new operator in C++ language is used to dynamically allocate memory from the heap at runtime. If there is sufficient memory, it allocates the required memory, returns the address of the allocated block, and initializes it (if needed).
We can access and manipulate this newly allocated memory using a pointer variable (that points to this location). The new operator is especially useful when working with arrays, classes, or other large data structures that need to be created during program execution. Let's look at the syntax of new operator, when used for single objects/ variables and when used for arrays.
Syntax Of The New Operator In C++
data_type *pointer_variable = new dataType; // For single variables
data_type *pointer_variable = new dataType[size]; // For arrays
Here,
- The term data_type refers to the type of data the pointer variable, and dataType refers to the type of data we will store in the variable/ location being pointed to. Ideally, both of these should be the same.
- The term pointer_variable refers to the name we give to the pointer variable that will store the address of the newly allocated memory. (The asterisk symbol (*) is the pointer notation.)
- In the second line of syntax, size refers to the number of elements the array will contain, and square brackets [] indicate the array notation.
Example- The snippet below showcases the implementation of the new operator/ new keyword in C++:
int *ptr = nullptr; // Pointer initialization expression with NULL
ptr = new int; // Then request memory for the variableOR
// Combine declaration of pointer and their assignment
int *ptr = new int;
The code above first allocates memory to a number or nullptr on the heap and stores the address of the memory in the pointer variable ptr. We can then use ptr to enter and replace numbers.
The use of a new operator to initialize value or create a memory block is demonstrated in the C++ program snippet below.
// Integer value initialized with value 21
int *p = new int(21);// Create an array of size 10.
int *x = new int[10];
Let's look at a full code example of the C++ new operator.
Code Example:
#include <iostream>
using namespace std;
int main() {
// Dynamically allocate an array of 5 integers
int* arr = new int[5];
// Initialize the array with values
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
// Print the array values
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Deallocate the array memory
delete[] arr;
return 0;
}
Output:
1 2 3 4 5
Explanation:
In the above code example-
- We include the <iostream> header file to use input/output operations. We also use the namespace std; statement to avoid writing std:: before every standard library element.
- Inside the main() function, we first dynamically allocate memory to an array using the new operator. Here:
- The new operator allocates memory for the an integer array type containing 5 values (integers), i.e., new int[5].
- In the same line, we assign this memory location to the integer pointer arr, i.e., int* arr. (It will point to the first element of the array)
- We then use a for loop to initialize the array elements with values from 1 to 5.
- Next, we use another for loop to print the values of the array elements.
- Finally, we deallocate the memory allocated for the array using the delete[] operator to avoid memory leaks.
Return Value Of New Operator In C++
The new operator in C++ returns a pointer to the heap memory, where dynamically allocated objects are stored.
- The type of the pointer returned depends on the type of the object being allocated. For example, if you're allocating an object of type int, the new operator will return a pointer of type int*.
- If the memory allocation fails, the new operator throws a std::bad_alloc exception by default. This behavior can be changed using the nothrow keyword, which causes the new operator to return a null pointer instead of throwing an exception.
Parameters Of New Operator In C++
- Count: This is an optional parameter that specifies the number of bytes to which memory will be allocated. The default value is 1.
- ptr: This parameter is used to specify the address of memory allocation where the new object should be placed.
- tag: This parameter (optional) is used to specify the allocation.
- alignment(al): This parameter (optional) is used to specify the alignment requirement to be used for allocating memory.
Check out this amazing course to become the best version of the C++ programmer you can be.
Example To Understand New Operator In C++
Let's understand the functionality of the new operator in C++ with the help of a code example.
Code Example:
#include <iostream>
class MyClass {
public:
MyClass() {
std::cout << "Constructor called!" << std::endl;
}
~MyClass() {
std::cout << "Destructor called!" << std::endl;
}
void doSomething() {
std::cout << "Doing something!" << std::endl;
}
};
int main() {
// Create a new object using the new operator
MyClass* obj = new MyClass();
// Call a static member function of the object
obj->doSomething();
// Delete the object when it's no longer needed
delete obj;
return 0;
}
Output:
Constructor called!
Doing something!
Destructor called!
Explanation:
- In this example, we define a simple class named MyClass with a constructor, descructor and member function:
- The constructor MyClass() is called automatically when a class object is created. It also prints "Constructor called!" to indicate that the object initialization has taken place.
-
- Then, we define destructor ~MyClass() called automatically for cleanup tasks, when an object is destroyed. It also prints "Destructor called!" to show when the object is being cleaned up.
- Next, we have a member function doSomething() which used std::cout to print a message- "Doing something!" to indicate some action is being performed by the object.
- Finally, the main() function return 0 to indicate that the program has been executed successfully.
The Grammar Elements Of The New Operator In C++
The new operator in C++ has the following grammar elements:
New Placement: This optional parameter specifies where the new object should be created in memory. This is an advanced feature primarily used in low-level memory management. For Example:
int arr[2] = { 1, 2 };
int* ptr = new (arr) int[2];
- type-id: It is a set of identifiers that allocates the object. It can be used in many contexts, such as notifications, dumps, etc. For Example:
int x = 5;
std::cout << typeid(x).name() << std::endl; // Output: i
- The new initializer: In this, the new operator allocates dynamic memory for the object and can be used to initialize the object at the same time. For Example:
int* p = new int(21); // initialize a single integer with value 21
- The noptr new declarator: This is a syntax used to create a new object or an array of objects with explicit constructor parameters. For Example:
int *ptr = new (std::nothrow) int[5]{1, 2, 3, 4, 5};
Storage Space Allocation
Allocated Storage refers to dynamic storage allocation or memory that is dynamically allocated at runtime using the new operator in C++. This memory is allocated on the heap, which is an area of memory reserved by a group allocating local variables.
When using the new operator to allocate memory, the compiler stores the requested memory size on the heap and returns a pointer to the first byte of the block. The size of the memory block depends on the size of the built-in data type being allocated, which can be determined at runtime.
Throwing Allocation
If memory cannot be allocated for the object, the new operator in C++ throws an exception, i.e., exception throw. This is called a "returning assignment" or a "review". By default, the new operator throws a std::bad_alloc exception if memory allocation fails. This exception can be caught using a try-catch block. Below is an example of the same.
Code Example:
#include <iostream>
#include <new>
class MyClass {
public:
MyClass() {
std::cout << "Constructor called!" << std::endl;
}
~MyClass() {
std::cout << "Destructor called!" << std::endl;
}
};
int main() {
try {
// Attempt to allocate memory using throwing new
MyClass* obj = new MyClass();
// Memory allocation succeeded
std::cout << "Memory allocation successful!" << std::endl;
// Use the object
// ...
// Delete the object when it's no longer needed
delete obj;
} catch (const std::bad_alloc& e) {
// Memory allocation failed
std::cerr << "Memory allocation failed: " << e.what() << std::endl;
}
return 0;
}
Output:
Constructor called!
Memory allocation successful!
Destructor called!
Explanation:
In this code example-
- We define a class named MyClass with a constructor and a destructor, just like in the previous example.
- Inside the main() function, we use a try-catch block to handle potential memory allocation exceptions.
- In the try block, we attempt to allocate memory for a MyClass object using the new operator with the nothrow keyword.
- If the allocation succeeds, a valid pointer to the object is returned, and we print a message to the console. Finally, we delete the object to deallocate the memory using the delete operator.
- However, if the memory allocation fails due to insufficient memory, a std::bad_alloc exception is thrown.
- We catch this exception in the catch block and print an error message to the console, indicating that memory allocation failed followed by the exception's description using e.what().
The Nothrow Allocation
In this type of memory allocation, we use the nothrow expression of the new operator. It functions like regular dynamic memory allocation but with a key difference in error handling. If the allocation fails due to insufficient memory, it returns a nullptr instead of throwing a std::bad_alloc exception.
Code Example:
#include <iostream>
#include <new>
class MyClass {
public:
MyClass() {
std::cout << "Constructor called!" << std::endl;
}
~MyClass() {
std::cout << "Destructor called!" << std::endl;
}
};
int main() {
// Attempt to allocate memory using nothrow new
MyClass* obj = new (std::nothrow) MyClass();
if (obj != nullptr) {
// Memory allocation succeeded
std::cout << "Memory allocation successful!" << std::endl;
// Use the object
// ...
// Delete the object when it's no longer needed
delete obj;
} else {
// Memory allocation failed
std::cerr << "Memory allocation failed!" << std::endl;
}
return 0;
}
Output:
Constructor called!
Memory allocation successful!
Destructor called!
Explanation:
Just like in the previous two examples, we define a class MyClass with a constructor and destructor, but no member function.
- Inside the main() function, we we create obj, an object of MyClass and use the new operator with std::nothrow keyword to allocate memory to it.
- This keyword ensures that if the memory allocation fails, it does not throw an exception but instead returns a null pointer.
- Then, we use an if-else statement to check if the allocation was successful by using the inequality relational operator to see if obj is not null.
- If the condition is true, it means allocation successful, and the if-block is executed where the new operator returns a valid pointer to the object.
- As shown in the commented lines in the code, we can use this object as required and then release the allocated memory using delete obj.
- If the condition is false, it means allocation failed, then the control moves to the else block.
- The new operator in this case returns a null pointer (nullptr) and we print an error message to the console.
-
Finally, the program returns 0, indicating successful execution.
New Placement In C++
Placement new is different from the regular new operator in C++. It allows the programmer to specify a particular location in memory where an object should be constructed.
- Unlike the regular new, placement new does not allocate memory. Instead, it uses pre-allocated memory provided by the programmer.
- To use placement new, you need to provide a pointer to the pre-allocated memory where you want the object to be constructed. You also need to include the header <new> to use the placement new operator.
Code Example:
#include <iostream>
class MyClass {
public:
MyClass(int value) : value(value) {
std::cout << "Constructor called! Value: " << value << std::endl;
}
~MyClass() {
std::cout << "Destructor called! Value: " << value << std::endl;
}
void setValue(int newValue) {
value = newValue;
}
void printValue() const {
std::cout << "Value: " << value << std::endl;
}
private:
int value;
};
int main() {
// Allocate memory for an object at a specific address
void* memory = operator new(sizeof(MyClass));
// Construct an object at the allocated memory using placement new
MyClass* obj = new(memory) MyClass(42);
// Call member functions on the constructed object
obj->printValue();
obj->setValue(99);
obj->printValue();
// Destruct the object and deallocate the memory
obj->~MyClass();
operator delete(memory);
return 0;
}
Output:
Constructor called! Value: 42
Value: 42
Value: 99
Destructor called! Value: 99
Explanation:
In this example,
- We define a MyClass class with a constructor, destructor, a setValue() method to modify the value, and a printValue() method to print the value.
- It also contains a private data member value (integer type), meaning it can only be accessed by components of the class.
- In the main() function, we first allocate raw memory for an object of MyClass using operator new.
- Next, we use placement new to construct an object of type MyClass at the specified memory location, passing the constructor argument 42.
- We then call printValue() to display the initial value, change the value to 99 using setValue(99), and print the updated value again using printValue().
- After using the object, we explicitly invoke the destructor with obj->~MyClass() to destroy the object and print the message to the console.
- Finally, we deallocate the raw memory using operator delete(memory).
How Does The C++ New Operator Works?
The new operator is used to allocate memory dynamically at runtime. When you use the new keyword in C++ to allocate memory, the following steps occur:
- The new operator first evaluates the type of the created object and decides how much memory to allocate.
- It then tries to allocate a linked memory of the desired size from the heap. If the memory allocation request is successful, a pointer to the first byte of the block is returned. If the allocation fails, a bad_alloc exception is thrown.
- If the allocation is successful, the new operator calls the object's constructor and initializes the memory block using the default constructor or the initializer constructor as specified in the new instructions.
- Finally, the new operator returns a pointer to the newly created object, which then can be used to access and modify objects stored in this memory.
When you allocate new memory, you need to deallocate it using the delete operator when it is no longer needed. Failure to do so can cause a memory leak where memory is allocated but not released, causing performance issues and potentially causing the program to run out of memory.
What Happens When Enough Memory In The Program Is Not Available?
When there is not enough memory available in a program to fulfill a memory allocation request, the undefined behavior will depend on the memory allocation function being used.
- New operator: If the new operator is used for an allocation (new type), an exception is thrown for std::bad_alloc when the memory allocation fails. You can catch this exception using a try-catch block to handle the error in your program.
- new(std::nothrow) operator: If you use the new(std::nothrow) operator to allocate memory and the allocation fails, it returns a nullptr instead of throwing an exception. You can check whether the allocation succeeded by verifying if the pointer is nullptr.
- Malloc Function: The malloc function in C returns a pointer (NULL) when memory allocation fails. It doesn't throw an exception. It is important to note that the direct use of the malloc() function is deprecated in C++. You should hence prefer to use a new operator instead.
Initializing Objects Allocated With New Operator In C++
Objects are allocated using the new operator and can be initialized using a variety of syntaxes. One way to instantiate an object is to use the constructor class. Below is an example of how to use the constructor to initialize an object with C++ new keyword.
Code Example:
#include <iostream>
class Box {
public:
Box(int value) : m_value(value) {} // Constructor to initialize m_value
void printValue() {
std::cout << m_value << std::endl; // Output the value
}
private:
int m_value; // Data member to store the value
};
int main() {
Box* ptr = new Box(42); // Allocate memory using new and initialize with constructor
ptr->printValue(); // Prints 42
delete ptr; // Deallocate memory
return 0;
}
Output:
42
Explanation:
In this example-
- We define a class named Box with a private data member m_value to store an integer value and public member functions Box(int value) and printValue().
- In the Box class, we define a constructor Box(int value) that takes an integer argument and initializes the private data member m_value with the provided value.
- We also define a member function printValue() that outputs the value of m_value using std::cout statement.
- In the main() function, we dynamically allocate memory for an object of the Box class using the new operator. This creates an instance of the class and calls the constructor with the value 42, which is stored in m_value. The pointer ptr holds the address of this newly created object.
- We then call the printValue() function using ptr->printValue(), which dereferences the pointer and prints the value 42 to the console.
- After printing the value, we deallocate the memory using delete ptr to free up the dynamically allocated memory and avoid memory leaks.
Lifetime Of Objects Allocated With The New Operator In C++
In C++, the lifetime of an object allocated using the new operator is determined by the programmer. This is in contrast to objects with automatic or static bytes of storage duration, which have their lifetime determined by the program structure.
When an object is dynamically allocated using new, it remains until explicitly declared using the delete operator. This means that the object can be extended beyond the point at which it was created, allowing it to be used by other parts of the program.
Code Example:
#include <iostream>
int main() {
int* ptr = new int(42); // Dynamically allocate memory
std::cout << *ptr << std::endl; // Output the value
delete ptr; // Free the allocated memory
return 0;
}
Output:
42
Explanation:
In this C++program-
- In the main() function, we dynamically allocate memory for an integer variable with the value of 42 using new operator.
- In the same line, we assign the address of the allocated valid memory location (where int is stored) to the pointer variable ptr.
- We then print the value to the console using std::cout command and the dereference operator (*) to access the integer value stored at the location ptr.
- Finally, we free the memory using the delete operator to avoid memory leaks.
Since objects are constantly allocated with new ones until they are released, it is important to ensure that all dynamically allocated objects are deallocated so that they can be removed when not needed.
What Is The Delete Operator In C++?
The delete operator is used to free up the dynamically allocated memory, that was previously allocated using the new operator in C++. When memory is allocated using new, it remains allocated until explicitly freed by the delete operator. Failing to deallocate memory will result in memory leaks, where the program consumes more and more memory over time without releasing it.
The Syntax For The Delete Operator
delete pointer_variable;
Here,
- pointer_variable is the name of the pointer that points to the newly allocated memory.
- The delete keyword marks the delete operator, which, when called, frees up the allocated memory. This memory can then be reused by the program or returned to the operating system.
Example:
int* ptr = new int;
*ptr = 21;
std::cout << "The value of the integer is: " << *ptr << std::endl;// Deallocate the memory when it is no longer needed
delete ptr;
The delete keyword here is used to deallocate memory that was allocated using the new operator in C++. If you forget to deallocate memory or use the wrong deallocation operator, you can end up with memory leaks or other problems.
Difference Between New And Delete Operator In C++
The new and delete operators are used for dynamic memory allocation and deallocation in C
++, respectively. Below are some key differences between the two operators:
Definition |
New operator |
Delete operator |
Usage |
The new operator is used to dynamically allocate memory for an object or array. |
The delete operator is used to free up the allocated memory. |
Syntax |
The new operator is followed by the user-defined data type of the object. |
The delete operator is followed by the memory address to be deallocated. |
Return type |
The new operator returns a pointer to the allocated memory. |
The delete keyword returns nothing. |
Size allocation |
The new operator is used to allocate a certain size of memory, which depends on the file type and the allocated object. |
The delete expression frees exactly the same memory allocated by the new. |
Handling of null pointers |
The new operator in throws an exception to std::bad_alloc if it fails to allocate the requested memory. |
The delete expression can be used safely with a null pointer without causing any errors. |
Looking for mentors? Find the perfect mentor for select experienced coding & software experts here.
Conclusion
The new operator in C++ is used to allocate memory at runtime. It allows you to create objects whose lifetime is independent of their declaration and allocate memory for arrays of objects whose size is unknown at the run time. The new operator in C++ returns a pointer to the new memory that can be used to access and modify objects stored in that memory.
It is important to properly manage the memory with the new, leaving the memory to the delete operator when it is not needed to avoid memory leaks. The new operator in C++ can also be used to instantiate an object in the constructor class and allocate sufficient memory using the operator new function without calling the constructor.
Frequently Asked Questions
Q. What is the delete operator in C++?
The delete operator is used to free up dynamically allocated memory that was previously allocated using the new operator. In other words, when you use the new operator to allocate memory to an object or a set of objects, you must free that memory when you are finished using it. This is where the delete operator comes into play.
For example:
int* ptr = new int;// Allocate memory for a single integer using new
delete ptr; // Free the memory using delete
Q. When to do dynamic memory allocation?
Dynamic memory allocation is a computer programming technique that allows programs to allocate and free memory at runtime. In other words, dynamic allocation is a way for programs to request for memory allocation from the operating system as needed rather than being limited to a fixed amount of memory at collection time. When allocating a multidimensional array, all dimensions except the first must be constant expressions that evaluate positive values.
Dynamic memory allocation is often used in programming languages such as C and C++, where the memory needs to be allocated and deallocated for the variables and for the data structures such as arrays, names, and trees. This is usually done using functions like malloc() and free() in C or the new and delete operators in C++.
Q. What is the new operator in C++?
The new operator is a keyword in the C++ programming language used to allocate memory for an object at runtime. It is often used to allocate memory for objects on the heap, which is a large pool of memory managed by a function.
The new operator is often used with a default constructor to initialize an allocated object. For example, if you want to allocate memory for an integer variable named "number", you can use the new operator as follows:
int* number = new int(20);
Q. Give an example of the new operator for a string in C++?
The block of code below showcases the use of C++ new keyword for string:
// Allocate a new string on the heap using the new operator
string* myString = new string("Hello, world!");// Free up the memory used by the string using the delete operator
delete myString;
In this example, we allocate a string object named myString on the heap using the new operator and call it Hello, world! We start with the value and then use the asterisk operator (*) to dereference the pointer and print the value of the string. Finally, we use the delete operator to free up the allocated memory used by the string.
Q. What is the difference between operator new and new operator?
- The operator new is a low-level function provided by the C++ standard library to allocate raw memory from the heap. It takes the size of the memory block to be allocated as its argument and returns a pointer to the beginning of the allocated block. Essentially, it just allocates memory but does not initialize it. The default operator new can be overridden by the user to provide custom memory allocation behavior.
- On the other hand, new operator, is a higher-level construct in the C++ language used to allocate memory for an object and invoke its constructor to initialize the object. The new operator internally calls operator new to allocate memory and then proceeds to construct the object in that allocated memory by calling the appropriate constructor.
Q. Can a new operator in C++ return null?
By default, the new operator in C++ does not return nullptr when memory allocation fails. Instead, it throws a std::bad_alloc exception. However, C++ provides a way to handle memory allocation failures more gracefully by using the not-throwing variant of new with std::nothrow. In this case, if memory allocation fails, the new operator will return nullptr rather than throwing an exception.
For Example:
int* ptr = new(std::nothrow) int; // Returns nullptr if allocation fails
if (!ptr) {
std::cout << "Memory allocation failed" << std::endl;
}
Here, new(std::nothrow) returns nullptr on failure.
Test Your Skills: Quiz Time
You might also be interested in reading the following:
- Typedef In C++ | Syntax, Application & How To Use It (With Examples)
- Difference Between C And C++| Features | Application & More!
- 51 C++ Interview Questions For Freshers & Experienced (With Answers)
- Find In Strings C++ | Examples To Find Substrings, Character & More!
- Data Types In C++ | A Detailed Explanation With 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 2 days ago
YASH SRIVASTAVA 1 month ago
Sanchit Dhale 1 month ago