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
Summary

Summary C++ INHERITANCE

Rating
-
Sold
-
Pages
16
Uploaded on
25-07-2024
Written in
2023/2024

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.

Show more Read less
Institution
Module

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() {

Written for

Institution
Module

Document information

Uploaded on
July 25, 2024
Number of pages
16
Written in
2023/2024
Type
SUMMARY

Subjects

£4.63
Get access to the full document:

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

Get to know the seller
Seller avatar
peterkuria3

Also available in package deal

Get to know the seller

Seller avatar
peterkuria3 Harvard University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
1
Documents
69
Last sold
-
ELITE TUTORING

Elite Tutoring is your ultimate destination for academic success. I offer a curated selection of high-quality study tools designed to enhance your learning experience and boost exam performance. From comprehensive study guides and interactive practice exams to expert-recommended resources, I provide everything you need to excel. Trust Elite Tutoring to turn your academic challenges into triumphs and help you pass your exams with confidence.

Read more Read less
0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

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

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight 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 smashed it. It really can be that simple.”

Alisha Student

Frequently asked questions