..........

closed account (yADjLyTq)
...................
Last edited on
You should post the code you have so far and ask specific questions.

This will get you started.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// .H File
class Book {
public:
 Book(); // Default Constructor
 ~Book(); // De-Constructor
 public void doSomething();
 public void setTitle(string value) { Title = value; }
 public string getTitle() { return Title; }

private:
string Title;
};

// .CPP File
#include "Book.h"

Book::Book() {
} // Default Constructor

void Book::doSomething() {
 // Do Something
}

Book::~Book() {
}
closed account (yADjLyTq)
.....
Last edited on
Ok. You should put it in [c0de] your code goes here [/c0de] tags (replace 0 with o) so it looks nice and formatted. What question do you have regarding your code?

That looks ok so far. I would be wary of only using 1 letter variable names (e.g. a, b, c, n , t, p) even as parameters. It will make it harder to understand the coder later.
Last edited on
closed account (yADjLyTq)
...........
Last edited on
closed account (yADjLyTq)
......
Last edited on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
class Book
{
private:
string title;
string pub;
int pages;
public:

//two constructor:
// - Default - initializes to The Aeneid, Penguin Company, 0
// - Overloaded - initializes to a, b, c
Book() : title("The Aeneid"), pub("Pengiun Company"), pages(0) {}
Book (string a, string b, int c) : title(a), pub(b), pages(c) {}

// accessors ("getters")
string getTitle() const { return title; }
string getPub() const { return pub; }
int getPages() const { return pages; }

// modifiers ("setters")
void setTitle(string t) { title = t; }
void setPub(string p) { pub = p; }
void setPages(int n) { pages = n; }
};

ostream& operator<<(ostream& output, const Book &B) {
    output << "(" <<  B.getTitle() << ", " << B.getPub() <<")";
    return output;  // for multiple << operators.
}

int main()
{ 
  Book newBook = Book();
  cout << newBook;
  return 0;
}
Last edited on
Overloading is nothing more than writing another function. Do you know what an overloaded function is? If not:

1
2
void foo( int x ) { cout << "foo( int x = " << x << " )" << endl; }
void foo( char c ) { cout << "foo( char c = " << c << " )" << endl; }


The function foo() is said to be overloaded, because there are actually multiple functions named "foo". They differ, in this case, only by the type of parameter they take.

Now for operators. Operators are functions that are called with special syntax. Normally, you use parentheses to call functions. For example, to use the above functions, foo( 4 ) says to call function foo with parameter 4.
You don't (typically) call operator functions with (). For example, if I write the following function:

1
2
3
4
bool operator==( unsigned u, string const& s ) 
{ 
     // Do something useful here...
}


Then I can call the above function like this:

1
2
3
4
5
6
string myStr = "Hello world!";
unsigned myUns = 12;

if( myUns == myStr ) // This calls the above operator== function
{
}



So you need to "overload" the "<<" operator for Book. There's a couple ways to do it. I'm not sure which way you were taught. The most general way is:

1
2
3
4
5
6
7
8
class Book {
   // ...
   template< typename charT, typename Traits > friend
   std::basic_ostream<charT, Traits>& operator<<(
      std::basic_ostream<charT, Traits>& os, Book const& book ) {
      // return os << book.title << ... etc
   }
};


Another less general way is:
1
2
3
4
5
6
class Book {
   // ...
   friend std::ostream& operator<<( std::ostream& os, Book const& book ) {
      // return os << book.title << ... etc
   }
};


I'm not sure if you were taught to write the function as a friend of the class or as a free function. I typically write them as friends because typically the output operator will want to output protected and/or private data that you'd otherwise have to write accessors for if you wrote operator<< as a non-friend free function.

Last edited on
closed account (yADjLyTq)
..........
Last edited on
closed account (yADjLyTq)
...........
Last edited on
Hi KUGIRL,

Y U have Edited to ur post to ........

:-O
Topic closed to new replies.