#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Down Under
    Posts
    24
    Rep Power
    0

    standard template library


    guys

    im new to c++ and i have to write a program without using the standard template library in c++ ...

    can someone guide me a bit further cause im totally lost!! thanks in advance ...

    so for example, how can the following code be changed so that it does not use stl:

    std::cout << "TOT Create a new instance" << std::endl;

    Cheers

    mila :chomp:
  2. #2
  3. No Profile Picture
    Closet coder
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2005
    Location
    Plantation, FL <---south florida
    Posts
    1,431
    Rep Power
    153
    first, what exactly do you need to code?
    the STL is a collection of algorithms, container classes and such that GENERALIZE most programming theories and paradigms (things like linked lists, templates, queues, etc.)
    most programming classes teach you by having you write out your own version of these structures to give you a grasp on how these generalized mechanisms work.
    stuff like std:cout has to do with namespaces.
    so tell us what exactly is the problem at hand and we'll give you ideas on how to approach it or you give us what yoiu think you should do and we'll tell you if yer in the right direction or not
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    You could write your own output object if you like, but it will not be portable (well, most of it will be portable, but there will always be a little non-portable part). For instance:
    Code:
    #include <cstdio> // See notes below
    
    class myostream {
     public:
      void myputchar(const char &ch);
    };
    myostream mycout;
    
    myostream& operator<<(myostream& os, const char *s) {
      while (*s) {
        os.myputchar(*s);
        s++;
      }
      return os;
    }
    
    myostream& operator<<(myostream& os, const char &ch) {
      os.myputchar(ch);
      return os;
    }
    
    myostream& operator<<(myostream& os, const int& d) {
      int digit = d % 10;
      int quot = d / 10;
      if (quot > 0)
        os << quot;
      os.myputchar(digit + '0');
      return os;
    }
    
    void myostream::myputchar(const char &ch) {
      // OS specific code. I cheated here by using standard putchar for illustrative purposes
      std::putchar(ch);
    }
    
    int main(void) {
      mycout << "Hello " << "World\n" << 20 << '\n';
      return 0;
    }
    The above code declares a mycout object that can handle int, char and char * objects. You can extend it further to add other datatypes such as float, double etc. Note that I cheated a bit by using a standard function to implement myputchar. Those are the bold highlighted parts above. If you were to implement this without using the standard library, that would be the only non-portable part of the code. For instance, in DOS environment, you could implement it something like this:
    Code:
    void myostream::myputchar(const char &ch) {
       asm {
           MOV AH, 02
           MOV DL, ch
           INT 21
      }
    }
    As you can see, most of that code is portable, except for myputchar() :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    planet<earth_like_t>
    Posts
    365
    Rep Power
    149
    Originally Posted by mile1982
    so for example, how can the following code be changed so that it does not use stl:

    std::cout << "TOT Create a new instance" << std::endl;
    Well... considering that code doesn't use the STL, it'd be freaking' easy!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Down Under
    Posts
    24
    Rep Power
    0
    thanks for that mate ...
    much appreciated ....

    cheers
    mila :trockon:

    Originally Posted by Scorpions4ever
    You could write your own output object if you like, but it will not be portable (well, most of it will be portable, but there will always be a little non-portable part). For instance:
    Code:
    #include <cstdio> // See notes below
    
    class myostream {
     public:
      void myputchar(const char &ch);
    };
    myostream mycout;
    
    myostream& operator<<(myostream& os, const char *s) {
      while (*s) {
        os.myputchar(*s);
        s++;
      }
      return os;
    }
    
    myostream& operator<<(myostream& os, const char &ch) {
      os.myputchar(ch);
      return os;
    }
    
    myostream& operator<<(myostream& os, const int& d) {
      int digit = d % 10;
      int quot = d / 10;
      if (quot > 0)
        os << quot;
      os.myputchar(digit + '0');
      return os;
    }
    
    void myostream::myputchar(const char &ch) {
      // OS specific code. I cheated here by using standard putchar for illustrative purposes
      std::putchar(ch);
    }
    
    int main(void) {
      mycout << "Hello " << "World\n" << 20 << '\n';
      return 0;
    }
    The above code declares a mycout object that can handle int, char and char * objects. You can extend it further to add other datatypes such as float, double etc. Note that I cheated a bit by using a standard function to implement myputchar. Those are the bold highlighted parts above. If you were to implement this without using the standard library, that would be the only non-portable part of the code. For instance, in DOS environment, you could implement it something like this:
    Code:
    void myostream::myputchar(const char &ch) {
       asm {
           MOV AH, 02
           MOV DL, ch
           INT 21
      }
    }
    As you can see, most of that code is portable, except for myputchar() :).
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Down Under
    Posts
    24
    Rep Power
    0

    re:


    hey man

    thanks for the quick reply. just another question. in ur code,

    can u please explain why in
    void myostream::myputchar(const char &ch)
    you use "::"

    and why in
    myostream& operator<<(myostream& os, const int& d)
    you use "<<"

    thanks mate ...

    much appreciated
  12. #7
  13. Super User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    648
    Rep Power
    75
    Originally Posted by mile1982
    hey man

    thanks for the quick reply. just another question. in ur code,

    can u please explain why in
    void myostream::myputchar(const char &ch)
    you use "::"

    and why in
    myostream& operator<<(myostream& os, const int& d)
    you use "<<"

    thanks mate ...

    much appreciated
    When you implement a class member function, you use the classname followed by :: followed by the function name. For example:
    Code:
    //implementation of my_function()
    void my_class::my_function(int arg1)
    
    { std::cout << "my_function recieved one integer argument with the value of: " << arg1; }
    In this example, operator<< isn't a member function of myostream, its just a normal function; as you can see it isn't defined in the class definition. The compiler determines that in main(), the code:
    Code:
    mycout << "Hello " << "World\n" << 20 << '\n';
    amoung other things, streams a const char * ("Hello" and "World") to a myostream, and thus uses the function that was defined for operator<< with an argument of myostream and const char *.
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    >you use "::"
    scope resolution

    >you use "<<"
    operator overloading
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Down Under
    Posts
    24
    Rep Power
    0

    re:


    thanks for that guys ...

    greatly appreciated

IMN logo majestic logo threadwatch logo seochat tools logo