Inheritance and polymorphism will affect the coupling of the class: Inheritance will increase the coupling because the derived class depends on the base class. Polymorphism reduces coupling because objects can respond to messages in a consistent manner through virtual functions and base class pointers. Best practices include using inheritance sparingly, defining public interfaces, avoiding adding data members to base classes, and decoupling classes through dependency injection. A practical example showing how to use polymorphism and dependency injection to reduce coupling in a bank account application.
The impact of inheritance and polymorphism on class coupling in C++
Introduction
Inheritance and polymorphism are key concepts in C++ that promote code reusability and flexibility. However, they can also have a significant impact on the degree of coupling of a class.
Coupling degree
Coupling degree measures the degree of dependence between classes. High coupling means that classes are closely related, and modifications to one class may affect another class.
Inheritance and coupling
Inheritance creates a subordination relationship between parent and child classes. A derived class depends on the base class because any changes in the base class may affect the derived class. This dependency increases coupling.
Example:
class Animal { public: virtual void speak(); }; class Dog : public Animal { public: void speak() override; }; class Cat : public Animal { public: void speak() override; };
In this example, the Dog
and Cat
classes inherit from Animal
kind. If the speak
function of the Animal
class is modified, the Dog
and Cat
classes will also need to be modified. This dependence results in a high degree of coupling.
Polymorphism and Coupling
Polymorphism allows objects to respond to different messages in a consistent way, even if they belong to different classes. This can be achieved through virtual functions and base class pointers.
Example:
class Shape { public: virtual double area(); }; class Rectangle : public Shape { public: double area() override; }; class Circle : public Shape { public: double area() override; }; int main() { Shape* shapes[] = {new Rectangle(), new Circle()}; for (Shape* shape : shapes) { cout << shape->area() << endl; } }
In this example, the Rectangle
and Circle
classes both inherit from Shape
kind. The main
function uses polymorphism to call the area
function for each shape object without knowing their specific type. This decoupling allows modification of the Rectangle
or Circle
class without modifying the main
function.
Best Practices
To reduce coupling, the following best practices should be followed:
Practical case:
Suppose we have a bank account application that contains the Account
base class and CheckingAccount
and SavingsAccount
Derived class. To reduce coupling, we can use polymorphism and dependency injection.
class Account { public: virtual double getBalance(); }; class CheckingAccount : public Account { public: double getBalance() override; }; class SavingsAccount : public Account { public: double getBalance() override; }; class Customer { private: Account* account; public: Customer(Account* account) : account(account) {} double getBalance() { return account->getBalance(); } }; int main() { CheckingAccount checkingAccount; SavingsAccount savingsAccount; Customer checkingCustomer(&checkingAccount); Customer savingsCustomer(&savingsAccount); cout << checkingCustomer.getBalance() << endl; cout << savingsCustomer.getBalance() << endl; }
By using dependency injection, the Customer
class is decoupled from a specific account type. It just needs to know how to call the getBalance
function. This allows new types of accounts to be easily added without modifying the Customer
class.
The above is the detailed content of How do inheritance and polymorphism affect class coupling in C++?. For more information, please follow other related articles on the PHP Chinese website!