1 Imagine a publishing company that markets both book and au
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().
2.) Various situations require that pairs of numbers be treated as a unit. For example, each screen coordinate has an x (horizontal) component and a y (vertical) component. Represent such a pair of numbers as a structure called pair that comprises two int member variables.
Now, assume you want to be able to store pair variables on a stack. That is, you want to be able to place a pair (which contains two integers) onto a stack using a single call to a push() function with a structure of type pair as an argument, and retrieve a pair using a single call to a pop() function, which will return a structure of type pair. Start with the Stack2 class in the STAKEN program in this chapter, and from it derive a new class called pairStack. This new class need contain only two members: the overloaded push() and pop() functions. The pairStack::push() function will need to make two calls to Stack2::push() to store the two integers in its pair, and the pairStack::pop() function will need to make two calls to Stack2::pop() (although not necessarily in the same orde
Solution
Answer 1
One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.
This also provides an opportunity to reuse the code functionality and fast implementation time.
#include<iostream>
#include<stdio.h>
using namespace std;
class publication {
char *title;
float price;
public:
publication(){
title = new char[256];
price = 0;
}
void getdata(){
cout<<\"input title and price\";
cin.getline(title, 256);
cin>>price;
}
void putdata(){
cout<<\"title:\"<<title<<endl;
cout<<\"price:\"<<price<<endl;
}
};
class book : public publication {
int count;
public:
void getdata(){
publication::getdata();
cout<<\"enter count of pages\";
cin>>count;
}
void putdata(){
publication::putdata();
cout<<\"page count:\"<<count<<endl;
}
};
class tape : public publication {
float time;
public:
void getdata(){
publication::getdata();
cout<<\"enter playing time in mnts\";
cin>>time;
}
void putdata(){
publication::putdata();
cout<<\"playing time in mnts:\"<<time<<endl;
}
};
int main(){
book *obj1 = new book;
tape *obj2 = new tape;
//Test class book
obj1->getdata();
obj1->putdata();
//Test class tape
obj2->getdata();
ond2->putdata();
return 0;
}
Answer 2
Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out),
where elements are inserted and extracted only from one end of the container.
Stack is a pre-defined class in C++ STL library.
// Example program
#include <iostream>
#include <string>
#include <stack>
using namespace std;
typedef struct pair1 {
int x;
int y;
}pair1;
int main (){
stack<pair1> myStack;
pair1 temp;
cout<<\"two integers in pair\ \";
cin>>temp.x>>temp.y;
// to add element to class
myStack.push(temp);
//to display the top;
cout<< myStack.top().x <<\" \"<<myStack.top().y;
//to delete elment from stack
myStack.pop();
return 0;
}


