Inheritance

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

cppCopy code#include <iostream>

class Animal {
public:
void speak() {
    std::cout &lt;&lt; "Animal speaks!" &lt;&lt; std::endl;
}
}; class Cat : public Animal { public:
void speak() {
    std::cout &lt;&lt; "Cat meows!" &lt;&lt; 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 *