Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 3 out of 16 pages
Summary

Summary C++ INHERITANCE

Document preview thumbnail
Preview 3 out of 16 pages

The document thoroughly explains inheritance in C++, a key feature of object-oriented programming. It details how classes can inherit properties and methods from base classes, promoting code reuse and hierarchy. Practical examples demonstrate single, multiple, and multilevel inheritance, along with access specifiers (public, protected, private). The notes cover constructors, destructors, and the importance of virtual functions for polymorphism. Emphasizing best practices, the document highlights inheritance's role in creating flexible and maintainable code structures.

Content preview

ELITE TUTORING




C++ Inheritance
Inheritance is one of the key features of Object-oriented programming in C++.
It allows us to create a new 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, the Dog class is derived from the Animal class. Since Dog is derived
from Animal , members of Animal are accessible to Dog .

,ELITE TUTORING




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




is-a relationship
Inheritance is an is-a relationship. We use inheritance only if an is-a
relationship is present between the two classes.
Here are some examples:

, ELITE TUTORING



• 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
// C++ program to demonstrate inheritance

#include <iostream>
using namespace std;

// base class
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() {

Document information

Uploaded on
July 25, 2024
Number of pages
16
Written in
2023/2024
Type
Summary
$5.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
1
Items
69
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions