OOP Assign 3
OOP Assign 3
(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";
}
};
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";
}
};
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();
Tape t1;
t1.get_data();
t1.put_data();
return 0;
}
OUTPUT
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";
}
};
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";
}
};
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();
Tape t1;
t1.get_data();
t1.put_data();
return 0;
}
OUTPUT
(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();
Tape t1;
t1.get_data();
t1.put_data();
Disk d1;
d1.get_data();
d1.put_data();
return 0;
}
OUTPUT
(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();
}
};
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();
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