C++ Inheritance
C++ Inheritance
A DV E RT I S E M E N T S
class Animal {
// eat() function
// sleep() function
};
/
Here,
Thank you for printing the
our content class
Dog at is derived from the Please
www.domain-name.com. Animal class.
check back soon for new
contents.
Since Dog is derived from Animal , members of Animal
Inheritance in C++
is-a relationship
/
Inheritance
Thank you for printing is an
our content is-a relationship. We use
at www.domain-name.com. inheritance
Please check back soon for new
contents.
only if an is-a relationship is present between the two
classes.
Search tutorials and examples
(/)
Here are some examples:
www.domain-name.com
A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.
/
Thank you for printing our content at www.domain-name.com. Please check back soon for new
// C++ program to demonstrate inheritance
contents.
#include <iostream>
using namespace std;
Search tutorials and examples
(/)
// base class
www.domain-name.com
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
A DV E RT I S E M E N T S
Output
/
Thank you for printing our content at www.domain-name.com. Please check back soon for new
I can eat!
contents.
I can sleep!
I can bark! Woof woof!!
Search tutorials and examples
(/)
Here, dog1 (the object of derived class
www.domain-name.com Dog ) can access
members of the base class Animal . It's because Dog is
inherited from Animal .
/
Thank you for printing our content at www.domain-name.com. Please check back soon for new
// C++ program to demonstrate protected members
contents.
#include <iostream>
#include <string>
Search tutorials and examples
using
(/) namespace std;
www.domain-name.com
// base class
class Animal {
private:
string color;
protected:
string type;
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
string getColor() {
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
I am a mammal
My color is black
class.
class Animal {
// code
};
/
Thank you for printing our content at www.domain-name.com. Please check back soon for new
class Cat : protected Animal {
contents.
// code
};
Search tutorials and examples
(/)
The various ways we can derive classes are known as
www.domain-name.com
access modes. These access modes have the following
effect:
Next Tutorial:
(/cpp-programming/public-
Inheritance
protected-private-inheritance)
Access Control
Previous Tutorial:
C++ Memory (/cpp-programming/memory-
management)
Management
A DV E RT I S E M E N T S
Related Tutorials
C++ Tutorial
C++ TutorialSearch
tutorials and examples
(/)
C++ Virtual Functions
www.domain-name.com
(/cpp-programming/virtual-functions)
C++ Tutorial
(/cpp-programming/access-modifiers)
C++ Tutorial
(/cpp-programming/multilevel-multiple-inheritance)