Home › Forums › C Programming › Errors in Program
- This topic has 3 replies, 2 voices, and was last updated 17 years, 4 months ago by S.Thananchayan.
Viewing 3 reply threads
- AuthorPosts
- August 11, 2007 at 10:03 pm #1996PRUTHVI KANTHParticipant
Question 1 (basic classes):
Consider the following code.1234567891011121314151617181920212223242526272829#include <iostream><br />using namespace std;<br />class Book {<br />Book(int numPages);<br />void SetPages(int numPages);<br />int GetPages() const;<br />private:<br />int pages;<br />}<br />Book::Book(int numPages) {<br />pages = numPages;<br />}<br />Book::SetPages(int numPages) {<br />pages = numPages;<br />}<br />int Book::GetPages() {<br />return pages;<br />}<br />int main() {<br />Book textbook;<br />textbook.SetPages(970);<br />int numPages = textbook.GetPages();<br />cout << "The textbook has " << numPages << " pages." << endl;<br />int ripout = 5;<br />textbook.SetPages(numPages - ripout);<br />cout << "I just tore out " << ripout << " pages." << endl;<br />cout << "It now has " << textbook.GetPages << " pages left." << endl;<br />return 0;<br />}</iostream>What errors are in this code? (There are at least 5.)
- August 13, 2007 at 10:11 am #3250S.ThananchayanParticipant
Ewwww thats got problems try this
1234567891011121314151617181920212223242526272829#include <iostream><br />template<class B1, class B2><br />class MyBook {<br />B1 book1;<br />B2 book2;<br />public:<br />MyBook(B1 b1, B2 b2) : book1(b1), book2(b2)<br />{ }<br />template</class><class B3, class B4><br />friend std::ostream& operator<<(std::ostream& os,<br />const MyBook<B3, B4>&);<br />};<br />template</class><class B1, class B2><br />std::ostream& operator<<(std::ostream& os,<br />const MyBook<B1, B2>& bb)<br />{<br />std::cout<<"My book has " << bb.book1 << ' ' << "pages" << std::endl;<br />std::cout<<"I just tore out " << bb.book1 - bb.book2 << ' ' << "pages" << std::endl;<br />std::cout<<"My book now has " << bb.book2 << ' ' << "pages" << std::endl;<br />return os;<br />}<br />int main()<br />{<br />int a = 970;<br />int b = 965;<br />MyBook<int,double> my_twothings(a, b);<br />std::cout << my_twothings << std::endl;<br />return 0;<br />}</class></iostream> - August 16, 2007 at 12:05 am #3251PRUTHVI KANTHParticipant
You are great :)
- August 17, 2007 at 9:57 am #3252S.ThananchayanParticipant
Thanks~
That idea came from Al Stevens.
I just changed some of it.
- AuthorPosts
Viewing 3 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.