Unit 3
1 -Inheritance
Inheritance is the process of creating new classes, called derived classes,
from existing classes or base classes. The derived class inherits all the capabilities
of the base class.
An inherited class is called a subclass of its parent class or super class.
Inheritance is a mechanism in which one class acquires the property of
another class. For example, a child inherits the traits of his/her parents.
With inheritance, we can reuse the fields and methods of the existing class.
Sub Class: The class that inherits properties from another class is called Sub
class or Derived Class.
Super Class: The class whose properties are inherited by sub class is called
Base Class or Super class.
(Parent class, Super class, Base class)
(Child class, Sub class, Derived class)
Syntax
class subclass_name : access_mode base class_name
{
//body of subclass
};
Example
Class A
{
-------
};
class B : public A
{
-------
};
Types of Inheritance
!.Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance
(One base class -One derived class)
Single Inheritance have One base class and One derived class C++ single
inheritance only one class can be derived from the base class. Based on the
visibility mode used or access specify used while deriving, the properties of the
base class are derived. Accesses specify can be private, protected or public.
Diagram
Example
Class A // base class
{ ..........
};
Class B: acess_specifier A // derived class
{ ...........
};
Multilevel Inheritance
(One Base - One Derived - One or more than One Intermediate class)
Multilevel Inheritance in C++ Programming. When a class is derived from a
class which is also derived from another class, i.e. a class having more than one
parent classes, such inheritance is called Multilevel Inheritance. The level of
inheritance can be extended to any number of level depending upon the relation.
Diagram
class A // base class
{ ...........
};
class B : acess_specifier A // derived class
{ ...........
};
class C : access_specifier B // derived from derived class B
{ ...........
};
Multiple Inheritance
More than One base class -One derived class
Multiple Inheritance in C++ Multiple Inheritance is a feature of C++ where
a class can inherit from more than one classes. The constructors
of inherited classes are called in the same order in which they are inherited
Diagram
Syntax
class A
{..........
};
class B
{ ...........
};
class C : acess_specifier A , access_specifier B // derived class from A and B
{...........
};
Difference between Multilevel and Multiple Inheritance
● Multilevel inheritance : Inheritance of characters by a child from father and
father inheriting characters from his father (grandfather)
● Multiple inheritance : Inheritance of characters by a child from mother and
father
Hierarchical Inheritance
One base class - More than One Derived class
Several classes are derived from common base class it is called hierarchical
inheritance.
In C++ hierarchical inheritance, the feature of the base class is inherited
onto more than one sub-class.
Diagram
Syntax
class A // base class
{..............
};
class B : access_specifier A // derived class from A
{ ...........
};
class C : access_specifier A // derived class from A
{ ...........
};
class D : access_specifier A // derived class from A
{ ...........
};
Hybrid Inheritance
Combination of More than One type of Inheritance
C++ hybrid inheritance is combination of two or more types of inheritance.
It can also be called multi path inheritance.
The hybrid combination of single inheritance and multiple inheritance.
Hybrid inheritance is used in a situation where we need to apply more than one
inheritance in a program.
Diagram
Syntax
class A
{ .........
};
class B : public A
{ ..........
};
class C
{ ...........
};
class D : public B, public C
{ ...........
};
Example program -Lab Program (9,10,11,12) Any one
2.Virtual Base class
Virtual base class is used in situation where a derived have multiple copies
of base class.
Two or more objects are derived from a common base class, we can prevent
multiple copies of the base class being present in an object derived from those
objects by declaring the base class as virtual when it is being inherited. Such a base
class is known as virtual base class. This can be achieved by preceding the base
class’ name with the word virtual.
Syntax
class A
{
}
class B: Virtual public A //virtual base class
{
}
class C: Virtual public A // virtual base class
{
{
}
class D: public B, public C
{
}
Example Program :
#include<iostream.h>
#include<conio.h>
class ClassA
{
public:
int a;
};
class ClassB : virtual public ClassA
{
public:
int b;
};
class ClassC : virtual public ClassA
{
public:
int c;
};
class ClassD : public ClassB, public ClassC
{
public:
int d;
};
void main()
{
ClassD obj;
obj.a = 10; //Statement 1
obj.a = 100; //Statement 2
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "\n A : "<< obj.a;
cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;
Output :
A : 100
B : 20
C : 30
D : 40
3.Virtual Function
A virtual function is a member function that you expect to be redefined in
derived classes. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object and
execute the derived class's version of the function.
Uses
virtual functions when you want to override a certain behavior (read method)
for your derived class rather than the one implemented for the base class and you
want to do so at run-time through a pointer to the base class.
Rules for calling virtual Function
1. When a virtual function in a base class is a created, there must be definition of a
virtual function in the base class.
2. They cannot be static member.
3. They can be friend function to another class.
4. They are accessed using object pointer.
5. A base pointer can serve as a pointer to derived object since it is type compatible
where as a derived object pointer variable to base object.
Syntax
class class- name
{
public:
virtual function-name()
{
}
};
Example
class bca
{
public:
virtual sum() // virtual function
{
}
};
Example program
#include <iostream.h>
class Bird
{
public:
virtual void features()
{
cout << "Loading Bird features.\n";
}
};
class Parrot : public Bird
{
public:
void features()
{
this->Bird::features();
cout << "Loading Parrot features.\n";
}
};
class Penguin : public Bird
{
public:
void features()
{
this->Bird::features();
cout << "Loading Penguin features.\n";
}
};
class Loader
{
public:
void loadFeatures(Bird *bird)
{
bird->features();
}
};
int main()
{
Loader *l = new Loader;
Bird *b;
Parrot p1;
Penguin p2;
b = &p1;
l->loadFeatures(b);
b = &p2;
l->loadFeatures(b);
return 0;
}
OUTPUT
Loading Bird features.
Loading Parrot features.
Loading Bird features.
Loading Penguin features.
4. Abstract class and Pure virtual Function
An abstract class is a class that is designed to be specifically used as a base
class. An abstract class contains at least one pure virtual function. You declare a
pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual
member function in the class declaration.
A class that is declared using “abstract” keyword is known as abstract class.
It can have abstract methods(methods without body) as well as concrete methods
(regular methods with body).
An abstract class cannot be instantiated, which means you are not allowed to
create an object of it.
An abstract class is mostly used to provide a base for subclasses to
extend and implement the abstract methods and override or use the implemented
methods in abstract class.
Pure virtual Function
A pure virtual function or pure virtual method is a virtual function that is
required to be implemented by a derived class if the derived class is not abstract.
Classes containing pure virtual methods are termed "abstract" and they cannot be
instantiated directly.
A subclass of an abstract class can only be instantiated directly if all
inherited pure virtual methods have been implemented by that class or a parent
class.
Example
Class student /*abstract class*/
{
Virtual void sum()=0; /*pure virtual function*/
};
Example
class Test // abstract class
{
public:
virtual void show() = 0;
};
Pure virtual function is also known as abstract class.
● We can't create an object of abstract class b'coz it has partial implementation
of methods.
● Abstract function doesn't have body
● We must implement all abstract functions in derived class.
Example Program
#include<iostream.h>
#include<conio.h>
class BaseClass //Abstract class
public:
virtual void Display1()=0; //Pure virtual function or abstract function
virtual void Display2()=0; //Pure virtual function or abstract function
void Display3()
cout<<"\n\tThis is Display3() method of Base Class";
};
class DerivedClass : public BaseClass
public:
void Display1()
cout<<"\n\tThis is Display1() method of Derived Class";
}
void Display2()
cout<<"\n\tThis is Display2() method of Derived Class"
};
void main()
DerivedClass D;
D.Display1(); // This will invoke Display1() method of Derived Class
D.Display2(); // This will invoke Display2() method of Derived Class
D.Display3(); // This will invoke Display3() method of Base Class
Output :
This is Display1() method of Derived Class
This is Display2() method of Derived Class
This is Display3() method of Base Class
5. This Pointer
C++ this Pointer. Every object in C++ has access to its own address
through an important pointer called this pointer. This pointer is an implicit
parameter to all member functions. ... Friend functions do not have a
this pointer, because friends are not members of a class.
A friend function of a class is defined outside that class' scope but it has the
right to access all private and protected members of the class. Even though the
prototypes for friend functions appear in the class definition, friends are not
member functions.
Uses
1. Each object gets its own copy of the data member.
2. 2. All access the same function definition as present in the code
segment.
The ‘this’ pointer is passed as a hidden argument to all non-static
member function calls and is available as a local variable within the body of all
non-static functions.
‘this’ pointer is a constant pointer that holds the memory address of the
current object. ‘this’ pointer is not available in static member functions as static
member functions can be called without any object (with class name).
The this pointer holds the address of current object, in simple words you
can say that this pointer points to the current object of the class
Example Program:
#include <iostream.h>
class B;
class A
{
private:
int numA;
public:
A(): numA(12) { }
friend int add(A, B);
};
class B
{
private:
int numB;
public:
B(): numB(1) { }
friend int add(A , B);
};
int add(A objectA, B objectB)
{
return (objectA.numA + objectB.numB);
}
int main()
{
A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}
6.Operator overloading
In C++, we can make operators to work for user defined classes. This means C++
has the ability to provide the operators with a special meaning for a data type, this ability
is known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can
concatenate two strings by just using +.
C++ provides a special function to change the current functionality of some
operators within its class which is often called as operator overloading. Operator
Overloading is the method by which we can change the function of some specific
operators to do some different task.
Syntax
Return_Type classname :: operator op(Argument list)
{
Function Body
}
Operator Overloading can be done by using three approaches, they are
1. Overloading unary operator.
2. Overloading binary operator.
Overloading Unary Operator: Let us consider to overload (-) unary operator. In
unary operator function, no arguments should be passed. It works only with one class
objects. It is a overloading of an operator operating on a single operand.
Overloading Binary Operator: In binary operator overloading function, there
should be one argument to be passed. It is overloading of an operator operating on two
operands.
Rules for overloading
● In case of a non-static function, the binary operator should have only one argument
and unary should not have an argument.
● In the case of a friend function, the binary operator should have only two argument
and unary should have only one argument.
● All the class member object should be public if operator overloading is
implemented.
Operators which cannot be overloaded.
● ?: (conditional)
● . ( member selection)
● .* (member selection with pointer-to-member)
● :: (scope resolution)
● sizeof (object size information)
● typeid (object type information)
● static_cast (casting operator)
● const_cast (casting operator)
Overloaded Operators
●
Example Program
#include <iostream.h>
using namespace std;
class IncreDecre
{
int a, b;
public:
void accept()
{
cout<<"\n Enter Two Numbers : \n";
cout<<" ";
cin>>a;
cout<<" ";
cin>>b;
}
void operator--() //Overload Unary Decrement
{
a--;
b--;
}
void operator++() //Overload Unary Increment
{
a++;
b++;
}
void display()
{
cout<<"\n A : "<<a;
cout<<"\n B : "<<b;
}
};
int main()
{
IncreDecre id;
id.accept();
--id;
cout<<"\n After Decrementing : ";
id.display();
++id;
++id;
cout<<"\n\n After Incrementing : ";
id.display();
return 0;
}