OOP Group A4
OOP Group A4
----------------------------------------------------------------------------------------------------------------
Assignment No: 3
----------------------------------------------------------------------------------------------------------------
Title of the Assignment: Imagine a publishing company which does marketing
for book and audio cassette versions. Create a class publication that stores the
title (a string) and price (type float) of publications. From this class derive two
classes: book which adds a page count (type int) and tape which adds a
playing time in minutes (type float). Write a program that instantiates the book
and tape class, allows user to enter data and displays the data members. If an
exception is caught, replace all the data member values with zero values
----------------------------------------------------------------------------------------------------------------
Prerequisite:
1. Object Oriented Programming
---------------------------------------------------------------------------------------------------------------
Objective of the Assignment: To learn the concept of inheritance and exception
handling.
---------------------------------------------------------------------------------------------------------------
Outcomes:
1. Students should able to understand the concept of Inheritance
2. Students shuld able to understand the concept of Exception handling and try to apply it.
---------------------------------------------------------------------------------------------------------------
Software & Hardware Requirements:
CPP Compiler
Department of Computer Engineering Subject : OOPCGL
---------------------------------------------------------------------------------------------------------------
Theory:
Inheritance:
Public:
This inheritance mode is used mostly. In this the protected member of Base class
becomes protected members of Derived class and public becomes public.
Protected:
In protected mode, the public and protected members of Base class becomes protected
members of Derived class.
Private:
In private mode the public and protected members of Base class become private members of
Derived class.
Types of Inheritance
In C++, we have 5 different types of Inheritance. Namely,
1. Single Inheritance
2. Multiple Inheritance
Department of Computer Engineering Subject : OOPCGL
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
Single Inheritance:
In this type of inheritance one derived class inherits from only one base class. It is the
most simplest form of Inheritance.
Syntax:
class subclass_name : access_modebase_class
{
//body of subclass
}; // Single Inheritence
#include <iostream>
usingnamespacestd;
classVehicle
{
public:
Vehicle()
{
cout<< "This is a Vehicle"<<endl;
}
};
classCar: publicVehicle
{
};
int main()
{
Car obj;
return0;
}
Output:
This is a vehicle
Multiple Inheritance:
In this type of inheritance a single derived class may inherit from two or more than two
base classes.
Syntax:
Department of Computer Engineering Subject : OOPCGL
// Multiple Inheritence
#include <iostream>
usingnamespacestd;
classVehicle {
public:
Vehicle()
{
cout<< "This is a Vehicle"<<endl;
}
};
classFourWheeler {
public:
FourWheeler()
{
cout<< "This is a 4 wheeler Vehicle"<<endl;
}
};
};
int main()
{
Car obj;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
Multilevel Inheritance:
In this type of inheritance the derived class inherits from a class, which in turn
inherits from some other class. The Super class for one, is sub class for the
other.
Department of Computer Engineering Subject : OOPCGL
// Multilevel Inheritance
#include <iostream>
using namespace std;
classVehicle
{
public:
Vehicle()
{
cout<< "This is a Vehicle"<<endl;
}
};
Class fourWheeler : public Vehicle
{ public:
fourWh
eeler()
{
cout<<"Objects with 4 wheels are vehicles"<<endl;
}
};
Class Car: public fourWheeler{
public:
car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};
int main()
{
Car obj;
return0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
Hierarchical Inheritance:
In this type of inheritance, multiple derived classes inherits from a single base class. //
Hierarchical Inheritance
classVehicle
{
Department of Computer Engineering Subject : OOPCGL
public:
Vehicle()
{
cout<< "This is a Vehicle"<<endl;
}
};
classCar: publicVehicle
{
};
classBus: publicVehicle
{
};
intmain()
{
Car obj1;
Bus obj2;
return0;
}
Output:
This is a Vehicle
This is a Vehicle
Hybrid Inheritance:
Hybrid Inheritance is combination of any 2 or more types of inheritances.
//Hybrid Inheritance
#include <iostream>
using namespace std;
classVehicle
{
public:
Vehicle()
{
cout<< "This is a Vehicle"<<endl;
}
};
classFare
{
public:
Department of Computer Engineering Subject : OOPCGL
Fare()
{
cout<<"Fare of Vehicle\n";
}
};
classCar: publicVehicle
{
};
classBus: publicVehicle, publicFare
{
};
int main({
Bus obj2;
return0;
}
Output:
This is a Vehicle
Fare of Vehicle
Exception Handling:
Exception handling is part of C++ and object oriented programming. they are added in C++ to
handle the unwanted situations during program execution. If we do not type the program
correctly then ot might result in errors. Main purpose of exception handling is to identify and
report the runtime error in the program.
Famous examples are divide by zero, array index out of bound error, file not found, device not
found, etc.
C++ exception handling is possible with three keywords iz. try, catch and throw. Exception
handling performs the following tasks:-
•Find the problem in the given code. It is also called as hit exception.
TRY:- It is block code in which there are chances of runtime error.This block is followed by one or
more catch block.Most error prone code is added in try block.
CATCH:- This is used to catch th exception thrown by the try blok. In catch block we take
corrective action on throwing exception. If files are openend , we can take corrective action like
closing file handles,closing database connetions,saving unsaved work ,etc.
Department of Computer Engineering Subject : OOPCGL
THROW:- Program throws exception when problem occurs.It is possible with throw
keyword. SNYTAX:=
...
...
}
//rest of the code
// Exception
#include
<iostream>
using
namespace
{
int x = -1;
// Some code
cout<< "Before try \n";
try {
cout<< "Inside try \n";
if (x < 0)
{
throw x;
cout<< "After throw (Never executed) \n";
Department of Computer Engineering Subject : OOPCGL
}
}
catch (int x ) {
cout<< "Exception Caught \n";
}
cout<< "After catch (Will be executed) \n";
return 0;
}
Output:
Before try
Inside try
Exception Caught
After catch (Will be executed)
Facilities:
Linux Operating Systems,G++
Algorithm:
1. Start.
4.Class Book having data members pages and member functions getdata() and pudata().
5. Class Tape having data members minutes and member functions getdata() and
pudata(). 6. Create an object bof class book and object t of class tape.
7. Stop.
I
: A class publication that stores the title (a string) and price (type float) of publications.
Derives two classes Book and Tape.
Display title and price from publication class. The result in following format:
Enter Title: OOP
Enter Price: 300
Enter Pages: 250
Department of Computer Engineering Subject : OOPCGL
1. What is Inheritance?
2. What are types of Inheritance?
3. What is Single Inheritance?
4. What is Multiple Inheritance?
5. What is Hierarchical Inheritance?
6. What is Multilevel Inheritance?
7. What is Hybrid Inheritance?
8. What is Exception handling?
9. What are try catch block of exception handling?