Module VI - Advance C++ features
Inheritance (Multilevel inheritance – Multiple inheritance) –, Virtual Functions
and Polymorphism (Virtual base class – Abstract class –Virtual functions – pure
virtual functions - Compile time and Run time polymorphisms), Templates and
Exception Handling,, C++ File I/O- ( Opening and Closing a File - Reading and
Writing Text Files )
1. Inheritance
Simple Definition
Inheritance is an Object-Oriented Programming feature that allows one class to acquire the
properties and behaviors of another class.
It promotes:
Code reusability
Extensibility
Easy maintenance
Syntax
class Base
{
// members
};
class Derived : access_specifier Base
{
// members
};
Example
class Animal
{
public:
void eat()
{
cout<<"Animal eats"<<endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
cout<<"Dog barks"<<endl;
}
};
Program Example
#include<iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Animal eats"<<endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
cout<<"Dog barks"<<endl;
}
};
int main()
{
Dog d;
[Link]();
[Link]();
return 0;
}
Output
Animal eats
Dog barks
2. Multilevel Inheritance
Definition
In multilevel inheritance, a class is derived from another derived class.
Structure
Grandparent
↓
Parent
↓
Child
Syntax
class A { };
class B : public A { };
class C : public B { };
Example Program
#include<iostream>
using namespace std;
class Student
{
public:
void getStudent()
{
cout<<"Student Details"<<endl;
}
};
class Marks : public Student
{
public:
void getMarks()
{
cout<<"Marks Details"<<endl;
}
};
class Result : public Marks
{
public:
void display()
{
cout<<"Result Displayed"<<endl;
}
};
int main()
{
Result r;
[Link]();
[Link]();
[Link]();
return 0;
}
Output
Student Details
Marks Details
Result Displayed
3. Multiple Inheritance
Definition
A class inherits properties from more than one base class.
Structure
Teacher Researcher
\ /
\ /
Professor
Syntax
class A { };
class B { };
class C : public A, public B
{
};
Example Program
#include<iostream>
using namespace std;
class Teacher
{
public:
void teach()
{
cout<<"Teaching"<<endl;
}
};
class Researcher
{
public:
void research()
{
cout<<"Research Work"<<endl;
}
};
class Professor : public Teacher, public Researcher
{
};
int main()
{
Professor p;
[Link]();
[Link]();
return 0;
}
Output
Teaching
Research Work
Virtual Functions and Polymorphism
Learning Objectives
Students will be able to:
Understand polymorphism.
Differentiate compile-time and run-time polymorphism.
Use virtual functions.
Explain abstract classes and pure virtual functions.
4. Polymorphism
Definition
Polymorphism means "many forms."
The same function or operator behaves differently in different situations.
Types of Polymorphism
1. Compile-Time Polymorphism
Achieved using:
Function Overloading
Operator Overloading
2. Run-Time Polymorphism
Achieved using:
Virtual Functions
Compile-Time Polymorphism Example
Function Overloading
#include<iostream>
using namespace std;
class Addition
{
public:
int add(int a,int b)
{
return a+b;
}
float add(float a,float b)
{
return a+b;
}
};
int main()
{
Addition a;
cout<<"Integer Sum = "<<[Link](10,20)<<endl;
cout<<"Float Sum = "<<[Link](5.5f,2.5f);
return 0;
}
Output
Integer Sum = 30
Float Sum = 8
5. Virtual Functions
Definition
A virtual function is a member function declared using the keyword virtual in the base class.
It allows the derived class version of the function to be called using a base class pointer.
Syntax
virtual return_type function_name();
Example Program
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
cout<<"Base Class"<<endl;
}
};
class Derived : public Base
{
public:
void show()
{
cout<<"Derived Class"<<endl;
}
};
int main()
{
Base *ptr;
Derived d;
ptr=&d;
ptr->show();
return 0;
}
Output
Derived Class
6. Virtual Base Class
Definition
A virtual base class avoids duplicate copies of base class members during multiple inheritance.
It solves the Diamond Problem.
Diamond Problem
Person
/ \
Teacher Student
\ /
Assistant
Without virtual inheritance, Assistant gets two copies of Person.
Syntax
class Teacher : virtual public Person
{
};
class Student : virtual public Person
{
};
Example Program
#include<iostream>
using namespace std;
class Person
{
public:
void display()
{
cout<<"Person Details"<<endl;
}
};
class Teacher : virtual public Person
{
};
class Student : virtual public Person
{
};
class Assistant : public Teacher, public Student
{
};
int main()
{
Assistant a;
[Link]();
return 0;
}
Output
Person Details
7. Abstract Class
Definition
A class containing at least one pure virtual function is called an abstract class.
Objects cannot be created for abstract classes.
Syntax
class A
{
public:
virtual void show() = 0;
};
8. Pure Virtual Function
Definition
A virtual function initialized to zero is called a pure virtual function.
It forces derived classes to implement the function.
Example Program
#include<iostream>
using namespace std;
class Shape
{
public:
virtual void area()=0;
};
class Circle : public Shape
{
public:
void area()
{
cout<<"Area of Circle"<<endl;
}
};
int main()
{
Circle c;
[Link]();
return 0;
}
Output
Area of Circle
Module 3: Templates
Learning Objectives
Students will be able to:
Understand generic programming.
Create function templates.
Create class templates.
9. Templates
Definition
Templates allow writing generic programs that work with different data types.
They eliminate code duplication.
Function Template
Syntax
template <class T>
return_type function_name(T arguments)
{
}
Example Program
#include<iostream>
using namespace std;
template <class T>
maximum(T a,T b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
cout<<"Maximum = "<<maximum(10,20)<<endl;
cout<<"Maximum = "<<maximum(5.5,2.2);
return 0;
}
Output
Maximum = 20
Maximum = 5.5
Class Template
Syntax
template <class T>
class ClassName
{
};
Example Program
#include<iostream>
using namespace std;
template<class T>
class Display
{
T data;
public:
Display(T d)
{
data=d;
}
void show()
{
cout<<"Data = "<<data;
}
};
int main()
{
Display<int> d1(100);
[Link]();
return 0;
}
Output
Data = 100
Module 4: Exception Handling
Learning Objectives
Students will be able to:
Handle run-time errors.
Use try, throw, and catch blocks.
Improve program reliability.
10. Exception Handling
Definition
Exception handling is a mechanism used to handle run-time errors and prevent abnormal program
termination.
Keywords Used
try
Contains risky code.
throw
Generates an exception.
catch
Handles the exception.
Syntax
try
{
// code
throw exception;
}
catch(type variable)
{
// handler
}
Example Program
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers: ";
cin>>a>>b;
try
{
if(b==0)
throw b;
cout<<"Division = "<<a/b;
}
catch(int x)
{
cout<<"Division by zero not allowed";
}
return 0;
}
Output
Enter two numbers: 10 0
Division by zero not allowed
Module 5: C++ File I/O
Learning Objectives
Students will be able to:
Work with files in C++.
Open and close files.
Read and write text files.
11. File Handling
Definition
File handling allows data to be stored permanently in secondary storage.
File Stream Classes
Class Purpose
ifstream Read from file
ofstream Write to file
fstream Read and Write
Opening a File
Syntax
ofstream fout;
[Link]("[Link]");
Closing a File
Syntax
[Link]();
Writing to a Text File
Example Program
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
[Link]("[Link]");
fout<<"Name: Geetha"<<endl;
fout<<"Department: BCA";
[Link]();
cout<<"Data Written Successfully";
return 0;
}
Output
Data Written Successfully
[Link]
Name: Geetha
Department: BCA
Reading from a Text File
Example Program
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
string line;
[Link]("[Link]");
while(getline(fin,line))
{
cout<<line<<endl;
}
[Link]();
return 0;
}
Output
Name: Geetha
Department: BCA
Summary
Students learned:
✓ Inheritance and its types (Multilevel and Multiple)
✓ Compile-time and Run-time Polymorphism
✓ Virtual Functions, Virtual Base Classes
✓ Abstract Classes and Pure Virtual Functions
✓ Templates for Generic Programming
✓ Exception Handling using try–throw–catch
✓ File Handling using ifstream, ofstream, and fstream