100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary C++ INHERITANCE $5.49   Add to cart

Summary

Summary C++ INHERITANCE

 5 views  0 purchase

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

[Show more]

Preview 3 out of 16  pages

  • July 25, 2024
  • 16
  • 2023/2024
  • Summary
All documents for this subject (10)
avatar-seller
peterkuria3
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() {

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller peterkuria3. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $5.49. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

82956 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$5.49
  • (0)
  Add to cart