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
Class notes

inheritance single , multi , multilevel in C++ program

Rating
-
Sold
-
Pages
7
Uploaded on
29-06-2025
Written in
2024/2025

This document contains a collection of easy-to-understand C++ programs, designed especially for beginners, college students, and BSCS first-year learners. Each program is written with clear syntax, step-by-step logic, and proper output display to help you build a strong foundation in C++.

Show more Read less
Institution
Course

Content preview

Document 8: Inheritance Types.
This document will cover the different types of inheritance in C++ (Single, Multiple, Multilevel),
explaining each concept with illustrative code examples. As requested, I will generate both
DOCX and PDF versions, maintaining anonymity.

Inheritance Types in C++
Introduction to Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a
class to inherit properties and behaviors (data members and member functions) from another
class. The class that inherits is called the derived class (or child class), and the class from
which it inherits is called the base class (or parent class).
Key benefits of Inheritance:
● Code Reusability: Members of the base class can be reused by the derived class,
reducing redundant code.
● Polymorphism: It enables runtime polymorphism through virtual functions.
● Extensibility: New functionalities can be added to existing classes without modifying
them.
● "Is-A" Relationship: Inheritance models the "is-a" relationship (e.g., "A Car is a
Vehicle").

Syntax for Inheritance
class DerivedClass : access_specifier BaseClass {
// Members of DerivedClass
};

The access_specifier (e.g., public, protected, private) determines how the protected and public
members of the BaseClass are inherited into the DerivedClass.
● public inheritance: public members of the base class remain public in the derived class,
protected members remain protected. private members are never accessible. This is the
most common and recommended type, modeling the "is-a" relationship directly.
● protected inheritance: public and protected members of the base class become
protected in the derived class.
● private inheritance: public and protected members of the base class become private in
the derived class.

Types of Inheritance
There are several types of inheritance in C++:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance (one base class, multiple derived classes) - Not explicitly covered
here, but conceptually simple.
5. Hybrid Inheritance (combination of multiple types) - Not explicitly covered here.

1. Single Inheritance

, Single inheritance involves one derived class inheriting from a single base class. It is the
simplest and most common form of inheritance.
Diagram:
Base Class

|
Derived Class

Example Program: Single Inheritance (Animal -> Dog)
#include <iostream>
#include <string>

// Base Class
class Animal {
public:
std::string species;
int age;

Animal(std::string s, int a) : species(s), age(a) {
std::cout << "Animal Constructor Called." << std::endl;
}

void eat() {
std::cout << species << " is eating." << std::endl;
}

void sleep() {
std::cout << species << " is sleeping." << std::endl;
}
};

// Derived Class (inherits publicly from Animal)
class Dog : public Animal {
public:
std::string breed;

// Dog's constructor calls Animal's constructor
Dog(std::string b, int a, std::string sp = "Dog") : Animal(sp, a),
breed(b) {
std::cout << "Dog Constructor Called." << std::endl;
}

void bark() {
std::cout << species << " the " << breed << " is barking:
Woof! Woof!" << std::endl;
}
};

int main() {
// Create an object of the derived class
Dog myDog("Golden Retriever", 3);

Written for

Institution
Course

Document information

Uploaded on
June 29, 2025
Number of pages
7
Written in
2024/2025
Type
Class notes
Professor(s)
Fawad gillani
Contains
All classes

Subjects

$10.99
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
fatimamaqbool77

Also available in package deal

Get to know the seller

Seller avatar
fatimamaqbool77 ncba&e
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
11 months
Number of followers
0
Documents
14
Last sold
-
study by fatima

Welcome to my store! I upload original, easy-to-understand notes designed for school and university students. My content covers a range of subjects including English Grammar, Functional English, Logic and Critical Thinking, Computer Science, and General Studies. All documents are written in simple language, with examples, definitions, and formatting that help you study faster and perform better in exams. Whether you’re preparing for tests or just want clear notes to understand your subject, you’ll find something helpful here.

Read more Read less
0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

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