Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strooustrup - Hello World exercise

1 view
Skip to first unread message

arnuld

unread,
Oct 30, 2008, 6:05:25 AM10/30/08
to
Section 10.6, Exercise 15 ( I only added "return 0" to the original
main() ). Given this program:


#include <iostream>

int main()
{
std::cout << "Hello World!" << std::endl;

return 0;
}

modify it to produce the output:

Initialize
Hello World!
Clean up


Do not change main() in anyway.


I really don't understand how to go about it.

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups a.k.a "Spammer's Paradise" is Blocked.

Juan Antonio Zaratiegui Vallecillo

unread,
Oct 30, 2008, 6:07:50 AM10/30/08
to
arnuld wrote:
> Section 10.6, Exercise 15 ( I only added "return 0" to the original
> main() ). Given this program:
>
>
> #include <iostream>
>
> int main()
> {
> std::cout << "Hello World!" << std::endl;
>
> return 0;
> }
>
>
>
> modify it to produce the output:
>
> Initialize
> Hello World!
> Clean up
>
>
> Do not change main() in anyway.
>
>
> I really don't understand how to go about it.
>
Try to answer the following questions:

* What does std::cout mean or represent?
* What does << mean or represent?
* Whta does std::endl mean or represent?

Once you have answered those questions, you will know the answer to your
problem.

Best regards,

Zara

Eberhard Schefold

unread,
Oct 30, 2008, 6:25:24 AM10/30/08
to
Juan Antonio Zaratiegui Vallecillo wrote:

> * What does std::cout mean or represent?
> * What does << mean or represent?
> * Whta does std::endl mean or represent?
>
> Once you have answered those questions, you will know the answer to your
> problem.

I'm not sure whether you've really noticed the "Do not change main() in
any way" part.

Eberhard Schefold

unread,
Oct 30, 2008, 6:31:39 AM10/30/08
to
arnuld wrote:

> Section 10.6, Exercise 15 ( I only added "return 0" to the original
> main() ).

... which is not necessary, actually (main() is treated specially in
this regard).

> #include <iostream>
>
> int main()
> {
> std::cout << "Hello World!" << std::endl;
>
> return 0;
> }
>
> modify it to produce the output:
>
> Initialize
> Hello World!
> Clean up
>
>
> Do not change main() in anyway.

You know that the normal flow of the program goes through main(), so any
change should be forbidden. There is another mechanism in C++, though;
Objects can reside in a "space" somewhat outside the "normal flow". If
you remember what that space is, you should be able to fulfill the
requirement.

arnuld

unread,
Oct 30, 2008, 8:33:55 AM10/30/08
to
> On Thu, 30 Oct 2008 11:31:39 +0100, Eberhard Schefold wrote:

> You know that the normal flow of the program goes through main(), so any
> change should be forbidden. There is another mechanism in C++, though;
> Objects can reside in a "space" somewhat outside the "normal flow". If
> you remember what that space is, you should be able to fulfill the
> requirement.


You are talking about the "classes", which I don't know yet how to write.
However here is my half-knowledged and uncompilable attempt:

#include <iostream>


class Pre_sentence
{
public:
void print_initialize();
};


class Post_sentence
{
public:
void print_cleanup();
};


Pre_sentence print_begin;
Post_sentence print_end;


print_begin::print_initialize;

int main()
{
std::cout << "Hello World!" << std::endl;

return 0;
}


void Pre_sentence::print_initialize()
{
std::cout << "Initialize" << std::endl;
}

void Post_sentence::print_cleanup()
{
std::cout << "Clean up" << std::endl;

DJ Dharme

unread,
Oct 30, 2008, 8:48:37 AM10/30/08
to

You are so close to the answer, there are two special methods in a
class. If you find them you will know the answer.

olekk

unread,
Oct 30, 2008, 10:42:10 AM10/30/08
to

arnuld wrote:
> Section 10.6, Exercise 15 ( I only added "return 0" to the original
> main() ). Given this program:
>
>
> #include <iostream>
>
> int main()
> {
> std::cout << "Hello World!" << std::endl;
>
> return 0;
> }
>
>
>
> modify it to produce the output:
>
> Initialize
> Hello World!
> Clean up
>
>
> Do not change main() in anyway.
>
>
> I really don't understand how to go about it.
>

#include <iostream>

struct Initializer
{
Initializer() { std::cout<<"Initialize"<<std::endl;}
~Initializer(){ std::cout<<"Cleen up"<<std::endl;}
};

Initializer myInit;

int main()
{
std::cout << "Hello World!" << std::endl;

return 0;

}

Global object will be created before main executed and destroyed after

sean_in...@yahoo.com

unread,
Oct 30, 2008, 12:27:22 PM10/30/08
to
On Oct 30, 6:05 am, arnuld <sunr...@invalid.address> wrote:
> Section 10.6, Exercise 15 ( I only added "return 0" to the original
> main() ).

Note that unlike C, C++ has an implicit "return 0;" at the end.

A. Bolmarcich

unread,
Oct 30, 2008, 12:51:24 PM10/30/08
to
On 2008-10-30, arnuld <sun...@invalid.address> wrote:
>> On Thu, 30 Oct 2008 11:31:39 +0100, Eberhard Schefold wrote:
>
>> You know that the normal flow of the program goes through main(), so any
>> change should be forbidden. There is another mechanism in C++, though;
>> Objects can reside in a "space" somewhat outside the "normal flow". If
>> you remember what that space is, you should be able to fulfill the
>> requirement.
>
>
> You are talking about the "classes", which I don't know yet how to write.
> However here is my half-knowledged and uncompilable attempt:

In your opening post to this thread you mentioned "Section 10.6,
Exercise 15". Because chapter 10 is about Classes you need to know
how to write classes to be able to do the exercises for that chapter.
Informataion needed to do the exercise is in the subsection on
Construction and Destruction.

Juha Nieminen

unread,
Oct 30, 2008, 3:22:30 PM10/30/08
to
olekk wrote:
> Initializer myInit;

As a matter of principle, I would have written that as:

namespace { Initializer myInit; }

Or at the very least as:

static Initializer myInit;

You should abhor global variables, even if it doesn't matter. Just a
question of principles.

Default User

unread,
Oct 30, 2008, 6:06:35 PM10/30/08
to
sean_in...@yahoo.com wrote:

C does as well, as of the 1999 standard.


Brian

Juan Antonio Zaratiegui Vallecillo

unread,
Oct 31, 2008, 4:52:36 AM10/31/08
to

Oh sorry, you are right. I thought I had seen the usual homework question

arnuld

unread,
Oct 30, 2008, 4:38:05 PM10/30/08
to
> On Fri, 31 Oct 2008 09:52:36 +0100, Juan Antonio Zaratiegui Vallecillo

> Oh sorry, you are right. I thought I had seen the usual homework question


I don't get homework . I am no longer a kid ;)

arnuld

unread,
Oct 30, 2008, 4:41:28 PM10/30/08
to
> On Thu, 30 Oct 2008 11:51:24 -0500, A. Bolmarcich wrote:

> In your opening post to this thread you mentioned "Section 10.6,
> Exercise 15". Because chapter 10 is about Classes you need to know
> how to write classes to be able to do the exercises for that chapter.

Actually, I wanted to start my abandoned learning of C++ and my
friend's Stroustrup was lying in front of me on my office desk. My
experience has taught me I can not learn programming my following a book
from cover to cover. So I opened the book at some random place and tried
to solve the exercise that came in front of me.


> Informataion needed to do the exercise is in the subsection on
> Construction and Destruction.

How about this:


#include <iostream>


class Useless
{
public:
Useless() { std::cout << "Initialize" << std::endl; }
~Useless() { std::cout << "Clean up" << std::endl; }
};


Useless obj_of_useless;


int main()
{
std::cout << "Hello World!" << std::endl;

return 0;
}

=========================== OUTPUT ====================================
[arnuld@dune cpp]$ g++ -ansi -pedantic -Wall -Wextra 10-6_15.cpp
[arnuld@dune cpp]$ ./a.out

Initialize
Hello World!
Clean up

[arnuld@dune cpp]$

Juan Antonio Zaratiegui Vallecillo

unread,
Oct 31, 2008, 5:27:48 AM10/31/08
to
arnuld wrote:
>> On Fri, 31 Oct 2008 09:52:36 +0100, Juan Antonio Zaratiegui Vallecillo
>
>> Oh sorry, you are right. I thought I had seen the usual homework question
>
>
> I don't get homework . I am no longer a kid ;)
>
>
Lucky you! I am 45 and I do get full loads of homework. I work at home ;-)

gw7...@aol.com

unread,
Oct 31, 2008, 4:23:25 PM10/31/08
to
On 30 Oct, 20:41, arnuld <sunr...@invalid.address> wrote:
> How about this:
>
> #include <iostream>
>
> class Useless
> {
> public:
>   Useless()  { std::cout << "Initialize" << std::endl; }
>   ~Useless() { std::cout << "Clean up"   << std::endl; }
>
> };
>
> Useless obj_of_useless;
>
> int main()
> {
>   std::cout << "Hello World!" << std::endl;
>
>   return 0;
>
> }

Yes, that looks just the sort of thing they were after.

Paul.

blargg

unread,
Nov 1, 2008, 12:07:19 AM11/1/08
to
In article
<491f5749-f52b-4614...@k36g2000pri.googlegroups.com>,
gw7...@aol.com wrote:

> On 30 Oct, 20:41, arnuld <sunr...@invalid.address> wrote:
> > How about this:
> >
> > #include <iostream>
> >
> > class Useless
> > {
> > public:

> > =A0 Useless() =A0{ std::cout << "Initialize" << std::endl; }
> > =A0 ~Useless() { std::cout << "Clean up" =A0 << std::endl; }
> >
> > };
> >
> > Useless obj_of_useless;
> >
> > int main()
> > {
> > =A0 std::cout << "Hello World!" << std::endl;
> >
> > =A0 return 0;


> >
> > }
>
> Yes, that looks just the sort of thing they were after.

