0% found this document useful (0 votes)
39 views10 pages

OOP Group A4

1. The document describes an assignment to create a Book and Tape class that inherit from a Publication class. The Book class adds a page count and the Tape class adds a playing time. 2. The program is meant to instantiate the Book and Tape classes, allow the user to enter data, and display the data members. Any exceptions should replace the data with zero values. 3. The document also provides theory on object oriented programming concepts like inheritance, exception handling, and different types of inheritance like single, multiple, hierarchical and more.

Uploaded by

Aarti Thombare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

OOP Group A4

1. The document describes an assignment to create a Book and Tape class that inherit from a Publication class. The Book class adds a page count and the Tape class adds a playing time. 2. The program is meant to instantiate the Book and Tape classes, allow the user to enter data, and display the data members. Any exceptions should replace the data with zero values. 3. The document also provides theory on object oriented programming concepts like inheritance, exception handling, and different types of inheritance like single, multiple, hierarchical and more.

Uploaded by

Aarti Thombare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Computer Engineering Subject : OOPCGL

Correctness of Documentation of Timely Dated Sign of


Write-up Program Program Viva Completion Total Subject Teacher
4 4 4 4 4 10

Date of Performance:..................................... Expected Date of Completion:.......................


Actual Date of Completion:..........................................

----------------------------------------------------------------------------------------------------------------

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:

Inheritance in Object Oriented Programming can be described as a process of creating new


classes from existing classes. New classes inherit some of the properties and behavior of the
existing classes. An existing class that is "parent" of a new class is called a base class. New
class that inherits properties of the base class is called a derived class. Inheritance is a
technique of code reuse. It also provides possibility to extend existing classes by creating
derived classes.

The basic syntax of inheritance is:

Class DerivedClass : accessSpecifier BaseClass

There are 3 access specifiers:

Namely public, private and protected.

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

classsubclass_name : access_mode base_class1, access_mode base_class2, ....


{
//body of subclass
};

// 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;
}
};

classCar: publicVehicle, publicFourWheeler


{

};

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.

• It informs error has occurred. It is called as throwing the exception.

• We receive the roe info. It is called as catching the exception.


•It takes the corrective action.It is called as exception handling.

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:=

//normal program code


try{
throw exception
}
catch(argument)
{

...

...

}
//rest of the code

// Exception

#include

<iostream>

using

namespace

std; int main()

{
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.

2. Create classes Publication, book and tape.

3 .Publication class having data members title, price.

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

Enter Title: POP


Enter Price: 200
Enter Minutes: 60
Title: OOP
Price: 300
Pages: 250
Title: POP
Price: 200
Minutes: 60
Conclusion:
Hence, we have successfully studied concept of inheritance and exception
handling. Questions:

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?

You might also like