不止一个例子。 [英] More than one instance.
问题描述
这是Pardoe& amp;的第一个节目。王。它假设
我无法解开:只有一个类的实例
消息。如何创建两条消息,例如欢迎和欢迎。和再见?
虽然我可以看出为什么会这样,但我认为
第一个例子是一个特例是不明智的。
Michael Bell
/ ---------------------- -------------------------------------------------- ---
#include< vcl.h>
#pragma hdrstop
// #include< string。 h //在Borland没必要
// #include" MT262io.h"
#include< iostream //启用cout
using namespace std; //启用cout
类消息
{
public:
void initialise();
无效显示();
受保护:
char内容[12];
};
>
无效消息:: initialise()
{
strcpy(内容,Hello world);
}
无效消息:: display()
{
cout<<内容<<结束;
}
#pragma argsused
int main(int argc,char * argv [])
{
消息你好;
hello.initialise();
hello.display();
getchar(); //来自MT262的屏幕保持器
返回0;
}
// ----------- -------------------------------------
- -
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;
}
//------------------------------------------------
--
推荐答案
课程信息
{
public:
>
消息(const std :: string& contents)
:contents_(内容){}
void display(){
std :: cout<< contents_<< std :: endl;
}
私人:
std :: string contents_;
};
int main(){
留言欢迎(欢迎!);
消息再见(Goodbye!);
welcome.display();
goodbye.display();
}
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();
}
10月12日下午12:32,Michael Bell< mich ... @ beaverbell.co.ukwrote:
On Oct 12, 12:32 pm, Michael Bell <mich...@beaverbell.co.ukwrote:
这是Pardoe& amp;王。它假设
我无法解开:只有一个类的实例
消息。如何创建两条消息,例如欢迎和欢迎。和再见?
虽然我可以看出为什么会这样,但我认为
第一个例子是一个特例是不明智的。
Michael Bell
/ ---------------------- -------------------------------------------------- - * -
#include< vcl.h>
#pragma hdrstop
// #include< string .h //在Borland没必要
// #include" MT262io.h"
#include< iostream //启用cout
使用namespace std; //启用cout
类消息
{
public:
void initialise();
无效显示();
受保护:
char内容[12];
};
>
无效消息:: initialise()
{
strcpy(内容,Hello world);
}
无效消息:: display()
{
cout<<内容<<结束;
}
#pragma argsused
int main(int argc,char * argv [])
{
消息你好;
hello.initialise();
hello.display();
getchar(); //来自MT262的屏幕保持者
返回0;}
// --------------- ---------------------------------
-
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;}
//------------------------------------------------
--
哇,这是一段非常糟糕的代码:
1)它使用char []而不是std :: string;糟糕!
假设您更改了消息以添加惊叹号
标记 - BANG - 您已超出缓冲区。
2)初始化方法是一个坏主意 - 这就是
构造函数的用途。
3)这很有趣一种硬编码它的文本的消息,但也许他们已经这样做了
a更简单的例子。我会对它进行选择。
解决这些问题并做其他一些
位产生这样的结果:
#include< string>
#include< iostream>
班级消息
{
public:
message(const char *);
void display();
private:
std :: string contents;
};
message :: message(const char * m):contents(m){}
无效消息:: display()
{
std :: cout<<内容<< std :: endl;
}
int main(int argc,char * argv [])
{
消息hello(Hello World);
hello.display();
消息告别(Goodbye cruel world; );
goodbye.display();
getchar(); //来自MT262的屏幕保持者
返回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;
}
2007年-10-12 13:32,Michael Bell写道:
On 2007-10-12 13:32, Michael Bell wrote:
这是Pardoe& amp;王。
Here is the first program from Pardoe & King.
Pardoe和King使用C ++进行面向对象编程?如果
就是这种情况,我很遗憾地说,你能用这本书做的最好的书可能会把你的公寓点燃它来加热你的公寓。它预先确定C ++标准超过一年(这意味着它比现在的10年更多)。这意味着书中所写的内容
甚至可能都不是合法的C ++(而且从目前为止我看到的是
肯定不是很好的C ++)。 />
-
Erik Wikstr ?? m
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
这篇关于不止一个例子。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!