0% found this document useful (0 votes)
34 views

OOP Assign 3

The document describes adding a disk class to an existing publication hierarchy. The disk class would be derived from the publication base class. It would include the same member functions as other classes like book and tape. A unique data item for disk would be the disk type (CD or DVD), stored using an enum. The user could select the type by entering c or d. Tests are created to exercise the new disk class.

Uploaded by

Huzaifa Mughal
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)
34 views

OOP Assign 3

The document describes adding a disk class to an existing publication hierarchy. The disk class would be derived from the publication base class. It would include the same member functions as other classes like book and tape. A unique data item for disk would be the disk type (CD or DVD), stored using an enum. The user could select the type by entering c or d. Tests are created to exercise the new disk class.

Uploaded by

Huzaifa Mughal
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/ 21

Assignment # 03

Object Oriented Programming


Submitted to:
Mr. Naveed Abbas
Submitted By:

Name: Huzaifa Muqeet


Roll #: 22014198-051
Batch: BS-SE-22-A
Course code: SE-103
Question 01:

(a)
Imagine a publishing company that markets both book and audiocassette
versions of its works. Create a class publication that stores the title (a
string) and price (type float) of a publication. 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). Each of these three classes should
have a getdata() function to get its data from the user at the keyboard,
and a putdata() function to display its data.

Write a main() program to test the book and tape classes by creating
instances of them, asking the user to fill in data with getdata(), and then
displaying the data with putdata().

#include <iostream>
using namespace std;

class Publication
{
private:
    string title;
    float price;

public:
    Publication()
    {
        title = " ";
        price = 0.0;
    }
    void get_data()
    {
        cout << "Enter Title: ";
        getline(cin, title);
        cout << "Enter price: ";
        cin >> price;
        cout << "-------------------------------\n";
    }
    void put_data()
    {
        cout << "Title: " << title << "\nPrice: " << price << endl;
        cout << "-------------------------------\n";
    }
};

class Book : public Publication


{
private:
    int page_count;

public:
    Book()
    {
        page_count = 0;
    }
    void get_data()
    {
        Publication ::get_data();
        cout << "Enter page count: ";
        cin >> page_count;
        cout << "-------------------------------\n";
    }
    void put_data()
    {
        Publication ::put_data();
        cout << "Page count: " << page_count << endl;
        cin.ignore();
        cout << "-------------------------------\n";
    }
};

class Tape : public Publication


{
private:
    float play_time;

public:
    Tape()
    {
        play_time = 0.0;
    }
    void get_data()
    {
        Publication ::get_data();
        cout << "Enter play time(in minutes): ";
        cin >> play_time;
        cout << "-------------------------------\n";
    }
    void put_data()
    {
        Publication ::put_data();
        cout << "Play time: " << play_time << " minutes" << endl;
        cout << "-------------------------------\n";
    }
};

int main()
{
    system("cls");
    Book b1;
    b1.get_data();
    b1.put_data();

    cout << "===============================\n";

    Tape t1;
    t1.get_data();
    t1.put_data();
    return 0;
}

OUTPUT

Enter Title: Seven Habits Of Effective People


Enter price: 378.49
-------------------------------
Enter page count: 239
-------------------------------
Title: Seven Habits Of Effective People
Price: 378.49
-------------------------------
Page count: 239
-------------------------------
===============================
Enter Title: Wolverine
Enter price: 269.73
-------------------------------
Enter play time (in minutes): 40
-------------------------------
Title: Wolverine
Price: 269.73
-------------------------------
Play time: 40 minutes
-------------------------------
(b)

Start with the publication, book, and tape classes of Part a. Add a base
class sales that holds an array of three floats so that it can record the
dollar sales of a particular publication for the last three months. Include a
getdata() function to get three sales amounts from the user, and a
putdata() function to display the sales figures. Alter the book and tape
classes so they are derived from both publication and sales. An object of
class book or tape should input and output sales data along with its other
data. Write a main() function to create a book object and a tape object
and exercise their input/output capabilities.
#include <iostream>
using namespace std;

