473,396 Members | 1,966 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,396 software developers and data experts.

More than one instance.

Here is the first program from Pardoe & King. It makes an assumption
that I cannot untangle: There is only one instance of the class
message. How do I create two messages, say "Welcome" and "Goodbye"?

Although I can see why it is like this, I feel it is unwise for a
first example to be a special case.

Michael Bell

/---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
// #include <string.h // Not necessary in Borland
// #include "MT262io.h"
#include <iostream // Enables cout
using namespace std; // Enables cout
class message
{
public :
void initialise ();
void display();
protected :
char contents[12];
};

void message::initialise()
{
strcpy (contents, "Hello world");
}
void message::display ()
{
cout << contents << endl;
}

#pragma argsused
int main(int argc, char* argv[])
{
message hello;
hello.initialise();
hello.display();

getchar(); // screenholder from MT262

return 0;
}
//------------------------------------------------

--
Oct 12 '07 #1
17 1324
class message
{
public:

message(const std::string& contents)
: contents_(contents) {}

void display() {
std::cout << contents_ << std::endl;
}

private:

std::string contents_;
};

int main() {
message welcome("Welcome!");
message goodbye("Goodbye!");
welcome.display();
goodbye.display();
}

Oct 12 '07 #2
On Oct 12, 12:32 pm, Michael Bell <mich...@beaverbell.co.ukwrote:
Here is the first program from Pardoe & King. It makes an assumption
that I cannot untangle: There is only one instance of the class
message. How do I create two messages, say "Welcome" and "Goodbye"?

Although I can see why it is like this, I feel it is unwise for a
first example to be a special case.

Michael Bell

/--------------------------------------------------------------------------*-

#include <vcl.h>
#pragma hdrstop
// #include <string.h // Not necessary in Borland
// #include "MT262io.h"
#include <iostream // Enables cout
using namespace std; // Enables cout
class message
{
public :
void initialise ();
void display();
protected :
char contents[12];
};

void message::initialise()
{
strcpy (contents, "Hello world");
}
void message::display ()
{
cout << contents << endl;
}

#pragma argsused
int main(int argc, char* argv[])
{
message hello;
hello.initialise();
hello.display();

getchar(); // screenholder from MT262

return 0;}

//------------------------------------------------

--
Wow, this is a pretty horrible bit of code:

1) It uses char[] instead of std::string; bad !
Say you changed the message to add an exclamation
mark at the end - BANG - you've overrun your buffer.

2) initialise methods are A Bad Idea - that's what
the constructor is for.

3) It's a funny sort of message that hardcodes its
text, but maybe they've done it that way to make
a simpler example. I'd paramaterise it though.

Fixing these problems and doing a few other
bits yields this:

#include <string>
#include <iostream>

class message
{
public :
message(const char*);
void display();
private:
std::string contents;
};

message::message(const char* m) : contents(m) {}

void message::display ()
{
std::cout << contents << std::endl;
}

int main(int argc, char* argv[])
{
message hello ("Hello World");
hello.display();

message goodbye("Goodbye cruel world");
goodbye.display();

getchar(); // screenholder from MT262

return 0;
}

Oct 12 '07 #3
On 2007-10-12 13:32, Michael Bell wrote:
Here is the first program from Pardoe & King.
Is that Object Oriented Programming Using C++, by Pardoe and King? If
that is the case I am sorry to say that the best you could do with that
book is probably to heat your apartment by setting it on fire. It pre-
dates the C++ standard with over one year (that means that it is more
than 10 years by now). This means that whatever is written in the book
might not even be legal C++ (and from what I have seen so far it is
certainly not good C++).

--
Erik Wikström
Oct 12 '07 #4
"Michael Bell" writes:
Here is the first program from Pardoe & King. It makes an assumption
that I cannot untangle: There is only one instance of the class
message. How do I create two messages, say "Welcome" and "Goodbye"?

Although I can see why it is like this, I feel it is unwise for a
first example to be a special case.

Michael Bell

/---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
// #include <string.h // Not necessary in Borland
// #include "MT262io.h"
#include <iostream // Enables cout
using namespace std; // Enables cout
class message
{
public :
void initialise ();
void display();
protected :
char contents[12];
};

void message::initialise()
{
strcpy (contents, "Hello world");
}
void message::display ()
{
cout << contents << endl;
}

