Thread: Trying to create valid home-made C++ functions.

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Trying to create valid home-made C++ functions.

    I included a .zip file in this post. The zip file contains one source code and one header file. No executables, because I know how must of you guys are scared to run those.

    Anyways, in the header file I created a class with an operator overloader using the macro <<(the same macro used for cout). I put it to good use in the main source code.

    I cheated a little in the operator<<. Inside I used printf(). In iostream there is no printf() to display text/numbers onto the console/screen.

    For all you veterans out there. Have any tips, suggestions or ideas to improve my home-made functions?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    small example of how this can be done properly
    Code:
    #include<iostream>
    using namespace std;
    
    class Point
    {
         int x;
         int y;
      public:
         Point(int X,int Y) : x(X),y(Y) {}
         friend ostream& operator << (ostream&,const Point&);
    };
    
    ostream& operator << (ostream& os,const Point& p)
    {
        os<<"( "<< p.x << "," << p.y << " )";
        return os; // enables cascading as above
    }
    
    int main()
    {
        Point p(10,20);
        cout<<p<<endl;
        return 0;
    }
    This is the way usually taught in books. When you have digested this then read this to see an even better way of doing it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create and display functions
    By jamie85 in forum C Programming
    Replies: 8
    Last Post: 03-19-2004, 07:19 AM
  2. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  3. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  4. Borland, libraries, and template member functions
    By roktsyntst in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2003, 09:52 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM