• Home
  • How do you define derived class explain with suitable example?

How do you define derived class explain with suitable example?

In object-oriented programming, a derived class is a class that is created from an existing class, known as the base class. The derived class inherits the characteristics and behaviors of the base class, and can also have additional characteristics and behaviors of its own.

For example, consider a base class called “Animal” that has the characteristics “species” and “diet,” and the behavior “movement.” We can create a derived class called “Dog” that inherits the characteristics and behavior of the Animal class, and also has the additional characteristics “breed” and “size,” and the behavior “barking.”

The derived class “Dog” would be defined as follows:

class Dog: public Animal {
public:
string breed;
string size;
void barking() {...}
};

In this example, the Dog class has inherited the characteristics “species” and “diet” from the Animal class, as well as the behavior “movement.” It has also added the characteristics “breed” and “size,” and the behavior “barking.”

The derived class can access and use all the characteristics and behaviors of the base class, as well as any additional characteristics and behaviors it has itself. This allows for code reuse and modularity in the program.