SHARE
TWEET

Untitled

a guest Sep 24th, 2019 147 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Book{
  2.   //field: isbnNo, title, price;
  3.   //string *autherPtr; int authorNo;
  4. public:
  5.   //write default constructor
  6.   //add other necessary member functions...
  7.   //declare necessary friend functions...
  8.   friend istream& operator>>(istream, Book&);
  9.   bool operator<(float f){ ... }
  10.   void operator+=(int x){...}  
  11.   float operator+(Book b){     }
  12.   friend float operator+(float, Book);
  13.  
  14. };
  15.  
  16. //need to overload >> operator to allow chaining
  17. //Should ask info for all the fields from user
  18.  
  19. //need to overload << operator to allow chaining
  20. //Should display all the fields of the object iff
  21. //the default values are replaced with user input
  22.  
  23. int main(){
  24.   int n, i; cout<<"How many books? "; cin>>n;
  25.   Book bookArr[n]; //an array of Book objects
  26.  
  27.   int noOfAuthors = populateBooks(bookArr, n);
  28.   //write this global function
  29.   //This function will replace the default values (set by constructor) of
  30.   //n Books by using cin>>BookObject format in a loop. Finally it returns
  31.   //total number of authors of all books of the array.
  32.  
  33.   cout<<”No of authors of all books is: “<<noOfAuthors<<endl;
  34.   cout << “Detail of the books are: “ << endl;
  35.   for(i=0; i<n ;i++) cout << bookArr[i] << endl;
  36.   int index = getBookIndexWithLowestPrice(bookArr, n);
  37.   int x; cout<<"% to increase lowest price: "; cin>>x;
  38.  
  39.   if(bookArr[index]<500.0) bookArr[index] += x;
  40.   //bookArr[index] is a book and if it’s
  41.   //price is <500 taka, then += will increase
  42.   //the book’s price by x%. Write necessary
  43.   //operator functions ( < , += )
  44.  
  45.   Book b1, b2, b3, b4; cin>>b1>>b2>>b3>>b4;
  46.  
  47.   //overload ‘+’ operator twice.
  48.   //‘+’ means adding price, not books
  49.   cout<<( (b1+b2+b3+b4 > 5000.0)
  50.     ? "Customer is given 10% discount."
  51.     : " customer is given 5% discount."
  52.   ) << endl;
  53.   return 0;
  54. }
  55. ----------------------
  56. //global
  57. istream& operator>>(istream& in, Book& b){
  58.       cin>>b.isbn; getline(in, b.title);
  59.       ....
  60.       cin>>authorNo;
  61.       autherPtr = new string[authorNo];
  62.       loop: authorNo times{
  63.      getline(in,autherPtr[i]);
  64.       }
  65.       return in;
  66. }
  67.  
  68. int populateBooks(Book arr[] , int size ){
  69.     int totalAuthNo=0;
  70.     loop: size times{
  71.         //arr[i].setBookInfo();
  72.         cin>>arr[i];
  73.         totalAuthNo += arr[i].getAuthorNo();
  74.     }
  75.     return totalAuthNo ;
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. b1+b2+b3+b4 > 5000.0
  83.  
  84. b1+b2 -> object+object return a float (say sum)
  85.  
  86. sum + b3
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top