You are on page 1of 26

Classes and Objects: Part-II

Department of Computer Science and Engineering


School of Engineering and Technology
Kaziranga University
Jorhat-785006, Assam.
monowar@kazirangauniversity.in
Inheritance

Inheritance allows a new class to be based on an existing


class.
The new class inherits all the member variables and functions of
the class it is based on.

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

Defining a derived class:


Class derived-class-name: visibility mode base-class-name
{
------
------
};

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();
}

The selection of the f1 function has been done in compile time.

8
M. H. Bhuyan Lec 12 and 13
Constructors in Derived Class

Derived classes dont inherit constructors and destructors.


The constructor of the derived class:
ClassName(list-of-parameters) :
C_1(list1), ..., C_n(list_n)
{
//
}

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

Class gamma : public beta, public alpha{


int m,n;
Public :
gamma(int a, float b, int c, int d): alpha(a), beta(b){
m=c;
n=d;
Cout<<c=<<c<<d=<<d<<endl;
};

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

Used to support polymorphism with pointers and


references
Declared virtual in a base class
Can be overridden in derived class
Overriding only happens when signatures are the same
Otherwise it just overloads the function or operator name
Ensures derived class function definition is resolved
dynamically

13
M. H. Bhuyan Lec 12 and 13
Virtual Function
class Base {
public:
virtual void f1();
void f2();
};

If function f1 is declared as virtual, then the selection of


the file will be done in running-time.
int main() {
Derived d;
d.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

We have to override all pure virtual functions in the derived


class.
In other case the derived class will be also abstract.

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

To avoid multiple copy of inheritance from multiple base


class, virtual base class is used
When a class is made a virtual base class, C++ takes
necessary care to see that only one copy of that class is
inherited.

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

The keyword this is a special type of pointer in C++


Every object in C++ has access to its own address through
this pointer.
The this pointer is an implicit parameter to all member
functions
Inside a member function, this may be used to invoke the
object.
Friend functions do not have a this pointer, because friends
are not members of a class.

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

Write a program in C++ to implement the students mark


cards of a semester; that is composed of three core subjects
and two elective subjects. [use the inheritance and the virtual
function]
Write a program in C++ to implement the students mark
cards of a semester; that is composed of three core subjects
and two elective subjects. [use this pointer]

25
M. H. Bhuyan Lec 12 and 13
Questions?

26
M. H. Bhuyan Lec 12 and 13

You might also like