0% found this document useful (0 votes)
14 views5 pages

Inheritance

Inheritance

Uploaded by

k13913703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Inheritance

Inheritance

Uploaded by

k13913703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

In *The C++ Programming Language* by Bjarne Stroustrup, **inheritance** is described as a mechanism

by which one class (called the derived or child class) can acquire the properties and behaviors (data
members and member functions) of another class (called the base or parent class). This enables the
creation of a hierarchical relationship between classes, promoting code reuse and modularity.

Inheritance in Classes:
In C++, inheritance allows a derived class to inherit members from a base class. The derived class can use
or override these members and may also introduce additional members specific to itself.

Real-World Example:
Consider the relationship between a **Vehicle** and a **Car**:

- **Base Class**: Vehicle (which has general attributes like speed, weight, and methods like move (),
stop ()).

- **Derived Class**: Car (which inherits from Vehicle and adds specific attributes like the number of
doors, model type, and methods like playMusic ()).

Code Example:
class Vehicle {

public:

int speed;

void move () {

// code to move the vehicle

void stop () {

// code to stop the vehicle

};

class Car: public Vehicle {

// Car is derived from Vehicle

public:
int numberOfDoors;

void playMusic () {

// code to play music

};

In this example, the `Car` class inherits the `speed`, `move () `, and `stop () ` from the `Vehicle` class and
adds its own specific functionality.

Graphical Representation of Inheritance:


In UML diagrams, inheritance is depicted as a solid line with a hollow arrowhead pointing from the
derived class to the base class.

```

+---------------------+

| Vehicle | <-- Base Class

+---------------------+

| speed: int | <-- Attributes (Data Members)

+---------------------+

| move (): void | <-- Methods (Member Functions)

| stop (): void |

+---------------------+

+---------------------+

| Car | <-- Derived Class

+---------------------+

| numberOfDoors: int | <-- Additional Attributes

+---------------------+

| playMusic (): void | <-- Additional Methods


+---------------------+

"Is-a" or "Is-a-kind-of" Relationship:


Inheritance models a relationship where the derived class **"is a"** type of the base class. This
expresses that a car **"is a"** kind of vehicle, or a square **"is a"** kind of rectangle.

- Vehicle is the general concept.

- Car is a specialized type of Vehicle (Car "is a" Vehicle).

Advantages of Inheritance:
1. Code Reusability: The derived class inherits properties and behaviors from the base class, allowing
shared functionality to be written once in the base class and reused across many derived classes.

2. Modularity: Changes made in the base class (like fixing a bug or improving functionality) are
automatically inherited by the derived classes, making maintenance easier.

3. Extensibility: You can extend the base class with new functionality in derived classes without altering
the original base class.

4. Polymorphism: Through inheritance, C++ allows objects to be treated as instances of their parent
class, enabling dynamic method overriding.

The Main Purpose of Reuse with Inheritance:

The main purpose of inheritance is to “reuse” code across multiple related classes without redundancy.
Instead of writing the same methods or attributes multiple times, you define them once in a base class
and inherit them where necessary.

Real-World Example of Reuse:


- A Person class might define common attributes like name and age, and methods like `introduce () `.

- A Student class could inherit from Person and add specific attributes like `studentID` and methods like
`study () `.

- Similarly, a Teacher class could inherit from Person and add attributes like `teacherID` and methods like
`teach () `.
By using inheritance, the `Student` and `Teacher` classes can reuse the common properties and behavior
defined in the `Person` class.

Example:

class Person {

public:

string name;

int age;

void introduce() {

cout << "My name is " << name << " and I am " << age << " years old.";

};

class Student : public Person {

public:

int studentID;

void study() {

cout << "I am studying.";

};

class Teacher : public Person {

public:

int teacherID;

void teach() {

cout << "I am teaching.";

};
```

In this example:

- **Student** and **Teacher** reuse the `name`, `age`, and `introduce()` method from **Person**.

- They also add their own specific attributes (`studentID`, `teacherID`) and methods (`study()`, `teach()`).

Summary:
- **Inheritance** allows derived classes to reuse and extend the properties and behaviors of base
classes.

- It models an **"is-a"** relationship.

- The main benefit is **code reuse** while maintaining the flexibility to add new features in derived
classes.

You might also like