Professional Documents
Culture Documents
2
M. H. Bhuyan Lec 12 and 13
Types of Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multi-level Inheritance
Hybrid Inheritance
3
M. H. Bhuyan Lec 12 and 13
Inheritance
4
M. H. Bhuyan Lec 12 and 13
Access Control
Base class access
In the base class In the derived class
specifier
public public public
protected public protected
private public no access
public protected protected
protected protected protected
private protected no access
public private private
protected private private
private private no access
5
M. H. Bhuyan Lec 12 and 13
Example
#include <iostream>
using namespace std;
class Base {
public:
void f1();
void f2();
};
class Derived : public Base {
public:
void f1();
};
6
M. H. Bhuyan Lec 12 and 13
Example
void Base::f1()
{
cout << "Base: f1\n";
}
void Base::f2()
{
cout << "Base: f2\n";
f1();
}
void Derived::f1()
{
cout << "Derived: f1\n";
}
7
M. H. Bhuyan Lec 12 and 13
Example
int main() {
Derived d;
d.f2();
}
8
M. H. Bhuyan Lec 12 and 13
Constructors in Derived Class
9
M. H. Bhuyan Lec 12 and 13
Example
#include <iostream>
using namespace std;
class alpha { int x;
public:
alpha() { };
alpha(int i)
{ x=i; cout << x<<x<< endl; }
};
Class beta { float y;
public:
beta(float j)
{ y=j; cout << y=<<y<< endl; }
};
10
M. H. Bhuyan Lec 12 and 13
Example
11
M. H. Bhuyan Lec 12 and 13
Example
int main(){
gamma g(9, 12.87, 40, 23);
return 0;
}
12
M. H. Bhuyan Lec 12 and 13
Virtual Function
13
M. H. Bhuyan Lec 12 and 13
Virtual Function
class Base {
public:
virtual void f1();
void f2();
};
14
M. H. Bhuyan Lec 12 and 13
Example
class A{
public: Only matter with pointer or
void x() {cout<<"A:x";}; reference
virtual void y() {cout<<"A:y";}; Calls on object itself resolved
}; statically
E.g., b.y();
class B : public A { Look first at pointer/reference
public: type
void x() {cout<<"B:x";};
virtual void y() {cout<<"B:y";}; If non-virtual there, resolve
statically
};
E.g., ap->x();
int main () { If virtual there, resolve dynamically
B b; E.g., ap->y();
A *ap = &b; B *bp = &b; Note that virtual keyword need
b.x(); // prints "B:x" not be repeated in derived
b.y(); // prints "B:y" classes
bp->x(); // prints "B:x" But its good style to do so
bp->y(); // prints "B:y"
ap.x(); // prints "A:x" Caller can force static resolution
ap.y(); // prints "B:y" of a virtual function via scope
return 0; operator
}; E.g., ap->A::y(); prints A::y
15
M. H. Bhuyan Lec 12 and 13
Abstract Classes and Pure Virtual Functions
A base class can have some known features, but we are not able
to define them, only in the derived class.
In this case, we declare a virtual function, but we dont define it
in the base class.
If a virtual member function is declared in the base class, but
isnt defined, we call it a pure virtual function.
Pure virtual functions are declared in the regular way, but the
declaration ends with =0. This means, that we dont want to define the
function right now.
If a class contains at least one pure virtual function, then we name it
abstract class.
No instance of an abstract class can be defined.
16
M. H. Bhuyan Lec 12 and 13
Overriding the Pure Virtual Functions
17
M. H. Bhuyan Lec 12 and 13
Example
class A {
public: A is an Abstract Base Class
virtual void x() = 0; Similar to an interface in Java
virtual void y() = 0; Declares pure virtual functions (=0)
};
Derived classes override pure
class B : public A { virtual methods
public: B overrides x(), C overrides y()
virtual void x();
};
Cant instantiate class with
declared or inherited pure
class C : public B { virtual functions
public: A and B are abstract, can create a
virtual void y(); C
};
Can still have a pointer to an
int main() { abstract class type
C *c1; Useful for polymorphism
c1->x();
c1->y();
return 0;
};
18
M. H. Bhuyan Lec 12 and 13
Virtual Base Class
19
M. H. Bhuyan Lec 12 and 13
Example
Class A {
};
Class B1 : virtual public A{
};
Class B2 : public virtual A{
..
};
Class C: public B1, public B2
{
. //only one copy of A will be inherited
.
};
20
M. H. Bhuyan Lec 12 and 13
Example
class A {
public:
int i; };
class B : virtual public A {
public:
int j; };
class C: virtual public A {
public:
int k; };
class D: public B, public C {
public:
int sum; };
int main() {
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << Value of i is : << ob.i<<\n;
cout << Value of j is : << ob.j<<\n; cout << Value of k is :<<
ob.k<<\n;
cout << Sum is : << ob.sum <<\n;
return 0; }
21
M. H. Bhuyan Lec 12 and 13
this Pointer
22
M. H. Bhuyan Lec 12 and 13
Example
#include <iostream>
using namespace std;
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
}
double Volume() {
return length * breadth * height;
}
23
M. H. Bhuyan Lec 12 and 13
Example
int compare(Box box) {
return this->Volume() > box.Volume();
}
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
}
else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
24
M. H. Bhuyan Lec 12 and 13
Problems
25
M. H. Bhuyan Lec 12 and 13
Questions?
26
M. H. Bhuyan Lec 12 and 13