More than one instance.

M

Michael Bell

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

--
 
R

ralpe

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();
}
 
T

tragomaskhalos

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

Guest

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

osmium

Michael Bell said:
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.
 
M

Michael Bell

In message <[email protected]>
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



--
 
M

Michael Bell

In message <[email protected]>
"osmium said:
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




--
 
M

Michael Bell

In message <[email protected]>
tragomaskhalos said:
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



--
 
M

Michael Bell

In message <[email protected]>
ralpe said:
class message
{
public:
message(const std::string& contents)
: contents_(contents) {}
void display() {
std::cout << contents_ << std::endl;
}

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



--
 
A

Andre Kostur

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

Markus Moll

Hi

Michael said:
In message <[email protected]>
Erik Wikström said:
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
 
T

tragomaskhalos

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

Michael Bell

In message <[email protected]>
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


--
 
O

osmium

Michael Bell said:
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.
 
M

Michael Bell

In message <[email protected]>
"osmium said:
"Michael Bell" wrote:
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




--
 
T

tragomaskhalos

In message <[email protected]>
[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.
 
M

Michael Bell

In message <[email protected]>
tragomaskhalos said:
In message <[email protected]>
[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.

Oh dear! I've gone and ordered from Amazon, and this

http://www.accu.org/index.php/book_reviews?view.xqy?review=cp003131&term=prata&
country=UK

is a pretty poor review. Still, it is only Friday evening and I
suppose I could change it before the staff come in on saturday
morning. If they do work Saturdays, that is. Is there anything better?

Michael Bell




--
 
G

Guest

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

Michael Bell

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top