473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Constructor Problems

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");
}

Feb 12 '07 #1
3 1627
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
Feb 12 '07 #2
On Feb 12, 5:08 pm, "Wilson" <tpw...@googlemail.comwrote:
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 '07 #3

pa***************@gmail.com 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.his 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/
Feb 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
0
by: Queen | last post by:
Hi! First of all, perdon for my English. I don't express the thinks very good. Well, I'm working with eclipse 3.1.1, wxWidgets 2.4.2, MSYS 1.0.10 and Mingw 3.1.0. I'm doing a project in c++....
4
by: Chameleon | last post by:
------------------- class A { public: A() { blabla(); } A(int a) : A() { another_blabla(); } // why wrong? } ------------------- I want 2nd ctor, runs content of 1st ctor and after its...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
32
by: seank76 | last post by:
While going through my company's existing codebase, I saw a bunch of weird lines. Take a look at this one: class A { public: A(Foo *f) : _f(f) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
10
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.