0% found this document useful (0 votes)
2 views4 pages

OOPp 3

The document contains a C++ program that defines a base class 'Publication' and two derived classes 'Book' and 'Tape'. It allows users to input and display details such as title, price, page count, and playing time, while also handling exceptions for negative values. The main function orchestrates the input and output of data for both classes, resetting values in case of an error.

Uploaded by

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

OOPp 3

The document contains a C++ program that defines a base class 'Publication' and two derived classes 'Book' and 'Tape'. It allows users to input and display details such as title, price, page count, and playing time, while also handling exceptions for negative values. The main function orchestrates the input and output of data for both classes, resetting values in case of an error.

Uploaded by

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

#include <iostream>

#include <string>

#include <stdexcept>

using namespace std;

// Base class

class Publication {

protected:

string title;

float price;

public:

Publication() : title(""), price(0.0f) {}

void inputData() {

cout << "Enter title: ";

getline(cin, title);

cout << "Enter price: ";

cin >> price;

if (price < 0) throw invalid_argument("Price cannot be negative.");

void displayData() const {

cout << "Title: " << title << "\nPrice: " << price << endl;

};

// Derived class for Book


class Book : public Publication {

private:

int pageCount;

public:

Book() : pageCount(0) {}

void inputData() {

Publication::inputData();

cout << "Enter page count: ";

cin >> pageCount;

if (pageCount < 0) throw invalid_argument("Page count cannot be negative.");

void displayData() const {

Publication::displayData();

cout << "Page Count: " << pageCount << endl;

};

// Derived class for Tape

class Tape : public Publication {

private:

float playingTime;

public:

Tape() : playingTime(0.0f) {}

void inputData() {
Publication::inputData();

cout << "Enter playing time (in minutes): ";

cin >> playingTime;

if (playingTime < 0) throw invalid_argument("Playing time cannot be negative.");

void displayData() const {

Publication::displayData();

cout << "Playing Time: " << playingTime << " minutes" << endl;

};

int main() {

try {

Book book;

cout << "Enter details for the book:\n";

book.inputData();

Tape tape;

cin.ignore();

cout << "\nEnter details for the tape:\n";

tape.inputData();

cout << "\nBook Details:\n";

book.displayData();

cout << "\nTape Details:\n";

tape.displayData();

} catch (const exception &e) {


cout << "Error: " << e.what() << endl;

Book book;

Tape tape;

cout << "\nAll data members have been reset to zero values.\n";

book.displayData();

tape.displayData();

return 0;

OUTPUT:

You might also like