#pragma argsused
int main(int argc, char* argv[])
{
message hello;
hello.initialise();
hello.display();

getchar(); // screenholder from MT262

return 0;
}
//------------------------------------------------
In my opinion, *most* books on C++ are pretty bad, I have shelves full of
them, and I think you have one too. The program is misleading, the name of
the class is "message", but it doesn't display a general message, it
displays a particular message, "Hello world". A more reasonable name for
the class would have been display_hello_world. Even then it is poorly done,
because it doesn't use a constructor where it should have been used.

I am not a big fan of the book usually recommended, _Accelerated C++_,
either. Look at recent books on C++ by Stephen Prata, and see if he
connects with your way of thinking.
Oct 12 '07 #5
In message <3z*****************@newsb.telia.net>
Erik Wikström <Er***********@telia.comwrote:
On 2007-10-12 13:32, Michael Bell wrote:
>Here is the first program from Pardoe & King.
Is that Object Oriented Programming Using C++, by Pardoe and King? If
that is the case I am sorry to say that the best you could do with that
book is probably to heat your apartment by setting it on fire. It pre-
dates the C++ standard with over one year (that means that it is more
than 10 years by now). This means that whatever is written in the book
might not even be legal C++ (and from what I have seen so far it is
certainly not good C++).
Yes, that's it. It was published in 1977. Is that unforgivably old? It
was given to me only this year, in kindness, by a professor of
computer science. Maybe he just didn't realise how time had passed.
We've all done it!

Whatever its other faults, (which may be many) it does what so many
other texts do, and gives a teaching example which is actually a
special case. In this case a class is declared, but there is only one
instance of that class, so you can use the class name for the only
instance without ambiguity. But how do you handle it if there is more
than one instance?

Thank you for your answer.

Michael Bell

--
Oct 12 '07 #6
In message <11**********************@k35g2000prh.googlegroups .com>
ralpe <ra************@gmx.netwrote:
class message
{
public:
message(const std::string& contents)
: contents_(contents) {}
void display() {
std::cout << contents_ << std::endl;
}
private:
std::string contents_;
};
int main() {
message welcome("Welcome!");
message goodbye("Goodbye!");
welcome.display();
goodbye.display();
}
I'll try this. But there has to be some method of choosing whether it
is welcome or goodbye.

Michael Bell

--
Oct 12 '07 #7
In message <5n************@mid.individual.net>
"osmium" <r1********@comcast.netwrote:
"Michael Bell" writes:
>Here is the first program from Pardoe & King. It makes an assumption
that I cannot untangle: There is only one instance of the class
message. How do I create two messages, say "Welcome" and "Goodbye"?

Although I can see why it is like this, I feel it is unwise for a
first example to be a special case.

Michael Bell

/---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
// #include <string.h // Not necessary in Borland
// #include "MT262io.h"
#include <iostream // Enables cout
using namespace std; // Enables cout
class message
{
public :
void initialise ();
void display();
protected :
char contents[12];
};

void message::initialise()
{
strcpy (contents, "Hello world");
}
void message::display ()
{
cout << contents << endl;
}

#pragma argsused
int main(int argc, char* argv[])
{
message hello;
hello.initialise();
hello.display();

getchar(); // screenholder from MT262

return 0;
}
//------------------------------------------------
In my opinion, *most* books on C++ are pretty bad, I have shelves full of
them, and I think you have one too. The program is misleading, the name of
the class is "message", but it doesn't display a general message, it
displays a particular message, "Hello world". A more reasonable name for
the class would have been display_hello_world. Even then it is poorly done,
because it doesn't use a constructor where it should have been used.
I am not a big fan of the book usually recommended, _Accelerated C++_,
either. Look at recent books on C++ by Stephen Prata, and see if he
connects with your way of thinking.
Oh dear, what a nest of snakes I have fallen into!

I'll look out for his books. Thank you for your advice.

Michael Bell


--
Oct 12 '07 #8
In message <11*********************@e34g2000pro.googlegroups. com>
tragomaskhalos <da*************@logicacmg.comwrote:
On Oct 12, 12:32 pm, Michael Bell <mich...@beaverbell.co.ukwrote:
>Here is the first program from Pardoe & King. It makes an assumption
that I cannot untangle: There is only one instance of the class
message. How do I create two messages, say "Welcome" and "Goodbye"?

Although I can see why it is like this, I feel it is unwise for a
first example to be a special case.

Michael Bell

/---------------------------------------------------------------------
-----*-

#include <vcl.h>
#pragma hdrstop
// #include <string.h // Not necessary in Borland
// #include "MT262io.h"
#include <iostream // Enables cout
using namespace std; // Enables cout
class message
{
public :
void initialise ();
void display();
protected :
char contents[12];
};

void message::initialise()
{
strcpy (contents, "Hello world");
}
void message::display ()
{
cout << contents << endl;
}

#pragma argsused
int main(int argc, char* argv[])
{
message hello;
hello.initialise();
hello.display();

getchar(); // screenholder from MT262

return 0;}

//------------------------------------------------

--
Wow, this is a pretty horrible bit of code:
1) It uses char[] instead of std::string; bad !
Say you changed the message to add an exclamation
mark at the end - BANG - you've overrun your buffer.
2) initialise methods are A Bad Idea - that's what
the constructor is for.
3) It's a funny sort of message that hardcodes its
text, but maybe they've done it that way to make
a simpler example. I'd paramaterise it though.
Fixing these problems and doing a few other
bits yields this:
#include <string>
#include <iostream>
class message
{
public :
message(const char*);
void display();
private:
std::string contents;
};
message::message(const char* m) : contents(m) {}
void message::display ()
{
std::cout << contents << std::endl;
}
int main(int argc, char* argv[])
{
message hello ("Hello World");
hello.display();
message goodbye("Goodbye cruel world");
goodbye.display();
getchar(); // screenholder from MT262
return 0;
}
Thank you. I'll try this.

Michael Bell

--
Oct 12 '07 #9
tragomaskhalos <da*************@logicacmg.comwrote in
news:11*********************@e34g2000pro.googlegro ups.com:

2) initialise methods are A Bad Idea - that's what
the constructor is for.
Not necessarily... you may have multiple constructors which share a bunch
of code. For code reuse purposes you may have those constructors all call
a common initialize method.

Oct 12 '07 #10
Hi

Michael Bell wrote:
In message <3z*****************@newsb.telia.net>
Erik Wikström <Er***********@telia.comwrote:
>Is that Object Oriented Programming Using C++, by Pardoe and King?
[...]
Yes, that's it. It was published in 1977. Is that unforgivably old?
Phew... that shocked me. Luckily, it was published in 19*9*7

Still, yes, that's unforgivably old, given that C++ was only standardised in
1998.

Markus

Oct 12 '07 #11
On 12 Oct, 16:32, Andre Kostur <nntps...@kostur.netwrote:
tragomaskhalos <dave.du.verg...@logicacmg.comwrote innews:11*********************@e34g2000pro.googleg roups.com:
2) initialise methods are A Bad Idea - that's what
the constructor is for.

Not necessarily... you may have multiple constructors which share a bunch
of code. For code reuse purposes you may have those constructors all call
a common initialize method.
Well yes good point; I meant *public* initialize methods.
Oct 12 '07 #12
In message <5n************@mid.individual.net>
"osmium" <r1********@comcast.netwrote:
[snip]
In my opinion, *most* books on C++ are pretty bad, I have shelves full of
them, and I think you have one too. The program is misleading, the name of
the class is "message", but it doesn't display a general message, it
displays a particular message, "Hello world". A more reasonable name for
the class would have been display_hello_world. Even then it is poorly done,
because it doesn't use a constructor where it should have been used.
I am not a big fan of the book usually recommended, _Accelerated C++_,
either. Look at recent books on C++ by Stephen Prata, and see if he
connects with your way of thinking.
Amazon has C++ (Primer Plus(Sams)) by Stephen Prata - Nov 2004.

Stephen Prata seems to have written books on C as far back as 1994,
but nothing else on C++. Is this the book you recommend?

Michael Bell
--
Oct 12 '07 #13
"Michael Bell" wrote:
>I am not a big fan of the book usually recommended, _Accelerated C++_,
either. Look at recent books on C++ by Stephen Prata, and see if he
connects with your way of thinking.

Amazon has C++ (Primer Plus(Sams)) by Stephen Prata - Nov 2004.

