473,397 Members | 2,028 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,397 software developers and data experts.

A question in TC++PL!

Hi,

In TC++PL, Chapter 10, there is an exercise which ask us to modify the
following program:

#include <iostream>

int main()
{
std::cout << "Hello, world!/n" ;
return 0;
}

so that the output would be:

Initialize
Hello, world!
Clean up

but we should not change main().

I believe we will have to modify the constructor and desctructor of class
"ostream" so that the constructor of "ostream" will output
Initialize
and the destructor of "ostream" will output
Clean up

But I don't know if my idea is correct. Is there better idea? Thanks!
Aug 4 '07 #1
3 1731
"Guofu Chen" <gc***@mathworks.comwrote in message
news:f9**********@fred.mathworks.com...
Hi,

In TC++PL, Chapter 10, there is an exercise which ask us to modify the
following program:

#include <iostream>

int main()
{
std::cout << "Hello, world!/n" ;
return 0;
}

so that the output would be:

Initialize
Hello, world!
Clean up

but we should not change main().

I believe we will have to modify the constructor and desctructor of class
"ostream" so that the constructor of "ostream" will output
Initialize
and the destructor of "ostream" will output
Clean up

But I don't know if my idea is correct. Is there better idea? Thanks!
One simple way:

#include <iostream>

class Foo
{
public:
Foo() { std::cout << "Initialize\n"; }
~Foo() { std::cout << "Cleanup\n"; }
};

Foo Bar;

int main()
{
std::cout << "Hello, world!\n" ;
return 0;
}

Although I'm not sure what they're actually looking for.
Aug 4 '07 #2
On 2007-08-04 22:56, Jim Langston wrote:
"Guofu Chen" <gc***@mathworks.comwrote in message
news:f9**********@fred.mathworks.com...
>Hi,

In TC++PL, Chapter 10, there is an exercise which ask us to modify the
following program:

#include <iostream>

int main()
{
std::cout << "Hello, world!/n" ;
return 0;
}

so that the output would be:

Initialize
Hello, world!
Clean up

but we should not change main().

I believe we will have to modify the constructor and desctructor of class
"ostream" so that the constructor of "ostream" will output
Initialize
and the destructor of "ostream" will output
Clean up
Certainly not, you should never attempt to modify the standard library!
>But I don't know if my idea is correct. Is there better idea? Thanks!

One simple way:

#include <iostream>

class Foo
{
public:
Foo() { std::cout << "Initialize\n"; }
~Foo() { std::cout << "Cleanup\n"; }
};

Foo Bar;

int main()
{
std::cout << "Hello, world!\n" ;
return 0;
}

Although I'm not sure what they're actually looking for.
Chapter 10 is about classes, and I would assume that this exercise is
meant to teach about the lifetime for global objects.

--
Erik Wikström
Aug 4 '07 #3
Just as a matter of principle, wouldn't it be better like:

Jim Langston wrote:
#include <iostream>
namespace
{
class Foo
{
public:
Foo() { std::cout << "Initialize\n"; }
~Foo() { std::cout << "Cleanup\n"; }
};

Foo Bar;
}
>
int main()
{
std::cout << "Hello, world!\n" ;
return 0;
}
Aug 5 '07 #4

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

Similar topics

5
by: torhu | last post by:
In 'The C++ Programming Language', Special Edition, there is an example in chapter 11.8, page 286 that is meant to illustrate the subscript operator (operator). It is a class that associates...
42
by: ES Kim | last post by:
Here's a code fragment from TC++PL Special Edition, p291: void f1(T a) { T v; T* p = &v; p--; // please note (my comment) *p = a; // oops: 'p' out of range, uncaught ++p; *p = a; // ok
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.