Home Forums C Programming Errors in Program

Viewing 3 reply threads
  • Author
    Posts
    • #1996
      PRUTHVI KANTH
      Participant

      Question 1 (basic classes):
      Consider the following code.

      #include 
      using namespace std;
      class Book {
      Book(int numPages);
      void SetPages(int numPages);
      int GetPages() const;
      private:
      int pages;
      }
      Book::Book(int numPages) {
      pages = numPages;
      }
      Book::SetPages(int numPages) {
      pages = numPages;
      }
      int Book::GetPages() {
      return pages;
      }
      int main() {
      Book textbook;
      textbook.SetPages(970);
      int numPages = textbook.GetPages();
      cout << "The textbook has " << numPages << " pages." << endl;
      int ripout = 5;
      textbook.SetPages(numPages - ripout);
      cout << "I just tore out " << ripout << " pages." << endl;
      cout << "It now has " << textbook.GetPages << " pages left." << endl;
      return 0;
      }

      What errors are in this code? (There are at least 5.)

    • #3250
      S.Thananchayan
      Participant

      Ewwww thats got problems try this
       

      #include 
      template
      class MyBook {
       B1 book1;
       B2 book2;
      public:
       MyBook(B1 b1, B2 b2) : book1(b1), book2(b2)
           {  }
       template

       friend std::ostream& operator<<(std::ostream& os,
                                       const MyBook&);
      };
      template

      std::ostream& operator<<(std::ostream& os,
                               const MyBook& bb)
      {
       std::cout<<"My book has " << bb.book1 << ' ' << "pages" << std::endl;
       std::cout<<"I just tore out " << bb.book1 - bb.book2 << ' ' << "pages" << std::endl;
       std::cout<<"My book now has " << bb.book2 << ' ' << "pages" << std::endl;
       return os;
      }
      int main()
      {
       int a = 970;
       int b = 965;
       MyBook my_twothings(a, b);
       std::cout << my_twothings << std::endl;
       return 0;
      }
    • #3251
      PRUTHVI KANTH
      Participant

      You are great :)

    • #3252
      S.Thananchayan
      Participant

      Thanks~
      That idea came from Al Stevens.
      I just changed some of it.

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.