Sei sulla pagina 1di 22

FDP on C++

Venue : SJT 316


Prof. S P Meenakshi
Session: 2 – 5 pm
Date : 14-11-19 Assistant Prof. (Sr)
SCOPE
VIT
Topics to be covered
• What is function overriding in C++?
• Accessing overridden function in the base class from the derived class
• Upcasting
• Early Binding Vs Late Binding
• Virtual Functions
• Pure Virtual Functions
• Abstract Classes
• Exercises
Class and Inhertance
• The class which inherits the members of another class is called as
derived class
• The class whose members are inherited is called base class
Base class and Derived class example
#include <iostream> int main()
using namespace std; {
class Vehicle // Base class Bus b1;
{
public: b1.bus_id= “TN_b001”;
string vehicle_id; b1.vehicle_id=“V_TN_b001”;
}
cout << “Bus id : “ << b1.bus_id <<
class Bus : public Vehicle // Derived class
endl;
{
cout << “Vehicle id:” << b1.vehicle_id
public:
<< endl;
string bus_id;
}
}
What is function overriding in c++?
• Base class and derived class can have member functions with same name
and arguments.
• The member function implementation may differ in derived class from
parent class
• Also derived class inherits public data members and member functions
from parent class
• An object is created for child class and using which a member function
(with same name in base and derived class) is called
• Only child class member function will be invoked
• The inherited parent member function is overridden by derived class
version
• This feature is called as function overriding
Inheritance - Example

