0% found this document useful (0 votes)
8 views

C++ Inheritance

C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C++ Inheritance

C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Thank you for printing our content at www.domain-name.com.

Please check back soon for new


contents.

Search tutorials and examples


(/)
C++ Inheritance
www.domain-name.com

In this tutorial, we will learn about inheritance in C++ with


the help of examples.

A DV E RT I S E M E N T S

Inheritance is one of the key features of Object-oriented


programming in C++. It allows us to create a new class
(/cpp-programming/object-class) (derived class) from an
existing class (base class).

The derived class inherits the features from the base


class and can have additional features of its own. For
example,

class Animal {
// eat() function
// sleep() function
};

class Dog : public Animal {


// bark() 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

are accessible to Dog .


Search tutorials and examples
(/)
www.domain-name.com

Inheritance in C++

Notice the use of the keyword public while inheriting


Dog from Animal.

class Dog : public Animal {...};

We can also use the keywords private and protected

instead of public . We will learn about the differences


between using private , public and protected later in
this tutorial.

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.

Example 1: Simple Example of C++


Inheritance

/
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 .

// Calling members of the Animal class


dog1.eat();
dog1.sleep();

C++ protected Members


The access modifier protected is especially relevant
when it comes to C++ inheritance.

Like private members, protected members are


inaccessible outside of the class. However, they can be
accessed by derived classes and friend
classes/functions.

We need protected members if we want to hide the data


of a class, but still want that data to be inherited by its
derived classes.

To learn more about protected, refer to our C++ Access


Modifiers (/cpp-programming/access-modifiers) tutorial.

Example 2 : C++ protected Members

/
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;
}

void setColor(string clr) {


color = clr;
}

string getColor() {

Output

I can eat!
I can sleep!
I can bark! Woof woof!!
I am a mammal
My color is black

Here, the variable type is protected and is thus


accessible from the derived class Dog . We can see this
as we have initialized type in the Dog class using the
function setType() .

On the other hand, the private variable color cannot


be initialized in Dog .
/
Thank you for printing our content at www.domain-name.com. Please check back soon for new
class Dog : public Animal {
contents.
public:
void setColor(string
Search clr) {
tutorials and examples
(/) // Error: member "Animal::color" is inaccessibl
color =www.domain-name.com
clr;
}
};

Also, since the protected keyword hides data, we cannot


access type directly from an object of Dog or Animal

class.

// Error: member "Animal::type" is inaccessible


dog1.type = "mammal";

Access Modes in C++ Inheritance


In our previous tutorials, we have learned about C++
access specifiers such as public, private, and protected
(/cpp-programming/public-protected-private-
inheritance).

So far, we have used the public keyword in order to


inherit a class from a previously-existing base class.
However, we can also use the private and protected

keywords to inherit classes. For example,

class Animal {
// code
};

class Dog : private 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:

. public: If a derived class is declared in public mode,


then the members of the base class are inherited by
the derived class just as they are.

. private: In this case, all the members of the base class


become private members in the derived class.

. protected: The public members of the base class


become protected members in the derived class.

The private members of the base class are always


private in the derived class.

To learn more, visit our C++ public, private, protected


inheritance (/cpp-programming/public-protected-
private-inheritance) tutorial.

Member Function Overriding in


Inheritance
Suppose, base class and derived class have member
functions with the same name and arguments.

If we create an object of the derived class and try to


access that member function, the member function in the
derived class is invoked instead of the one in the base
class.
/
The our
Thank you for printing member function
content of derived class overrides
at www.domain-name.com. the back soon for new
Please check
contents.
member function of base class.

Learn more about


Search Function
tutorials and overriding
examples in C++ (/cpp-
(/)
programming/function-overriding).
www.domain-name.com

Recommended Reading: C++ Multiple Inheritance

Next Tutorial:
(/cpp-programming/public-
Inheritance
protected-private-inheritance)
Access Control

Previous Tutorial:
C++ Memory (/cpp-programming/memory-
management)
Management

(h ps://twi er.com/intent/tweet?text=Check this amazing


(h ps://www.facebook.com/sharer/sharer.php?
article: C++
u=h ps://www.programiz.com/cpp-
Share on:
Inheritance&via=programiz&url=h ps://www.programiz.com/cpp-
programming/inheritance)
programming/inheritance)
Was this article helpful?

A DV E RT I S E M E N T S

Related Tutorials

C++ Tutorial

Public, Protected and Private Inheritance in C++


Programming /
Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
(/cpp-programming/public-protected-private-inheritance)

C++ TutorialSearch
tutorials and examples
(/)
C++ Virtual Functions
www.domain-name.com

(/cpp-programming/virtual-functions)

C++ Tutorial

C++ Access Modifiers

(/cpp-programming/access-modifiers)

C++ Tutorial

C++ Multiple, Multilevel and Hierarchical Inheritance

(/cpp-programming/multilevel-multiple-inheritance)

You might also like