Constructor Problems

Discussion in 'C++' started by Wilson, Feb 12, 2007.

  1. Wilson

    Wilson Guest

    i am very new to c++ and am creating a new program, below are two
    seperate parts of the program, made to run seperately, however the
    constructor in one works (prints "constructing" on the screen), while
    the constructor in the other doesnt. Can you please help me understand
    why, i think they are almost identical.

    This one works:

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;

    class Student
    {
    public:
    Student()
    {
    cout << "constructing" << endl;
    a = 0;
    b = 0.0;
    }
    protected:
    int a;
    float b;
    };

    int main(int nNumberofArgs, char* pszArgs[])
    {
    cout << "Creating a new Student object" << endl;
    Student s;
    cout << "Creating a second student object" << endl;
    Student t;
    system("PAUSE");
    }
    ------------------------------------------------------------------------------------------------------------
    this one doesnt:

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;

    class test
    {
    public:
    test()
    {
    cout << "constructing";
    b = 0;
    a = 0;
    }
    protected:
    int a;
    int b;
    };
    int main(int nNumberofArgs, char* pszArgs[])
    {
    cout << "Creating new test object" << endl;
    test one;
    cout << "creating second test object" << endl;
    test two;

    system("PAUSE");
    }
    Wilson, Feb 12, 2007
    #1
    1. Advertising

  2. Wilson wrote:
    > i am very new to c++ and am creating a new program, below are two
    > seperate parts of the program, made to run seperately, however the
    > constructor in one works (prints "constructing" on the screen), while
    > the constructor in the other doesnt. Can you please help me understand
    > why, i think they are almost identical.


    Are you sure you posted the actual code that allegedly "doesn't work"?
    One of the common mistakes C++ programmers make is writing

    SomeClass object();

    to [try to] define an object and default-construct it, instead of the
    correct

    SomeClass object;

    Are you sure in your latter program you didn't write

    test one();
    or
    test two();

    ?

    >
    > This one works:
    >
    > #include <cstdio>
    > #include <cstdlib>
    > #include <iostream>
    > using namespace std;
    >
    > class Student
    > {
    > public:
    > Student()
    > {
    > cout << "constructing" << endl;
    > a = 0;
    > b = 0.0;
    > }
    > protected:
    > int a;
    > float b;
    > };
    >
    > int main(int nNumberofArgs, char* pszArgs[])
    > {
    > cout << "Creating a new Student object" << endl;
    > Student s;
    > cout << "Creating a second student object" << endl;
    > Student t;
    > system("PAUSE");
    > }
    > ------------------------------------------------------------------------------------------------------------
    > this one doesnt:
    >
    > #include <iostream>
    > #include <cstdlib>
    > #include <cstdio>
    > using namespace std;
    >
    > class test
    > {
    > public:
    > test()
    > {
    > cout << "constructing";
    > b = 0;
    > a = 0;
    > }
    > protected:
    > int a;
    > int b;
    > };
    > int main(int nNumberofArgs, char* pszArgs[])
    > {
    > cout << "Creating new test object" << endl;
    > test one;
    > cout << "creating second test object" << endl;
    > test two;
    >
    > system("PAUSE");
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 12, 2007
    #2
    1. Advertising

  3. Wilson

    Guest

    On Feb 12, 5:08 pm, "Wilson" <> wrote:
    > i am very new to c++ and am creating a new program, below are two
    > seperate parts of the program, made to run seperately, however the
    > constructor in one works (prints "constructing" on the screen), while
    > the constructor in the other doesnt. Can you please help me understand
    > why, i think they are almost identical.
    >
    > This one works:
    >
    > #include <cstdio>
    > #include <cstdlib>
    > #include <iostream>
    > using namespace std;
    >
    > class Student
    > {
    > public:
    > Student()
    > {
    > cout << "constructing" << endl;
    > a = 0;
    > b = 0.0;
    > }
    > protected:
    > int a;
    > float b;
    >
    > };
    >
    > int main(int nNumberofArgs, char* pszArgs[])
    > {
    > cout << "Creating a new Student object" << endl;
    > Student s;
    > cout << "Creating a second student object" << endl;
    > Student t;
    > system("PAUSE");}
    >
    > ------------------------------------------------------------------------------------------------------------
    > this one doesnt:
    >
    > #include <iostream>
    > #include <cstdlib>
    > #include <cstdio>
    > using namespace std;
    >
    > class test
    > {
    > public:
    > test()
    > {
    > cout << "constructing";
    > b = 0;
    > a = 0;
    > }
    > protected:
    > int a;
    > int b;};
    >
    > int main(int nNumberofArgs, char* pszArgs[])
    > {
    > cout << "Creating new test object" << endl;
    > test one;
    > cout << "creating second test object" << endl;
    > test two;
    >
    > system("PAUSE");
    >
    > }


    It would seem that you're running into buffered output problems.

    I'm assuming that PUAUSE is an external shell script or program that
    sleeps for a bit so you can examine output.

    Notice that you're not shifting std::endl into std::cout. std::endl
    has a std::flush included in it, thus flushing the buffer.

    To fix this, you can either include std::endl in the test constrctor,
    use std::cerr, or a host of other options. On a side note, you should
    usually use std::cerr for debugging like this. These buffered output
    problems will can cause lots of headache.

    On a rather minor detail, I believe std::cerr is actually line
    buffered. But, most debugging output is done one a per line basis so
    this really shouldn't be a big deal.

    The program below demonstrates the problem a bit better. It will print
    Constructing then wait 5 seconds before printing the second
    constructing. This is because at program close, all file descriptors
    are flushed. In your program I assume your using Ctrl-C to quite the
    program which won't flush the file output, hence it appears to not be
    printing.

    #include <iostream>

    class A
    {
    public:
    A()
    {
    std::cout << "Constructing." << std::endl ;
    }
    } ;

    class B
    {
    public:
    B()
    {
    std::cout << "Constructing" ;
    }
    } ;

    int
    main( int argc, char* argv[] )
    {
    A a ;
    B b ;
    sleep( 5 ) ;
    }

    HTH,
    Paul Davis
    , Feb 13, 2007
    #3
  4. Wilson

    Grizlyk Guest

    wrote:
    >
    > I'm assuming that PAUSE is an external shell script or program that
    > sleeps for a bit so you can examine output.


    I think PAUSE wait for key pressed. Following example do the same:

    #include<stdio.h>
    getchar();

    If <conio.h> is ported to your host as extention, getch() avaliable also:

    #include<conio.h>
    if(!getch())getch();

    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
    Grizlyk, Feb 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,043
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,335
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,818
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    465
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,225
Loading...

Share This Page