#include <iostream>
class Bus : public Vehicle // Derived class
using namespace std; {
class Vehicle // Base class };
{
int main()
public: {
string vehicle_id; Bus b1;
b1.vehicle_id ="V_TN_001";
void disp() b1.disp();
{
cout << " From parent class : " << }
vehicle_id << endl;}
};
Example – function overridding
#include <iostream>
using namespace std;
class Bus : public Vehicle // Derived class
class Vehicle // Base class {
{ public:
public: string bus_id;
string vehicle_id; void disp()
{
void disp() cout << "In child class : " << bus_id << endl;
{ }
cout << "In parent class : " << }
vehicle_id << endl;}
}
Example – function overriding (cont.,)
int main()
{
Bus b1;
b1.bus_id="TN_b001";

b1.vehicle_id="V_TN_b001";
b1.disp();
}
Accessing overridden function in the base class
from the derived class
• Derived class inherits data members and member functions from parent
class
• Parent member function can be overridden in derived class
• Overridden parent member function can be accessed inside child class
• Scope resolution operator :: is used to access parent member function
• Example: To access disp() of Vehicle class in Bus Class
• Vehicle::disp() -- is used in Bus class
• Class specification is important
• With out class it is interpreted as recursive call
Accessing overridden function in the base class
from the derived class - Example
#include <iostream> class Bus : public Vehicle // Derived class
{
using namespace std; public:
class Vehicle // Base class string bus_id;
{ void disp()
{
public: cout << "In child class : " << bus_id << endl;
string vehicle_id; Vehicle::disp(); // Accessing base class function in
void disp() derived class}
}
{
int main()
cout << "In parent class : " <<
vehicle_id << endl; {
Bus b1;
} b1.bus_id="TN_b001";
} b1.vehicle_id="V_TN_b001";
b1.disp();
}
Upcasting and Downcasting
• Object references can be type casted
• Example – parent object reference can be type casted to derived
object reference and vice versa
• Upcasting – derived class reference /pointer treated as base class
pointer
• Implicit type cast allowed
• Downcasting – base class reference/pointer treated as derived class
pointer
• Explicit typecast required
• It is not safe operation most of the times
Upcasting and Downcasting Example
class Bus : public Vehicle
#include <iostream> {
using namespace std; public: int main()
string bus_id; {
class Vehicle
disp() Vehicle v1, *pv;
{ { Bus b1, *pb;
public: cout << "In child : " << bus_id
string vehicle_id; << endl; //upcast
} pv = &b1;
disp() }; upcast(*pv);
{ upcast(Vehicle& v) pv->disp();
{
cout << "In parent : " << vehicle_id
<< endl; v.disp(); //downcast
} pb = (Bus *) &v1;
}
downcast(Bus& b) downcast(*pb);
{ Bus *pb1;pb->disp();
}; b.disp(); }
}
Early Binding
• Binding refers to the process that is used to convert identifiers (such
as variables and function names) into addresses
• Focus on function binding
• Early binding – binding function name to address during compilation
time
• In programs, we have direct function calls and indirect function calls
• A direct function call is a statement that directly calls a function
• Compiler adds a machine language instruction jumpto with function
definition address in place of the function call
• It is static binding
Early Binding - Example
int main()
#include <iostream> {
using namespace std; int a=5, b=9;
void add(int x, int y)
char op;
{
cout << "Enter '+' for addition, '*' for multiplication " <<
int z;
endl;
z= x+y;
cin >> op;
cout << x << " + " << y << " = " << z;
}
switch(op)
void multiply(int x, int y) {
{
int z; case '+': add(a,b);break;
z = x* y; case '*': multiply(a,b);break;
cout << x << " * " << y << " = " << z; }

} }
Late Binding
• In some programs, it is not possible to know which function will be
called until runtime
• This is known as dynamic binding or late binding
• Function pointers are used for this task
• Function pointer is a pointer that points to a function
• Function call operator (()) is used to call the function pointed by the
function pointer
• Late binding involves an extra level of indirection i.e, machine
instruction to read the function pointer and then jump to the address
Late Binding Example
int main()
#include <iostream>
{
using namespace std;
int a=5, b=9;
int add(int x, int y)
char op;
{
//create a function pointer
int z; int (*Fptr)(int,int) = NULL;
z= x+y;
cout << x << " + " << y << " = " << z; cout << "Enter '+' for addition, '*' for multiplication " << endl;
} cin >> op;
int multiply(int x, int y)
{ switch(op)
int z; {
z = x* y; case '+': Fptr=add;break;
cout << x << " * " << y << " = " << z; case '*': Fptr=multiply;break;
}
} // Call the function, the pointer points to; late binding
Fptr(a,b);
}
Virtual Functions
• A virtual function is a member function in base class
• It is expected to be redefined in the derived class (overridden)
• During upcasting, the base class virtual function can be called to
execute the overridden version of the derived class
• Virtual functions ensure that the correct function is called for an
object, regardless of the type of reference (or pointer) used for
function call.
• Functions are declared with a virtual keyword in base class.
• The resolving of function call is done at Run-time.
• They are mainly used to achieve Runtime polymorphism
Virtual function - Example
#include <iostream> class Display
using namespace std; {
class Vehicle public:
{ disp_features(Vehicle *v)
public:
{
v->disp();
string vehicle_id;
}
virtual disp()
};
{ cout << "Display vehicle features: " << vehicle_id << endl; } int main()
}; {
class Bus : public Vehicle Vehicle *vptr,v;
{
Bus b;
Display d;
public:
b.bus_id="TN_b001";
string bus_id;
b.vehicle_id="V_TN_b001";
disp() vptr=&b;
{ cout << "Display bus features: " << bus_id << endl; } d.disp_features(vptr);
}; }
Abstract Class and Pure Virtual Function
• Base class which cannot be instantiated
• Classes can be derived from abstract class
• Objects can be instantiated for derived class
• Abstract class contains pure virtual functions
• A virtual function whose declaration ends with “=0” is pure virtual
function
• Inheritance is used in this concept for better visualization of the
problem
Abstract Class and Pure Virtual Function - Example
#include <iostream>
class Bus : public Vehicle
using namespace std; {
//Abstract class public :
// Constructor
class Vehicle Bus()
{ {
protected:
type="Bus";
}
int numWheels, seats; wheels_capacity()
string type,owner; {
cout << "Enter number of wheels for " << type << " : " << endl;
public:
cin >> numWheels;
getdata() cout <<"Enter number of sheets for " << type << " : " << endl;
{ cin >> seats;
}
cout << "Enter the owner name of vehicle :" << endl;
disp_details()
cin >> owner; {
} cout << "Vehicle owner : " << owner << endl;
cout << "Vehicle type : " << type << endl;
//pure virtual function cout << "Number of wheels : " << numWheels << endl;
virtual wheels_capacity()=0; cout << "Seat Capacity : " << seats << endl;
}
virtual disp_details()=0;
};
};
Abstract Class and Pure Virtual Function – Example (cont.)

int main()
{
Bus b1;
b1.getdata();
b1.wheels_capacity();
b1.disp_details();
}
Exercises
• Exercise 1 : Add one more pure virtual function bankloan and two
classes Car and Bike. Display vehicle details and bank loan details of
different vehicle objects.
• Exercise 2 : Create base class for a battle game. Create derived classes
for Gunbattle and Airbattle. Use overridden concept in the derived
classes and display the details of base and derived objects.
• Exercise 3 : Create player class for the problem given in exercise 2.
Create function: involvebattle in player class that takes upcasted
Gunbattle or Airbattle objects and add score to the player.
• Exercise 4: Create IndianMovie as base class and Tamil and Telugu
movies as derived classes. Show the usage of early and late bindings
for the functions used in the classes.

Potrebbero piacerti anche