class Publication
{
private:
    string title;
    float price;

public:
    Publication()
    {
        title = " ";
        price = 0.0;
    }
    void get_data()
    {
        cout << "Enter Title: ";
        getline(cin, title);
        cout << "Enter price: ";
        cin >> price;
        cout << "-------------------------------\n";
    }
    void put_data()
    {
        cout << "Title: " << title << "\nPrice: " << price << endl;
        cout << "-------------------------------\n";
    }
};

class Sales
{
private:
    float m_sale[3];

public:
    void get_data()
    {
        cout << "Enter last 3 months sales\n";
        for (int i = 0; i < 3; i++)
        {
            cout << "Enter month " << i + 1 << " sale: ";
            cin >> m_sale[i];
        }
        cout << "-------------------------------\n";
    }
    void put_data()
    {
        cout << "Last 3 months sales:\n";
        for (int i = 0; i < 3; i++)
        {
            cout << "Month " << i + 1 << " sale: " << m_sale[i] << "$" <<
endl;
        }
        cout << "-------------------------------\n";
    }
};

class Book : public Publication, public Sales


{
private:
    int page_count;

public:
    Book()
    {
        page_count = 0;
    }
    void get_data()
    {
        Publication ::get_data();
        Sales ::get_data();
        cout << "Enter page count: ";
        cin >> page_count;
        cout << "-------------------------------\n";
    }
   
void put_data()
    {
        Publication ::put_data();
        Sales ::put_data();
        cout << "Page count: " << page_count << endl;
        cin.ignore();
        cout << "-------------------------------\n";
    }
};

class Tape : public Publication, public Sales


{
private:
    float play_time;

public:
    Tape()
    {
        play_time = 0.0;
    }
    void get_data()
    {
        Publication ::get_data();
        Sales ::get_data();
        cout << "Enter play time(in minutes): ";
        cin >> play_time;
        cout << "-------------------------------\n";
    }
    void put_data()
    {
        Publication ::put_data();
        Sales ::put_data();
        cout << "Play time: " << play_time << " minutes" << endl;
        cout << "-------------------------------\n";
    }
};
int main()
{
    system("cls");
    Book b1;
    b1.get_data();
    b1.put_data();

    cout << "===============================\n";

    Tape t1;
    t1.get_data();
    t1.put_data();
    return 0;
}

OUTPUT

Enter Title: Titanic


Enter price: 34.67
-------------------------------
Enter last 3 months sales
Enter month 1 sale: 437
Enter month 2 sale: 146
Enter month 3 sale: 715
-------------------------------
Enter page count: 467
-------------------------------
Title: Titanic
Price: 34.67
-------------------------------
Last 3 months sales:
Month 1 sale: 437$
Month 2 sale: 146$
Month 3 sale: 715$
-------------------------------

Page count: 467


-------------------------------
===============================
Enter Title: Final Farewell
Enter price: 164.59
-------------------------------
Enter last 3 months sales
Enter month 1 sale: 123
Enter month 2 sale: 456
Enter month 3 sale: 142
-------------------------------
Enter play time (in minutes): 38
-------------------------------
Title: Final Farewell
Price: 164.59
-------------------------------
Last 3 months sales:
Month 1 sale: 123$
Month 2 sale: 456$
Month 3 sale: 142$
-------------------------------
Play time: 38 minutes
-------------------------------

(c)
Assume that the publisher in Part a and b decides to add a third way to
distribute books: on computer disk, for those who like to do their reading
on their laptop. Add a disk class that, like book and tape, is derived from
publication. The disk class should incorporate the same member
functions as the other classes. The data item unique to this class is the
disk type: either CD or DVD. You can use an enum type to store this item.
The user could select the appropriate type by typing c or d.

