C++ Object Arrays, Nested Classes, and Polymorphism
Arrays of Objects in C++
Arrays are fundamental data structures that store collections of elements. An array of objects in C++ is a sequence of objects of the same class. Each object in the array has its own attributes and methods.
Example:
const students = [
{ name: "Alice", age: 20, grade: "A" },
{ name: "Bob", age: 22, grade: "B" },
{ name: "Charlie", age: 21, grade: "A" }
];
console.log(students[0].name); // Output: Alice
console.log(students[1].age); // Output: 22
console.log(students[2].grade); // Output: A
Nested Classes
A nested class is declared inside another (enclosing) class. It has the same access rights as other members, but enclosing class members don’t have special access to nested class members; standard access rules apply.
Example:
#include <iostream>
using namespace std;
class Enclosing {
private:
int b;
class Nested {
int y;
void NestedAccess(Enclosing *a) {
cout << "Nested Class " << a->b;
}
};
};
int main() {}
Constant and Static Member Functions
A constant member function doesn’t modify the object’s state. Declare it with const
after the function declaration. It can be called on constant and non-constant objects.
A static member function belongs to the class itself, not to any specific object. It can access only static members of the class.
The this Pointer in C++
The this
pointer is an implicit pointer passed to non-static member functions. It points to the current object invoking the function. Uses:
- Accessing current object’s members.
- Returning the current object.
- Distinguishing between local and member variables with the same name.
Nameless Temporary Objects
A nameless temporary object (rvalue) is created on the fly, used, and discarded. It has no name and a temporary lifetime, existing during the expression’s evaluation. Examples: 3 + 4
, std::string("temp")
, MyClass()
.
Overloading Unary Operators
Unary operators can be overloaded to work with user-defined types (classes). Example: overloading ++
and --
for a complex number class.
Abstract Classes
An abstract class can’t be instantiated directly. It serves as a blueprint for subclasses. It may contain abstract methods (without implementation) that subclasses must implement.
Exception Handling
Exception handling manages errors during program execution. try
blocks enclose code that might throw exceptions. catch
blocks handle specific exception types.
Class Templates
Class templates allow defining classes generically, working with various data types without rewriting code. Use the template
and typename
(or class
) keywords.
Function Template Overloading
Function templates can be overloaded like regular functions, providing different implementations based on parameter types and counts.
Random File Access
Functions like fseek()
, ftell()
, and rewind()
enable random access within files, moving the file pointer to specific positions.
Reading from Binary Files
The read()
member function reads data from a binary file. Use fread()
in C to read binary data into structures.
Constructor Characteristics
Constructors initialize objects. They have the same name as the class, no return type, are invoked automatically upon object creation, can be overloaded, and initialize instance variables.
Constructor/Destructor Order in Inheritance
- Single Inheritance: Base constructor then derived constructor. Derived destructor then base destructor.
- Multilevel Inheritance: Topmost base constructor down to most derived. Most derived destructor up to topmost base.
- Multiple Inheritance: Base constructors in declaration order, then derived constructor. Derived destructor, then base destructors in reverse declaration order.
Returning Objects from Functions
Objects can be returned from functions like other variables. No special keywords are needed.
Polymorphism
Polymorphism allows performing a single action in multiple ways. Types:
- Compile-time (Static): Resolved at compile time (e.g., method overloading).
- Run-time (Dynamic): Resolved at run time (e.g., method overriding).
- Subtype: Superclass variable refers to subclass object.
- Parametric: Generic functions/data types (templates).