Professional Documents
Culture Documents
Inheritance
First Lecture
Date: 03/05/2017
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).
2
{
// body of class
};
3
#include <iostream>
class test
{
};
int main()
{
test x;
return 0;
}
As we can see, both i and j cannot be accessed from outside the class(in the case of i is
private, and j is protected).
4
We will use a table to keep track of how each one of them operate(and we will update it
as we go along).
Base members access type
Private
Protected
Public
● Members of base class will become members of derived class
For example:
#include <iostream>
class base
{
public:
};
5
{
public:
};
6
● In the previous example both i and j are private in base and thus cannot be
accessed from derived members.
● Public access type means that public members of base class become public in
derived, while protected in base will become protected in derived.
For example:
#include <iostream>
class base
{
public:
};
{
public:
7
};
{
x.print ();
y.set_k (3);
return 0;
}
Notice that y.print() will print rubbish to the screen, since y can’t access i, j(they are
hidden). Our data members will look like this:
8
Base members access type
● Using private access type, all public and protected data members of base will
become private in derived(and thus not accessible).
● Using protected inheritance access type, all public and protected members of
base becomes protected members of class derived.
9
Base members access type
HomeWork
Q:Write a program that has two classes (Father and Son),
Father has the following data types(f_id, f_age, set_f, print_f),
while Son has(s_id, s_age, set_s, print_s). And then test the 9
probabilities in the table above.
OOP
Inheritance Types
Date: 12/03/2017
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
2
1. Single Inheritance
● It’s the type that we have been using all along and it looks something like this:
Example:
#include <iostream>
class First
{
private:
int f_private;
3
protected:
int f_protected;
public:
int f_public;
{
}
};
{
private:
int s_private;
protected:
int s_protected;
public:
int s_public;
{
}
};
4
{
First f;
Second s;
f.f_private = 3; // Error
f.f_protected = 3; // Error
s.s_private = 4; // Error
s.s_protected = 4; // Error
s.f_private = 5; // Error
s.f_protected = 5; // Error
return 0;
}
5
6
2. Multi-Level 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).
Example:
#include <iostream>
class First
{
private:
int f_private;
protected:
7
int f_protected;
public:
int f_public;
{
}
};
{
private:
int s_private;
protected:
int s_protected;
public:
int s_public;
{
}
};
{
private:
8
int th_private;
protected:
int th_protected;
public:
int th_public;
{
}
};
{
First f;
Second s;
Third th;
f.f_private = 3; // Error
f.f_protected = 3; // Error
s.s_private = 4; // Error
s.s_protected = 4; // Error
9
s.f_private = 5; // Error
s.f_protected = 5; // Error
th.th_private = 6; // Error
th.th_protected = 6; // Error
return 0;
}
10
11
3. Hierarchical Inheritance
● In this type two classes(siblings) both inherit from one class (father)
Example:
#include <iostream>
class Father
{
private:
int f_private;
protected:
int f_protected;
public:
int f_public;
12
{
}
};
{
private:
int s_private;
protected:
int s_protected;
public:
int s_public;
{
}
};
{
private:
int d_private;
protected:
int d_protected;
13
public:
int d_public;
{
}
}
{
Father f;
Son s;
Daughter d;
f.f_private = 5; // Error
f.f_protected = 5; // Error
s.s_private = 6; // Error
s.f_private = 6; // Error
s.s_protected = 6; // Error
s.f_protected = 6; // Error
14
d.d_private = 7; // Error
d.f_private = 7; // Error
d.d_protected = 7; // Error
d.f_protected = 7; // Error
return 0;
}
15
4. Multiple Inheritance
● In this type of inheritance a class inherits from 2 or more classes.
● This type of inheritance looks like this:
Example:
#include <iostream>
class Father
{
private:
int f_private;
protected:
int f_protected;
public:
int f_public;
16
{
}
};
class Mother
{
private:
int m_private;
protected:
int m_protected;
public:
int m_public;
{
}
};
{
private:
int s_private;
protected:
int s_protected;
17
public:
int s_public;
{
}
};
{
Father f;
Mother m;
Son s;
f.f_private = 2; // Error
f.f_protected = 2; // Error
m.m_private = 3; // Error
m.m_protected = 3; // Error
s.s_private = 4; // Error
s.s_protected = 4; // Error
18
s.f_private = 2; // Error
s.f_protected = 2; // Error
s.m_private = 2; // Error
s.m_protected = 2; // Error
return 0;
}
19
20
5. Hybrid Inheritance
● This type combine all the previous types in different ways
● This type can look like this:
21
Inheritance
Lecture 3: Example About Inheritance
Date: 19/03/2017
Q/ Consider the following program
#include <iostream>
class One {
int i;
};
int j;
public:
};
2
{
x.read ();
x.read_one ();
x.read_two ();
y.read_one ();
y.reading ();
return 0;
}
3
Answer:
2. The errors that will be produced are the following:
y.read_one() → Because read_one() is Protected and can NOT be used outside of
the object.
4
int x, y;
};
class Q {
protected:
float a, b;
};
};
Answer:
class P {
int x, y;
public:
{
x = 0;
y = 0;
}
};
class Q {
protected:
5
float a, b;
};
public:
{
a = 0;
b = 0;
}
};
int i, j;
};
class DE {
int k;
};
class GH {
int m;};
6
Answer:
class AB {
protected:
int x, y;
};
int k;}
7
class AB {
int i, j;
};
protected:
int k;
};
int m;
};
Do the following:
8
Answer:
9
And to access all these data members we need to write the following code:
#include <iostream>
class Store {
private:
public:
{
st_no = s;
city = c;
}
};
private:
int g_type;
public:
{
g_type = g;
}
};
private:
10
public:
{
read_group (g);
code = c;
price = p;
num = n;
}
};
{
Item x;
return 0;
}
#include <iostream>
class Store {
private:
11
public:
{
return st_no;
}
{
return city;
}
};
private:
int g_type;
};
private:
public:
{
return 1;
return 0;
}
12
};
{
Item x;
x.check (x);
return 0;
}
Inheritance
By: Aws Al-Taee
Lecture 4: Hybrid Inheritance
&
Date: 26/03/2017
Hybrid Inheritance
● This is the last type of inheritance in C++ language
2
Example:
#include <iostream>
class A {
protected:
int i;
};
class B: public A {
protected:
int j;
};
class C: public A {
protected:
int k;
3
};
int l;
public:
{
}
};
4
As we can notice, there are two i data members in the class D, and this will create a
problem( The compiler will complain of an ambiguity problem), as the compiler doesn’t
know which i to use ( the one inherited from B or the one inherited from C ). This is
called the diamond problem.
#include <iostream>
class A {
protected:
int i;
};
class B: public A {
protected:
int j;
};
class C: public A {
protected:
int k;
};
5
int l;
public:
{
}
};
#include <iostream>
class A {
protected:
int i;
};
protected:
int j;
};
protected:
6
int k;
};
int l;
public:
{
}
};
Order of Execution
There are two order of execution for Constructors ( depending on the availability of
parameters) and one order of execution for Destructors ( Since Destructors don’t have
any parameters ).
7
Example:
#include <iostream>
class A {
protected:
int i;
public:
A()
{
}
~A()
{
}
};
8
class B: public A {
public:
B()
{
}
~B()
{
}
};
{
A x;
B y;
return 0;
}
9
Example:
#include <iostream>
10
class A {
protected:
int i;
public:
A()
{
}
~A()
{
}
};
class B: public A {
public:
B()
{
11
}
~B()
{
}
};
class C: public B {
int k;
public:
C()
{
}
~C()
{
}
};
12
{
A x;
B y;
C z;
return 0;
}
13
14
In case of multiple inheritance, constructors are called from left to right as specified in
inheritance list, Destructors in reverse order.
Example:
#include <iostream>
class A {
protected:
int i;
public:
A()
{
}
~A()
{
15
}
};
class B {
protected:
int j;
public:
B()
{
}
~B()
{
}
};
int k;
16
public:
C()
{
}
~C()
{
}
};
{
C x;
return 0;
}
17
Notice that C inherits A before B and so our execution screen will look like this:
Example:
#include <iostream>
class A {
protected:
int i;
public:
A()
{
18
}
~A()
{
}
};
class B {
protected:
int j;
public:
B()
{
}
~B()
{
19
}
};
int k;
public:
C()
{
}
~C()
{
}
};
{
C x;
20
return 0;
}
Inheritance
By: Aws Al-Taee
Lecture 5: Parameterized Constructors in Inheritance
Date: 02/04/2017
Parameterized Constructors
● This is the second type of constructors in inheritance.
For example:
#include <iostream>
class Base
{
public:
Base ()
{
2
}
Base ( int j )
{
}
~Base ()
{
}
};
{
public:
Derived1 ( int i )
{
}
};
{
3
Derived1 D1(1);
return 0;
}
Notice that we have 2 constructors in class Base, but only one of them was used and
that’s because the first one is the default one, so it will be the one to be used. To use the
second constructor of class Base, we need to specify it in the constructor of class
Derived1 like so:
#include <iostream>
class Base
{
public:
Base ()
{
4
}
Base ( int j )
{
}
~Base ()
{
}
};
{
public:
{
}
};
5
{
Derived1 D1(1);
return 0;
}
We can also have more than one constructor in class Derived1 and each one of those
constructors call a different constructor from class Base.
For example:
#include <iostream>
class Base
{
public:
Base ()
{
6
}
Base ( int j )
{
}
~Base ()
{
}
};
{
public:
Derived1 ( int i )
{
}
{
7
}
~Derived1 ()
{
}
};
{
Derived1 D1(1);
return 0;
}
Pointers
Date: 29/04/2017
➢ They only different between pointers and other types of data types are that
pointers hold the memory address of another variable instead of an actual
value.
Notice in the image above, we have a variable X of type integer and it has a value
of 70 and an address of 2001, while PTR is a variable of type pointer and it has a
value 2001 which happens to be the value of X.
2
How To De ne Pointers?
❖ To define pointers in c++ we follow the following formula:
■ Pointer type refers to the type of data the pointer is pointing to.
Example:
#include <iostream>
{
int x = 5;
int *xPtr;
xPtr = &x;
return 0;
}
xPtr = &x;
Which tells the compiler to let xPtr point at the address of x.
3
cout << xPtr << endl; Will print the address saved in xPtr (in my case it was
0x7ffe305099a4. But it will be different every time run the program).
4
➢ Example:
#include <iostream>
{
int x = 5;
*xPtr = 100;
return 0;
}
Running this we will see that the value of x will change from 5 to 100, although we never
used x = 100.
5
6
➢ Example:
#include <iostream>
{
return 0;
}
This will create an array of type char, that have 5 elements. Notice that the array does
not have any name. It does have, however, a pointer arrPtr pointing to its first
element(the pointer always points to the first element).
7
Example:
#include <iostream>
{
return 0;
}
Notice how in the statement ( char *arrPtr = arr;) we didn’t use the ( & operator )
because the name of the array is in itself a pointer ( it points to the first element of the
array).
8
➢ Example:
#include <iostream>
class Test {
private:
int i;
public:
{
}
{
}
};
{
Test A;
9
A.read ();
A.print ();
return 0;
}
Notice that ( A.read() and ptr -> read() are the same). We need to use the arrow ( → )
when we deal with pointers.
Notice that zPtr is a pointer pointing to Z, but also have a memory address ( 40123 in
this case).
Pointers to classes
❖ Pointers to the base class are compatible with all classes derived from it.
➢ We have to keep in mind that we can access data types inherited from
base class when using a pointer to a derived class.
Example:
#include <iostream>
class base{
private:
int i;
public:
{
2
}
};
private:
int j;
public:
{
}
};
{
base a, *b;
derived c, *d;
b = &a;
d = &c;
3
// and c is derived
b = &c;
// while a is base
d = &a;
return 0;
}
Example:
#include <iostream>
class base {
private:
int i = 10;
public:
4
{
}
};
private:
int j = 5;
public:
{
}
};
{
base *a;
derived b;
a = &b;
5
return 0;
}
The output will be ( i = 10 ) instead of ( j = 5 ) and that’s because a does NOT recognize
the print_data function of the derived class.
Dynamic Binding
&
Operator Overloading
Date: 29/05/2017
Virtual Function
● Virtual functions are used to allow us to use dynamic binding
● The simple idea behind it is to define a base class, and then make other classes
inherit from it. And then define a pointer to the base class, and make a new
object from any of the children of that base class.
○ Inheritance
○ Virtual function
Example:
#include <iostream>
2
class Shape
{
protected:
public:
{
width = x;
length = y;
}
{
}
};
3
{
public:
{
cout << "Rectangle Area = " << width * length << endl;
}
};
{
public:
{
cout << "Triangle Area = " << 0.5 * width * length << endl;
}
};
4
{
public:
{
cout << "Circle Area = " << 0.5 * length * length * 3.14 << endl;
}
};
{
Shape *s;
int l, w;
int choice;
cout << "Choose from the following:" << endl << "1.Rectangle
2.Triangle 3.Circle" << endl;
5
switch (choice ) {
case 1:
s = new Rectangle;
break;
case 2:
s = new Triangle;
break;
case 3:
s = new Circle;
6
break;
default:
break;
}
return 0;
}
And this is what we call dynamic binding, the area function that the program calls,
depends on what the user specifies and the compiler does not know which one to call
until it is told by the user.
7
We can use what is called Pure Virtual Functions in our base class like so:
To:
A simple trace of the program looks like this (User inputs are in white):
8
Operator Overloading
● C++ allows us to overload most operators in the language.
● We need to keep in mind that we can’t overload every operator(we can’t overload
[. , * ?]
○ For example: the operator ( ++ ) takes only one parameter, so when we
overload it, we must provide only one parameter.
Example:
#include <iostream>
class Rectangle
{
int l, w;
public:
9
{
l = x;
w = y;
}
{
r1.l = l + r.l;
r1.w = w + r.w;
return r1;
}
{
return true;
}
return false;
}
{
10
}
};
{
r = p + q;
r.print_val ();
if (p == q) {
}
else {
}
return 0;
}