#include <iostream>
using namespace std;
// base class 1
class Publication
{
private:
    string title;
    float price;

public:
    Publication()
    {
        title = " ";
        price = 0.0;
    }
    void get_data()
    {
        cout << "Enter Title: ";
        getline(cin, title);
        cout << "Enter price: ";
        cin >> price;
        cout << "-------------------------------------\n";
    }
    void put_data()
    {
        cout << "Title: " << title << "\nPrice: " << price << endl;
        cout << "-------------------------------------\n";
    }
};
// base class 2
class Sales
{
private:
    float m_sale[3];

public:
    void get_data()
    {
        cout << "Enter last 3 months sales\n";
        for (int i = 0; i < 3; i++)
        {
            cout << "Enter month " << i + 1 << " sale: ";
            cin >> m_sale[i];
        }
        cout << "-------------------------------------\n";
    }
    void put_data()
    {
        cout << "Last 3 months sales:\n";
        for (int i = 0; i < 3; i++)
        {
            cout << "Month " << i + 1 << " sale: " << m_sale[i] << "$" <<
endl;
        }
        cout << "-------------------------------------\n";
    }
};
// derived 1 from base 1,2
class Book : public Publication, public Sales
{
private:
    int page_count;

public:
    Book()
    {
        page_count = 0;
    }
    void get_data()
    {
        Publication ::get_data();
        Sales ::get_data();
        cout << "Enter page count: ";
        cin >> page_count;
    }
   
void put_data()
    {
        Publication ::put_data();
        Sales ::put_data();
        cout << "Page count: " << page_count << endl;
        cin.ignore();
    }
};
// derived class 2 from base 1,2
class Tape : public Publication, public Sales
{
private:
    float play_time;

public:
    Tape()
    {
        play_time = 0.0;
    }
    void get_data()
    {
        Publication ::get_data();
        Sales ::get_data();
        cout << "Enter play time(in minutes): ";
        cin >> play_time;
        cin.ignore();
    }
    void put_data()
    {
        Publication ::put_data();
        Sales ::put_data();
        cout << "Play time: " << play_time << " minutes" << endl;
    }
};
// derived class 3 from base 1,2
class Disk : public Publication, public Sales
{
private:
    // enumeration
    enum item
    {
        DVD,
        CD
    };
    item disk_type;
    char op;

public:
    void get_data()
    {
        Publication ::get_data();
        Sales ::get_data();
    line: // line
        cout << "Remember! Enter (c for CD or d for DVD)\nEnter (c or d): ";
        cin >> op;
        cout << "-------------------------------------\n";
        if (op == 'c' || op == 'C')
        {
            disk_type = CD;
        }
        else if (op == 'd' || op == 'D')
        {
            disk_type = DVD;
        }
        else
        {
            // line
            goto line;
        }
    }
    void put_data()
    {
        Publication ::put_data();
        Sales ::put_data();
        cout << "Disk type: ";
        if (disk_type == 0)
        {
            cout << "DVD\n";
        }
        else if (disk_type == 1)
        {
            cout << "CD\n";
        }
        cout << "-------------------------------------\n";
    }
};

int main()
{
    system("cls");
    Book b1;
    b1.get_data();
    b1.put_data();

    cout << "=====================================\n";

    Tape t1;
    t1.get_data();
    t1.put_data();

    cout << "=====================================\n";

    Disk d1;
    d1.get_data();
    d1.put_data();
    return 0;
}

OUTPUT

Enter Title: Human Psychology


