C++ is an object-oriented programming (OOP) language that is considered as the best language for creating large-scale applications. C++ is a superset of the C language.
C++ programming is a powerful, high-performance programming language that has played an important role in developing many modern software applications.
Features:
C++ is considered a superset of c programming. The main differences between C and C++ are:
In C++, Access modifiers or access specifiers are the keywords in object-oriented languages. It helps to set the accessibility of classes, methods, and other members.
There are 3 types of access modifiers in C++
Public Access Modifier: Members declared as public can access the program from anywhere, including outside the class. This is useful for functions and data that need to be widely available.
Private Access Modifier: Members declared as private are accessible only within the class in which they are defined. No external functions or classes can directly access private members, which is essential for protecting sensitive data.
Protected Access Modifier: Members declared protected can be accessed within the class and by derived (inherited) classes. This modifier is particularly useful in inheritance scenarios where you want to allow subclasses to access certain members while restricting access from outside the inheritance hierarchy.
Feature |
Pointer |
Reference |
definition |
A variable that holds the memory address of another variable. |
An alias/Alternative name for an existing variable. |
Memory Address |
Needs additional memory to store the address. |
Generally, it utilizes less memory compared to pointers. |
Reassignment |
It can be reassigned to point to another variable at any time. |
It cannot be reassigned to reference another variable after initialization. |
Use Cases |
Useful for dynamic memory allocation and data structures like linked lists. |
Typically used for function parameters to avoid copying large objects. |
Nullability |
Can be null, indicating that it points to no valid object. |
Cannot be null; must always refer to a valid object. |
In C++, Object-Oriented Programming (OOP) Relates to several core concepts that improve code reusability, modularity, and flexibility. These key OOP concepts include:
An object is an instance of a class with its own identity and behaviour.
Encapsulation is also a part of OOPs' concept. It refers to the bundling of data with the methods that operate on that data. It also helps to restrict any direct access to some of an object’s components.
Abstraction is an OOP concept to build the structure of real-world objects. It “shows” only essential attributes and “hides” unnecessary information from the outside. The main focus of abstraction is to hide the unnecessary details from the users. It is one of the most important concepts of OOPs.
Inheritance is the mechanism by which one class (the derived class) inherits properties and behaviours (data members and methods) from another class (the base class). This promotes code reuse and hierarchical relationships between classes.
Polymorphism is one of the most used and the core concepts in OOP languages. It explains the concept of different classes can be used with the same interface. Each of these classes can have its own implementation of the interface
In C++, two popular options for storing data are arrays and lists. While both serve the same purpose, their structure, size, and usage differ significantly.
Arrays in C++:
The size defined in the declaration cannot grow/shrink dynamically
Lists in C++ (Linked Lists):
Dynamic data structure with elements stored in nodes
Choosing Between Arrays and Lists:
Lists: Ideal for dynamic memory management, insertion/deletion, and storing elements of different data types
C++ Destructor:
A C++ destructor is a special member function that is automatically called when an object is deleted or goes out of scope. Its primary duty is to deallocate the object's memory and perform any necessary cleanup.
Rules for C++ Destructors: