C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language, incorporating object-oriented features.
C++ supports several basic data types, including:
int
- Integer typefloat
- Single precision floating pointdouble
- Double precision floating pointchar
- Character typebool
- Boolean typeA class in C++ is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables), and implementations of behavior (member functions or methods).
A class is a blueprint or template for creating objects, whereas an object is an instance of a class.
Inheritance is a feature of C++ that allows a new class (derived class) to inherit properties and behavior from an existing class (base class).
Polymorphism in C++ refers to the ability of a function, object, or method to take many forms, typically through the use of inheritance and virtual functions.
Virtual functions in C++ are member functions in a base class that can be overridden in derived classes to provide polymorphic behavior.
A constructor is a special member function of a class that is executed whenever new objects of that class are created, used to initialize the object’s state.
A destructor is a special member function of a class that is executed when an object of that class is destroyed, used to free resources allocated by the object.
Operator overloading allows C++ operators to have user-defined meanings when applied to user-defined types (classes).
public
: Members are accessible from outside the class.private
: Members are only accessible within the class itself.protected
: Members are accessible within the class and by derived class members.A template in C++ is a feature that allows functions and classes to operate with generic types, making them more versatile and reusable.
There are two types of polymorphism in C++:
The `this` pointer is an implicit pointer available in member functions, pointing to the object for which the function is called.
A namespace in C++ is a declarative region that provides a scope to the identifiers inside it, preventing name conflicts in large programs.
`new` is a C++ operator that allocates memory and calls constructors, while `malloc` is a C function that only allocates memory without calling constructors.
An abstract class in C++ is a class that cannot be instantiated on its own and usually contains at least one pure virtual function.
Some key differences include:
The `friend` keyword in C++ allows a non-member function or another class to access the private and protected members of the class in which it is declared.
C++ provides a mechanism for exception handling using `try`, `catch`, and `throw` blocks to handle runtime errors and other exceptional conditions.
Stack memory is used for static memory allocation (like local variables), while heap memory is used for dynamic memory allocation.
A pointer is a variable that stores the memory address of another variable.
A reference is an alias for another variable, providing an alternative name for the variable.
A lambda expression is an anonymous function that can be used to create inline functions.
RAII (Resource Acquisition Is Initialization) is a programming idiom in C++ that ties resource management to object lifetime.
Smart pointers are objects that act as pointers but automatically manage the memory of the object they point to, preventing memory leaks.
`delete` is used to deallocate memory for a single object, while `delete[]` is used to deallocate memory for an array of objects.
Typecasting is the conversion of one data type to another, either implicitly or explicitly.
Move semantics in C++ allow the resources of a temporary object to be moved to another object, improving performance by avoiding unnecessary copying.