Enter price: 49.99
-------------------------------------
Enter last 3 months sales
Enter month 1 sale: 400
Enter month 2 sale: 500
Enter month 3 sale: 200
-------------------------------------
Enter page count: 276
Title: Human Psychology
Price: 49.99
-------------------------------------
Last 3 months sales:
Month 1 sale: 400$
Month 2 sale: 500$
Month 3 sale: 200$
-------------------------------------
Page count: 276
=====================================
Enter Title: Lightning Strike
Enter price: 79.99
-------------------------------------
Enter last 3 months sales
Enter month 1 sale: 174
Enter month 2 sale: 294
Enter month 3 sale: 829
-------------------------------------
Enter play time (in minutes): 39
Title: Lightning Strike
Price: 79.99
-------------------------------------
Last 3 months sales:
Month 1 sale: 174$
Month 2 sale: 294$
Month 3 sale: 829$
-------------------------------------
Play time: 39 minutes
=====================================
Enter Title: The Conjuring
Enter price: 63.5
-------------------------------------
Enter last 3 months sales
Enter month 1 sale: 92
Enter month 2 sale: 85
Enter month 3 sale: 46
-------------------------------------
Remember! Enter (c for CD or d for DVD)
Enter (c or d): f
-------------------------------------
Remember! Enter (c for CD or d for DVD)
Enter (c or d): D
-------------------------------------
Title: The Conjuring
Price: 63.5
-------------------------------------
Last 3 months sales:
Month 1 sale: 46$
Month 2 sale: 85$
Month 3 sale: 46$
-------------------------------------
Disk type: DVD
-------------------------------------

(d)
Start with the publication, book, and tape classes of Part a. Suppose you
want to add the date of publication for both books and tapes. From the
publication class, derive a new class called publication2 that includes this
member data. Then change book and tape so they are derived from
publication2 instead of publication. Make all the necessary changes in
member functions so the user can input and output dates along with the
other data. For the dates, you can have three attributes, day, month and
year and functions to input and display it in date format.

#include <iostream>
using namespace std;

class Publication
{
private:
    string title;
    float price;

public:
    Publication()
    {
        title = " ";
        price = 0.0;
    }
    void get_data()
    {
        cout << "Enter Title: ";
        getline(cin, title);
        cout << "Enter price: ";
        cin >> price;
        cout << "------------------------------------\n";
    }
    void put_data()
    {
        cout << "Title: " << title << "\nPrice: " << price << endl;
    }
};
class Publication2 : public Publication
{
private:
    int day, month, year;

public:
    void get_data()
    {
        Publication ::get_data();
        cout << "Enter date (DD/MM/YEAR): ";
        cin >> day >> month >> year;
        cout << "------------------------------------\n";
    }
    void put_data()
    {
        Publication ::put_data();
        cout << "Date of Publication: " << day << "/" << month << "/" << year
<< endl;
    }
};
class Book : public Publication2
{
private:
    int page_count;

public:
    Book()
    {
        page_count = 0;
    }
    void get_data()
    {
        Publication2 ::get_data();
        cout << "Enter page count: ";
        cin >> page_count;
        cout << "------------------------------------\n";
    }
    void put_data()
    {
        Publication2 ::put_data();
        cout << "Page count: " << page_count << endl;
        cin.ignore();
    }
};

class Tape : public Publication2


{
private:
    float play_time;

public:
    Tape()
    {
        play_time = 0.0;
    }
    void get_data()
    {
        Publication2 ::get_data();
        cout << "Enter play time(in minutes): ";
        cin >> play_time;
        cout << "------------------------------------\n";
    }
    void put_data()
    {
        Publication2 ::put_data();
        cout << "Play time: " << play_time << " minutes" << endl;
    }
};

int main()
{
    system("cls");
    Book b1;
    b1.get_data();
    b1.put_data();

    cout << "====================================\n";

    Tape t1;
    t1.get_data();
    t1.put_data();
    return 0;
}

OUTPUT
Enter Title: Incidious
Enter price: 33.4
------------------------------------
Enter date (DD/MM/YEAR): 02/07/2023
------------------------------------
Enter page count: 313
------------------------------------
Title: Incidious
Price: 33.4
Date of Publication: 02/07/2023
Page count: 313
====================================
Enter Title: Call of the wild
Enter price: 89.5
------------------------------------
Enter date (DD/MM/YEAR): 06/08/2023
------------------------------------
Enter play time (in minutes): 67
------------------------------------
Title: Call of the wild
Price: 89.5
Date of Publication: 06/08/2023
Play time: 67 minutes

You might also like