Inheritance

This example shows how to use inheritance to create a derived class.

cppCopy code#include <iostream>

class Animal {
public:
    void speak() {
        std::cout << "Animal speaks!" << std::endl;
    }
};

class Cat : public Animal {
public:
    void speak() {
        std::cout << "Cat meows!" << std::endl;
    }
};

int main() {
    Cat myCat;
    myCat.speak(); // Output: Cat meows!
    return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *