Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

REPORT FORM 2019 / 2020

Ministry of Higher Education & Scientific Research


University of Baghdad
Department of Computer Science

Student name : ‫اسيا عكاب يوسف زغير‬


Tittle: oop (Inheritance)
Course: second
Stage:2
Lecturer name: D.dunia fadheel saffo
>Introduction<

● There are some important concepts that we


must talk about when discussing inheritance

○ Base Class: A class from which we inherit


(Animals is base for birds, and Birds is base
for Ducks).
○ Derived Class: A class which inherits from a
base class(Birds is derived
from Animals, and Ducks is derived from
Birds)
> General Form for Inheritance in C++ <

class derived class name: access type


base class name
{
// body of class
};

> Protected data members in a class <


● Protected data members are one of the three
data members in a class (the other
two being private and public)
● On the surface it behaves exactly like private
data members, but like we are going
to see later, they are very beneficial when
dealing with inheritance in classes.
> How To Use Inheritance <

Just like the data members in classes, there are


3 types of access for classes(Public, Private and
Protected).
Each one of them affects how we data members
are accessed.
We will use a table to keep track of how each one
of them operate.
> Inheritance Types <

There are 5 types of inheritance in C++language


1. Single Inheritance
2. Multi-Level Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance

1. Single Inheritance
● It’s the type that we have been using all along and it looks
something like this:

class staff
{
private:
char name[50];
int code;
public:
void getdata();
void display();};

class typist: public staff


{ private:
int speed;
public:
void getdata();
void display();
}
2. Multi-Level Inheritance

A class can be derived from more than one base
class. This is called multiple inheritance.
● This type of inheritance is the same as the last
one except the fact that it involves more than two
classes(hence we call it multi level).
● It looks like this:

Name & age


needed only
once
How to use:

class Person {
// Data members of person
public:
Person(int x) { cout << "Person::Person(int
) called" << endl; }
};
class Faculty : public Person {
// data members of Faculty
public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<<
endl;
}
};
class Student : public Person {
// data members of Student
public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<<
endl;
}
};
// multi-inherited
class TA : public Faculty, public Student {
public:
TA(int x):Student(x), Faculty(x) {
cout<<"TA::TA(int ) called"<< endl;
}
};
int main() {
TA ta1(40);
return 0;
}
3. Hierarchical Inheritance
● In this type two classes(siblings) both inherit from
one class (father)
● This type of inheritance looks like this:

4. Multiple Inheritance
● In this type of inheritance a class inherits from 2 or
more classes.
● This type of inheritance looks like this:
5. Hybrid Inheritance
● This type combine all the previous types in different
ways
● This type can look like this:
<source>

1-pdf ( Object-Oriented Programming in C++, Fourth


Edition ) by Robert Lafore

2- pdf( Object-Oriented Programming in C ++)


by Mahmoud said – New system technology

You might also like