modular: (of machines, buildings, etc.) consisting of separate parts or units that can be joined together
Inheritance in C++ is one of the core pillars of Object-Oriented Programming (OOP). It allows you to create new classes (called derived classes) based on existing ones (called base classes). This helps you reuse code, establish relationships between classes, and extend functionality without rewriting everything from scratch.
attribute: a quality or feature of somebody/somethingIn C++, a data member is simply a variable that belongs to a class. It represents the attributes or properties of the objects created from that class.
A member function in C++ is a function that belongs to a class. It defines the behavior of the objects created from that class, while data members define the state (attributes).
ammo: ammunition (= a supply of bullets, etc. to be fired from guns)
hindsight: the understanding that you have of a situation only after it has happened and that means you would have done things in a different way
In C++, the phrase virtual void is used when declaring a virtual function that returns nothing (void).
Virtual functions are a cornerstone of runtime polymorphism—they allow the program to decide at runtime which version of a function to call, depending on the actual type of the object being pointed to, not just the type of the pointer.
Declared in a base class using the keyword virtual.
Can be overridden in a derived class.
Enable dynamic dispatch (late binding).
If a class has virtual functions, its destructor should also be virtual to ensure proper cleanup when deleting derived objects through base pointers.
A destructor in C++ is a special member function of a class that is automatically called when an object goes out of scope or is explicitly deleted. Its main purpose is to release resources (like memory, file handles, or network connections) that the object may have acquired during its lifetime.
In C++, a protected member is a class member (data or function) that sits in between private and public in terms of accessibility.
Accessible inside the class where it is defined.
Accessible in derived (child) classes.
NOT accessible from outside the class hierarchy (unlike public).
So, protected is mainly useful when you want to hide details from the outside world but still allow subclasses to use or extend them.
Polymorphism in C++ is one of the most powerful features of Object-Oriented Programming (OOP). The word literally means “many forms”, and in programming it allows the same function, operator, or method name to behave differently depending on the context.
Function overloading allows multiple functions with the same name but different parameter lists within the same scope.
Function overriding allows a derived class to provide a specific implementation of a method already defined in its base class.
A parameter list appears inside the parentheses of a function declaration or definition.
void greet(string name, int age);
Parameter Type: Specifies the data type (string, int, etc.)
Parameter Name: Variable name used inside the function (name, age)
In C++, access specifiers determine the visibility and accessibility of class members (variables and functions). They are essential for implementing encapsulation — one of the core principles of object-oriented programming.
Types of access specifiers in C++ include Public, Private and Protected.
A virtual function is a member function in a base class that you expect to override in derived classes. It’s declared using the virtual keyword.
In C++, const override is used when a virtual function in a derived class overrides a const virtual function from a base class. It ensures both const correctness and runtime polymorphism.
In C++, polymorphism and pointers work together to enable runtime polymorphism, allowing you to call derived class methods through base class pointers. This is a powerful feature of object-oriented programming that supports flexible and extensible code design.
instance: a particular example or case of something
The declaration std::vector<Item*> in C++ defines a dynamic array of pointers to Item objects.
inventory: The name of the vector.
inventory is a container that holds pointers to Item objects, not the objects themselves.
An abstract class in C++ is a class that serves as a blueprint for other classes but cannot be instantiated directly. It’s mainly used to define a common interface for derived classes.
An abstract class is created when at least one pure virtual function is declared.
A pure virtual function is a virtual function with = 0 at the end of its declaration.
You cannot create objects of an abstract class, but you can use pointers or references to it.
Any class that inherits from an abstract class must implement all pure virtual functions, otherwise it also becomes abstract.
A pure virtual class in C++ is essentially the same as an abstract class—it’s a class that contains at least one pure virtual function. Such a class cannot be instantiated directly and is meant to serve as a base class for other classes to implement specific behavior.
Inheritance is the mechanism by which one class (child/derived) acquires properties and behaviors of another class (parent/base). It promotes code reusability and establishes a relationship between classes.
class A {
public:
void greet() {
cout << "Hello from A!" << endl;
}
};
class B : public A {
// B inherits greet() from A
};
Polymorphism allows objects of different classes to be treated as objects of a common base class, typically through virtual functions. It enables flexibility and dynamic behavior.
class Base {
public:
virtual void show() {
cout << "Base class" << endl;
}
};
class Derived : public Base {
public:
void show() override {
cout << "Derived class" << endl;
}
};
Base* ptr = new Derived();
ptr->show(); // Outputs: Derived class
Base* ptr: Declares a pointer to the base class Base.
new Derived(): Creates an object of the derived class Derived.
ptr->show(): Calls the show() method using the base class pointer
Microsoft Copilot
沒有留言:
發佈留言