Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [C++-tut] Making << Work With Your Own Types
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Tue Nov 15, 2005 1:59 am   Post subject: [C++-tut] Making << Work With Your Own Types

We've all seen the << operator used with things like ints, floats, and strings.

But is there any way we can use some weird type of our own creation with it?

Let's say we create a one of my very simple Name classes.

c++:
class Name
{
   private:
      std::string first_name, last_name;
   public:
      Name(std::string f, std::string l)
      : first_name(f), last_name(l)
      {
      }

      std::string first() const
      {
         return first_name;
      }

      std::string last() const
      {
         return last_name;
      }

      std::string full_name() const
      {
         return first_name + " " + last_name;
      }
};


And now, we know that operators are just functions, and that we can overload functions based on argument type, so we can easily write an overloaded version of the << operator.

c++:
std::ostream& operator<<(std::ostream& out, const Name& n)
{
   return out << n.full_name();
}


Then we can create a Name and output it easily.


c++:
Name bobs_name("Bob", "Smith");

std::cout << bobs_name << std::endl;


Convenient, eh?
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Sun Jan 22, 2006 11:18 pm   Post subject: (No subject)

That's nifty, I knew they were functions but I never dreamed you could overload them. Wink I think I forgot to include this one in the walkthrough, please let me know if there are any more I missed.
Justin_




PostPosted: Tue Jan 24, 2006 2:27 pm   Post subject: (No subject)

But, since this is a tutorial you should maybe 'tutor' a little. You use a function "operator" in your code. Those who are not familiar with overloading operators like: "<<", and those you forgot to mention: "+" "+=" and the list goes on... Would not know what this keyword does. You should explain it in a little more detail, or rather, you should at least explain it somewhat. . .

I mean since you can correct me, it's only fair that I can correct you too. A tutorial should expound on new concepts. "operator" is key to what this tutorial is on. You should explain it.
wtd




PostPosted: Tue Jan 24, 2006 6:04 pm   Post subject: (No subject)

The operator keyword designates an operator.

I don't like to do too much hand-holding.
md




PostPosted: Wed Jan 25, 2006 11:34 am   Post subject: (No subject)

I think wtd's tutorial style works rather well. It's simple and strait forward if you are willing to put some effort into your own learning, while at the same time get's some pretty nifty (as wtd would say Wink) stuff across.

Making tutorials too simple to understand doesn't help anyone. Learning works best if it is a challenge and if you have to think about what you are learning, otherwise it'll just go in one ear and out the other.
Null




PostPosted: Wed May 24, 2006 9:17 pm   Post subject: (No subject)

I would also add that if you make an overloaded operator such as << a friend, you can access private fields of the class.

For example (haven't figured out that fancy C++ highlighting yet Wink ):

code:

#include <string>
#include <iostream>

class Name {
        private:
                std::string first;
                std::string last;
        public:
                Name(const std::string &first_n, const std::string &last_n)
                        : first(first_n), last(last_n) {
                }

                friend std::ostream &operator<<(std::ostream &os, const Name &name);
                const std::string &getFirst() const { return first; }
                const std::string &getLast() const { return last; }
};

std::ostream &operator<<(std::ostream &os, const Name &name) {
        os << name.first << " " << name.last;
        return os;
}

int main() {
        Name name("Joe", "Smith");

        std::cout << name << std::endl;
        return 0;
}


OUTPUT
Joe Smith
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: