[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Strooustrup - Hello World exercise

arnuld

10/30/2008 10:05:00 AM

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.

23 Answers

Juan Antonio Zaratiegui Vallecillo

10/30/2008 10:08:00 AM

0

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

10/30/2008 10:25:00 AM

0

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

10/30/2008 10:32:00 AM

0

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

10/30/2008 12:34:00 PM

0

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






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

DJ Dharme

10/30/2008 12:49:00 PM

0

On Oct 30, 5:33 pm, arnuld <sunr...@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:
>
> #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;
>
> }
>
> --www.lispmachine.wordpress.com
> my email is @ the above blog.
> Google Groups a.k.a "Spammer's Paradise" is Blocked.

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

10/30/2008 2:42:00 PM

0



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_raleigh

10/30/2008 4:27:00 PM

0

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

10/30/2008 4:51:00 PM

0

On 2008-10-30, arnuld <sunrise@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

10/30/2008 7:23:00 PM

0

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.

arnuld

10/30/2008 8:38:00 PM

0

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


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