Unlike this questionable solution that defines no new classes:

#include <iostream>
#include <cstdlib>

namespace
{
void cleanup() { std::cout << "Clean up\n"; }

int init = (std::cout << "Initialize\n", std::atexit( cleanup ));
}

int main() { }

Stuart Golodetz

unread,
Nov 1, 2008, 7:54:50 PM11/1/08
to
arnuld wrote:
> Section 10.6, Exercise 15 ( I only added "return 0" to the original
> main() ). Given this program:
>
>
> #include <iostream>
>
> int main()
> {
> std::cout << "Hello World!" << std::endl;
>
> return 0;
> }
>
>
>
> modify it to produce the output:
>
> Initialize
> Hello World!
> Clean up
>
>
> Do not change main() in anyway.
>
>
> I really don't understand how to go about it.

Clearly the constructor/destructor way (see other posts) is the intended
way to go about it, but just for fun:

(a)

#include <iostream>

#define main f

int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}

#undef main
#define f main

int f()
{


std::cout << "Initialize" << std::endl;

std::cout << "Hello World!" << std::endl;

std::cout << "Clean up" << std::endl;

return 0;
}

(b)

#include <stdio.h>

namespace N { namespace std {

struct X {} cout;
struct Y {} endl;

X& operator<<(X& x, const char *p)
{
printf("Initialize\n%s\nClean up", p);
return x;
}

X& operator<<(X& x, const Y&)
{
printf("\n");
return x;
}

}}

using namespace N;

int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}

***

I'm not 100% sure whether the second one's legal or not (anyone?). I
know it's illegal to randomly add stuff to the std namespace
(specializations of operators etc. aside), but is my attempt to get
around that restriction (above) valid? I'm curious...

Cheers,
Stu

Juha Nieminen

unread,
Nov 2, 2008, 6:40:43 AM11/2/08
to
Stuart Golodetz wrote:

> arnuld wrote:
>> Do not change main() in anyway.
>>
>
> Clearly the constructor/destructor way (see other posts) is the intended
> way to go about it, but just for fun:
>
> (a)
>
> #include <iostream>
>
> #define main f
>
> int main()

The original problem says "do not change main() *in any way*". I
understand that to include using the preprocessor to change it.

arnuld

unread,
Nov 3, 2008, 12:40:31 AM11/3/08
to

I don't understand one thing here, constructor is called as soon as the
object is created but when does destructor is called:


1) when main() goes out of scope in the program. Its is not feasible
because in main() the "return 0" exits the program.

2) The destructor is called just before "return 0" ?


--
www.lispmachine.wordpress.com
my email is @ the above blog.

Google Groups is UnBlocked now :)


MPNIKHIL

unread,
Nov 3, 2008, 3:44:21 AM11/3/08
to

Cant one use operator overloading for accomplishing this?

Pete Becker

unread,
Nov 3, 2008, 7:56:10 AM11/3/08
to
On 2008-11-03 00:40:31 -0500, arnuld <sun...@invalid.address> said:

>
> I don't understand one thing here, constructor is called as soon as the
> object is created but when does destructor is called:
>
>
> 1) when main() goes out of scope in the program. Its is not feasible
> because in main() the "return 0" exits the program.
>
> 2) The destructor is called just before "return 0" ?

Standard-conforming code can't tell the difference, so from one
perspective, that distinction doesn't matter.

On a practical note, though, the destructor is called after the return
from main. In C, running a program effectively executes
exit(main(...)), and the exit function, in theory, deals with any
cleanup that's needed. In fact, there's usually quite a bit of code
that gets executed before entry into main and quite a bit that gets
executed after the return from main, and most of it isn't contained in
the exit function. Your compiler supplies whatever code is needed to
set up and tear down the program's environment, and that includes
running constructors and destructors.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Juha Nieminen

unread,
Nov 3, 2008, 2:49:54 PM11/3/08
to
arnuld wrote:
> 1) when main() goes out of scope in the program. Its is not feasible
> because in main() the "return 0" exits the program.

No, the "return 0" exits main(). There's a difference. Nothing stops
the compiler from executing code after main() has ended.

Stuart Golodetz

unread,
Nov 4, 2008, 4:45:04 AM11/4/08
to

I was working on the assumption that it meant "don't change the *text*
of main in any way". But you're right, it's open to a number of
different interpretations. If one wanted to be particularly annoying,
for instance, one could specify that that included not changing the
position of main within the file. I guess it's still possible in that
case: you just include a different header instead of iostream which has
a name with exactly the same length as iostream and includes it, e.g.

// wibble.h
#include <iostream>

// The extra code you actually wanted to put in the main file goes here

...

// main.cpp
#include "wibble.h"

int main()
{
...
}

But at that point it's starting to get a little silly! :-)

Stu

0 new messages