You are on page 1of 2

#include <iostream>

#include <string>

// Base class

class Animal {

private:

std::string name;

public:

Animal(const std::string& n) : name(n) {}

void eat() {

std::cout << name << " is eating." << std::endl;

void sleep() {

std::cout << name << " is sleeping." << std::endl;

};

// Derived class

class Dog : public Animal {

private:

std::string breed;

public:

// Constructor for Dog class, calling the base class constructor explicitly

Dog(const std::string& n, const std::string& b) : Animal(n), breed(b) {}

void bark() {

std::cout << "Woof! Woof!" << std::endl;


}

// Overriding the sleep method from the base class

void sleep() {

std::cout << "The dog named " << getName() << " is sleeping." << std::endl;

// Additional method specific to the Dog class

void displayBreed() {

std::cout << "Breed: " << breed << std::endl;

};

int main() {

// Creating an instance of the Dog class

Dog myDog("Buddy", "Golden Retriever");

// Accessing methods from the base class

myDog.eat();

myDog.sleep(); // Calls the overridden sleep method in Dog class

// Accessing methods from the derived class

myDog.bark();

myDog.displayBreed();

return 0;

You might also like