Strooustrup - Hello World exercise

A

arnuld

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.
 
J

Juan Antonio Zaratiegui Vallecillo

arnuld said:
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
 
E

Eberhard Schefold

Juan said:
* 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.
 
E

Eberhard Schefold

arnuld said:
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.
 
A

arnuld

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

DJ Dharme

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.
 
O

olekk

arnuld said:
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
 
J

Juha Nieminen

olekk said:
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.
 
A

arnuld

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

arnuld

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]$
 
J

Juan Antonio Zaratiegui Vallecillo

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

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

Juan Antonio Zaratiegui Vallecillo

arnuld said:
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 ;-)
 
G

gw7rib

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.
 
J

Juha Nieminen

Stuart said:
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.
 
A

arnuld

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

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" ?
 
M

MPNIKHIL

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

Cant one use operator overloading for accomplishing this?
 
J

Juha Nieminen

arnuld said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top