Stephen Prata seems to have written books on C as far back as 1994,
but nothing else on C++. Is this the book you recommend?
Yes, C++ Primer Plus would be fine. I have an old edition so I didn't want
to identify it.
Oct 12 '07 #14
In message <5n************@mid.individual.net>
"osmium" <r1********@comcast.netwrote:
"Michael Bell" wrote:
>>I am not a big fan of the book usually recommended, _Accelerated C++_,
either. Look at recent books on C++ by Stephen Prata, and see if he
connects with your way of thinking.

Amazon has C++ (Primer Plus(Sams)) by Stephen Prata - Nov 2004.

Stephen Prata seems to have written books on C as far back as 1994,
but nothing else on C++. Is this the book you recommend?
Yes, C++ Primer Plus would be fine. I have an old edition so I didn't want
to identify it.
OK. I'll go and buy it.

All this discussion of dates has led me to look at the Open University
textbooks I have been studying, they say "First edition 1999, second
edition 2002". Is even that OLD?

I also have "C++ in a nutshell" by Ray Lischner, Publisher O'Reilly,
first edition 2003, which is a reference book, way beyond my needs,
but I suppose I'll grow into it.

Thank you for your help.

Michael Bell


--
Oct 12 '07 #15
On 12 Oct, 20:08, Michael Bell <mich...@beaverbell.co.ukwrote:
In message <5n99p7Fh3un...@mid.individual.net>
"osmium" <r124c4u...@comcast.netwrote:
[snip]

Amazon has C++ (Primer Plus(Sams)) by Stephen Prata - Nov 2004.

Stephen Prata seems to have written books on C as far back as 1994,
but nothing else on C++. Is this the book you recommend?
But check out the book reviews on www.accu.org before you
part with any cash.

Oct 12 '07 #16
On 2007-10-12 17:32, Andre Kostur wrote:
tragomaskhalos <da*************@logicacmg.comwrote in
news:11*********************@e34g2000pro.googlegro ups.com:

>2) initialise methods are A Bad Idea - that's what
the constructor is for.

Not necessarily... you may have multiple constructors which share a bunch
of code. For code reuse purposes you may have those constructors all call
a common initialize method.

It is bad enough of an idea for them to allow one constructor to call
another in the next version of the standard. The problem with
initialisation functions are that they can be called after the object is
created, making them protected or private mitigates this problem
somewhat but it is still not an elegant solution.

--
Erik Wikström
Oct 12 '07 #17
In message <11**********************@i38g2000prf.googlegroups .com>
tragomaskhalos <da*************@logicacmg.comwrote:
On 12 Oct, 20:08, Michael Bell <mich...@beaverbell.co.ukwrote:
>In message <5n99p7Fh3un...@mid.individual.net>
"osmium" <r124c4u...@comcast.netwrote:
[snip]

Amazon has C++ (Primer Plus(Sams)) by Stephen Prata - Nov 2004.

Stephen Prata seems to have written books on C as far back as 1994,
but nothing else on C++. Is this the book you recommend?
But check out the book reviews on www.accu.org before you
part with any cash.
This is a REALLY GOOD SITE.

Michael Bell

--
Oct 12 '07 #18

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
2
by: chris.millar | last post by:
The class below uses the switch clause in the GetXSLT method. I want it to code it with a more OO approach does anyone have any suggestions, or change to make. cheers using System; using...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
4
by: James L | last post by:
Any ideas how to stop the user opening an app more than once? So there is not more than one instance running at the same time. Thanks.
8
by: BK | last post by:
Converting a rather large solution from 2003 to 2005. All the errors are worked out, but I'm trying to clean up the warnings as best I can. The good news is that the application and it's...
0
by: aspineux | last post by:
setacl and getacl look to be already "Cyrus" specific (according the doc), why not to extend imaplib a little bit more ? Here are some code I wrote and tested to support cyrus "expire" that...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
23
by: Tony Johansson | last post by:
Hello! I just wonder what is the point of having the reader variable declared as TextReader in the snippet below.. Is it because of using the polymorfism on the reader variable perhaps. using...
3
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
you can write writer.WriteStartElement("data", "http://www.w3.org/1999/XMLSchema-instance"); in your code for, <data xmlns="http://www.w3.org/1999/XMLSchema-instance"> how do you write, ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.