Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A complex c++ question.

A complex c++ question.

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
14 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Imran Farooqui

    #include "stdafx.h"

    using std::cout;
    using std::endl;

    class CTest
    {
    public:

    CTest()
    {
    cout << "Hello John bye" << endl;

     exit(0);
    

    }

    };

    int main(int argc, char* argv[])
    {
    cout << "John" << endl;

    return 0;
    }

    CTest test;

    C Offline
    C Offline
    cmk
    wrote on last edited by
    #5

    What about as Imran had but :

    CTest()
    {
    cout << "Hello ";
    }

    ~CTest()
    {
    cout << " Bye";
    }

    ... although you should really do your own homework. ;) [edit] Hmmm, this doesn't print the " Bye", altough the code goes there. Even adding cout.flush() doesn't print it. Looks like ostream system has been shut down by then. Using printf() does work in printing " Bye", but you need to eat the \n first, and adding \b doesn't seem to work ... hmmm [/edit] ...cmk Save the whales - collect the whole set

    C L 2 Replies Last reply
    0
    • A Anonymous

      Hi Imran, It will print ,"Hi John bye John" but i need "Hi john bye".

      I Offline
      I Offline
      Imran Farooqui
      wrote on last edited by
      #6

      Please refer the code i wrote above: In the constructor of CTest class, there is an exit(0) call. So this program will never enter the main function. Trick is that, in C++, global objects are created before program enters the main function. So we created the object of CTest class as a global object. As such its constructor gets called in which we first print our desired string and then exit(0) gets calls and the program ends.

      1 Reply Last reply
      0
      • A Antti Keskinen

        Based on Imran's solution, here is a bit more customized version which does as desired.

        #include <iostream>
        using namespace std;

        class CFirst
        {
        public:
        CFirst() { cout << "Hi "; }
        }

        class CLast
        {
        public:
        CLast() { cout << " Bye"; }
        }

        CFirst object1;

        int main( int argc, char* argv[] )
        {
        cout << "John" << endl;
        return 0;
        }

        CLast object2;

        This piece would print "Hi John\n Bye". Removing the line change from inside main is impossible, as far as I know, without doing some serious stream interception. All this seems unnecessary though. Just boot the line change from there, or pretend that you didn't notice it :P -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #7

        Actually this will/may put : "Hi ByeJohn" because both objects are initialized (constructors called) before main(). I say may, because as far as i know you can't count on the order that global objects will be initialized within a given init_seg. Having said that you could use the #pragma init_seg() directives to put CFirst, CLast and main in different .cpp files and use init_seg(lib) for CFirst and init_seg(user) for CLast to ensure the order they were constructed ... but that seems like a whole lot of work to still get the wrong output. :) ...cmk Save the whales - collect the whole set

        1 Reply Last reply
        0
        • C cmk

          What about as Imran had but :

          CTest()
          {
          cout << "Hello ";
          }

          ~CTest()
          {
          cout << " Bye";
          }

          ... although you should really do your own homework. ;) [edit] Hmmm, this doesn't print the " Bye", altough the code goes there. Even adding cout.flush() doesn't print it. Looks like ostream system has been shut down by then. Using printf() does work in printing " Bye", but you need to eat the \n first, and adding \b doesn't seem to work ... hmmm [/edit] ...cmk Save the whales - collect the whole set

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #8

          Couldn't remember the console cursor movement functions (ala curses). However, here is a hack that works (and is within the contraints given :)), but not likely the solution you need :

          #include
          using namespace std;

          class CFirst{
          public:
          CFirst() {
          cout << "Hi ";
          }
          ~CFirst() {
          printf("Bye\n");
          }
          };
          CFirst f;

          #define endl ' '

          int main( int argc, char* argv[] )
          {
          cout << "John" << endl;
          return 0;
          }

          ...cmk Save the whales - collect the whole set

          1 Reply Last reply
          0
          • C cmk

            What about as Imran had but :

            CTest()
            {
            cout << "Hello ";
            }

            ~CTest()
            {
            cout << " Bye";
            }

            ... although you should really do your own homework. ;) [edit] Hmmm, this doesn't print the " Bye", altough the code goes there. Even adding cout.flush() doesn't print it. Looks like ostream system has been shut down by then. Using printf() does work in printing " Bye", but you need to eat the \n first, and adding \b doesn't seem to work ... hmmm [/edit] ...cmk Save the whales - collect the whole set

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #9

            Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual

            C J A 3 Replies Last reply
            0
            • L Lost User

              Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #10

              Not laughing, as valid a solution as any, given the constraints. :) ...cmk Save the whales - collect the whole set

              1 Reply Last reply
              0
              • L Lost User

                Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual

                J Offline
                J Offline
                Joaquin M Lopez Munoz
                wrote on last edited by
                #11

                That's the kind of solution that pisses teachers off. I love it. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                1 Reply Last reply
                0
                • L Lost User

                  Hello all, please don't laugh :laugh: but here is one solution #include #define cout cout << "Hello " #define endl " Bye" << endl int main(int argc, char* argv[]) { cout << "John" << endl; return 1; } :rolleyes: When all else fails read the manual

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #12

                  hi, this solution should result in compilation error, although i haven't checked it but we can't apply #define on the cout and endl like this.

                  A 1 Reply Last reply
                  0
                  • A Anonymous

                    hi, this solution should result in compilation error, although i haven't checked it but we can't apply #define on the cout and endl like this.

                    A Offline
                    A Offline
                    Antti Keskinen
                    wrote on last edited by
                    #13

                    It isn't doable, because you #define cout as a part of itself.. Like, what you're doing is:

                    #define MAX_AMOUNT MAX_AMOUNT + 20

                    Similar type of error. Man I really wish that that thing would've worked, it would've been a kickass "spoil-a-teacher's-day" for some C++-course :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

                    M 1 Reply Last reply
                    0
                    • A Antti Keskinen

                      It isn't doable, because you #define cout as a part of itself.. Like, what you're doing is:

                      #define MAX_AMOUNT MAX_AMOUNT + 20

                      Similar type of error. Man I really wish that that thing would've worked, it would've been a kickass "spoil-a-teacher's-day" for some C++-course :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

                      M Offline
                      M Offline
                      Monty2
                      wrote on last edited by
                      #14

                      IT REALLY WORKS like a charm , go ahead try to compile , it works !!! ;P When All Else Fails Read the Manual

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups