473,781 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "constructi ng" 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 << "constructi ng" << 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 << "constructi ng";
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 1653
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 "constructi ng" 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 << "constructi ng" << 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 << "constructi ng";
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...@googlem ail.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 "constructi ng" 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 << "constructi ng" << 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 << "constructi ng";
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 << "Constructi ng." << std::endl ;
}
} ;

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

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())get ch();

--
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
3784
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
3581
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 one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
45
6368
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
0
1524
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++. I've got problems with one library, the compiler don't accept the multiply definition of the constructor. The compiler give me errors in the lines of the constructor definition. But it don't anything about what happends withs this lines. Anybody...
4
1624
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 content.
13
2471
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 certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
32
2237
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
3625
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 v);
13
3971
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 is much appreciated. JD
10
1911
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test { public: Test() { cout<<"Cons\n";} Test(Test& a) { cout<<"Copy cons\n";}
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8964
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.