Posts

Showing posts from June, 2021

Chapter:9 Inheritance, solved Exercise question book by Robert Lafore

Image
  OOP BY ROBERT LOFORE 4th Edition Chapter:9 Inheritance Question no 5  :   Derive a class called employee2 from the employee class in the EMPLOY program in this chapter. This new class should add a type double data item called compensation, and also an enum type called period to indicate whether the employee is paid hourly, weekly, or monthly. For simplicity you can change the manager, scientist, and laborer classes so they are derived from employee2 instead of employee. However, note that in many circumstances it might be more in the spirit of OOP to create a separate base class called compensation and three new classes manager2, scientist2, and laborer2, and use multiple inheritance to derive these three classes from the original manager, scientist, and laborer classes and from compensation. This way none of the original classes needs to be modified. Program source code: #include <iostream> #include <conio.h> using namespace std; const int LEN = 80...

Chapter:9 Inheritance, solved Exercise question book by Robert Lafore

Image
  OOP BY ROBERT LOFORE 4th Edition Chapter:9 Inheritance Question no 4: Assume that the publisher in Exercises 1 and 3 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 .   Program source code: #include <iostream> #include <string> #include <conio.h> using namespace std; class publication { private:  string title;  float price; public:  void getdata(void)  {   string t;   float p;   cout << "Enter title of publication: ";   cin >> t;   cout << "Enter price of publication: ";   cin >> p;...

Chapter:9 Inheritance, solved Exercise question book by Robert Lafore

Image
  OOP BY ROBERT LOFORE 4th Edition Chapter:9 Inheritance Question no 3  : Start with the publication, book, and tape classes of Exercise 1. 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.  Program Source code: # include <iostream> #include <string> #include <conio.h> using namespace std; class publication { private: string title; float price; public: void getdata(void) { string t; float p; cout << "Enter title of publication: "; cin...

Chapter:9 Inheritance, solved Exercise question book by Robert Lafore

Image
  OOP BY ROBERT LOFORE 4th Edition Chapter:9 Inheritance Question no 2;   Recall the STRCONV example from Chapter 8. The String class in this example has a flaw: It does not protect itself if its objects are initialized to have too many characters. (The SZ constant has the value 80.) For example, the definition String s = “This string will surely exceed the width of the “ “screen, which is what the SZ constant represents.”; will cause the str array in s to overflow, with unpredictable consequences, such as crashing the system. With String as a base class, derive a class Pstring (for “protected string”) that prevents buffer overflow when too long a string constant is used in a definition. A new constructor in the derived class should copy only SZ–1 characters into str if the string constant is longer, but copy the entire constant if it’s shorter. Write a main() program to test different lengths of strings.  Program Source code: #include <iostream> #include <con...

Chapter:9 Inheritance, solved Exercise question book by Robert Lafore

Image
  OOP BY ROBERT LOFORE 4th Edition Chapter:9 Inheritance Question no 1   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(). Program Source code: #include <iostream> #include<string> using namespace std; class publication {   protected:   string title;   float price;     public:   publication()   { ...