这是好OOP吗? [英] Is this good OOP?

查看:71
本文介绍了这是好OOP吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用OOP的好方法吗?另外,这是计算斐波那契数字的好方法吗?


代码:


#include< iostream>

#include< cstdlib>

使用命名空间std;


模板< class T>

class invalidArg

{

public:

virtual void Write(T& arg)const {cout<< arg<< "是无效的。 " <<

endl;}

};


class fib

{

public:

unsigned long operator()(unsigned long n)

{

if(n< = 0)

抛出invalidArg< unsigned long>();

else if(n == 1 || n == 2)

返回1;

unsigned long a = 1,b = 0;

unsigned long i = 0x8000;

while(i)

{

unsigned long aa;

aa = n& i?(2 * b + a)* a:a * a + b * b;

b = n& i?a * a + b * b:b *(2 * ab);

a = aa;

i>> = 1;

}

返回b;

}

};


class mainObj

{

public:

static mainObj * Instance()

{

if(!instanceFlag)

{

main = new mainObj();

instanceFlag = true;

返回主要部分;

}

其他

返回主要部分;

}

~mainObj()

{

instanceFlag = false;

}

私人:

mainObj()

{

for(;;)

{

cout<< 输入一个数字: << endl;

unsigned long x;

cin>> x;

尝试

{

cout<< 斐波纳契数的值: << fib()(x)<<结束;

}

catch(invalidArg< unsigned long>& inv)

{

inv.Write (x);

}

catch(...)

{

cout<< 发生了错误。请联系Microsoft获取

帮助。

<<结束;

}

}

}

静态bool instanceFlag;

静态mainObj * main;

};

bool mainObj :: instanceFlag = false;

mainObj * mainObj :: main = 0;

namespace {mainObj * Main = mainObj :: Instance();}

int main(){system(" PAUSE");返回0;}


这是一个好的OOP吗?谢谢。

Is this a good way of making use of OOP? Also, is this a good way of
computing Fibonacci numbers?

Code:

#include <iostream>
#include <cstdlib>
using namespace std;

template <class T>
class invalidArg
{
public:
virtual void Write(T& arg)const{cout << arg << " is invalid. " <<
endl;}
};

class fib
{
public:
unsigned long operator()(unsigned long n)
{
if(n<=0)
throw invalidArg<unsigned long>();
else if(n==1||n==2)
return 1;
unsigned long a=1, b=0;
unsigned long i=0x8000;
while (i)
{
unsigned long aa;
aa=n&i?(2*b+a)*a:a*a+b*b;
b=n&i?a*a+b*b:b*(2*a-b);
a=aa;
i>>=1;
}
return b;
}
};

class mainObj
{
public:
static mainObj* Instance()
{
if(!instanceFlag)
{
main=new mainObj();
instanceFlag=true;
return main;
}
else
return main;
}
~mainObj()
{
instanceFlag=false;
}
private:
mainObj()
{
for(;;)
{
cout << "Enter a number: " << endl;
unsigned long x;
cin >> x;
try
{
cout << "Value of fibonacci number: " << fib()(x) << endl;
}
catch(invalidArg<unsigned long>& inv)
{
inv.Write(x);
}
catch(...)
{
cout << "An error has occurred. Please contact Microsoft for
assistance. "
<< endl;
}
}
}
static bool instanceFlag;
static mainObj *main;
};
bool mainObj::instanceFlag = false;
mainObj* mainObj::main=0;
namespace{mainObj* Main=mainObj::Instance();}
int main(){system("PAUSE"); return 0;}

Is this good OOP? Thanks.

推荐答案



" Protoman" <镨********** @ gmail.com>在消息中写道

news:11 ********************* @ g49g2000cwa.googlegro ups.com ...

"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
这是一个利用OOP的好方法吗?另外,这是计算斐波纳契数的好方法吗?

代码:

#include< iostream>
#include< cstdlib>
使用命名空间std;

模板< class T>
类invalidArg
{
公开:
virtual void Write(T& arg )const {cout<< arg<< "是无效的。 " <<
endl;}
};

class fib
{
公开:
unsigned long operator()(unsigned long n)
{
if(n< = 0)
抛出invalidArg< unsigned long>();
if if(n == 1 || n == 2)
返回1;
unsigned long a = 1,b = 0;
unsigned long i = 0x8000;
while(i)
{
unsigned long aa ;
aa = n& i?(2 * b + a)* a:a * a + b * b;
b = n& i?a * a + b * b:b *( 2 * ab);
a = aa;
i>> = 1;
}
返回b;
}
};

类mainObj
公开:
静态mainObj *实例()
{
if(!instanceFlag)
{
main = new mainObj();
instanceFlag = true;
返回main;
}

返回main;
}
〜 mainObj()
{
instanceFlag = false;
}
私人:
mainObj()
{
for(;;)
{
cout<< 输入一个数字: << endl;
unsigned long x;
cin>> x;
尝试
{
cout<< 斐波纳契数的值: << fib()(x)<< endl;
}
catch(invalidArg< unsigned long>& inv)
{
inv.Write(x);
}
catch(。 ..)
{
cout<< 发生了错误。请与Microsoft联系以获取帮助。
<< endl;
}
}
静态bool instanceFlag;
静态mainObj * main;
};
bool mainObj :: instanceFlag = false;
mainObj * mainObj :: main = 0;
namespace {mainObj * Main = mainObj :: Instance();}
int main(){system(" PAUSE");返回0;}
这是一个好的OOP吗?谢谢。
Is this a good way of making use of OOP? Also, is this a good way of
computing Fibonacci numbers?

Code:

#include <iostream>
#include <cstdlib>
using namespace std;

template <class T>
class invalidArg
{
public:
virtual void Write(T& arg)const{cout << arg << " is invalid. " <<
endl;}
};

class fib
{
public:
unsigned long operator()(unsigned long n)
{
if(n<=0)
throw invalidArg<unsigned long>();
else if(n==1||n==2)
return 1;
unsigned long a=1, b=0;
unsigned long i=0x8000;
while (i)
{
unsigned long aa;
aa=n&i?(2*b+a)*a:a*a+b*b;
b=n&i?a*a+b*b:b*(2*a-b);
a=aa;
i>>=1;
}
return b;
}
};

class mainObj
{
public:
static mainObj* Instance()
{
if(!instanceFlag)
{
main=new mainObj();
instanceFlag=true;
return main;
}
else
return main;
}
~mainObj()
{
instanceFlag=false;
}
private:
mainObj()
{
for(;;)
{
cout << "Enter a number: " << endl;
unsigned long x;
cin >> x;
try
{
cout << "Value of fibonacci number: " << fib()(x) << endl;
}
catch(invalidArg<unsigned long>& inv)
{
inv.Write(x);
}
catch(...)
{
cout << "An error has occurred. Please contact Microsoft for
assistance. "
<< endl;
}
}
}
static bool instanceFlag;
static mainObj *main;
};
bool mainObj::instanceFlag = false;
mainObj* mainObj::main=0;
namespace{mainObj* Main=mainObj::Instance();}
int main(){system("PAUSE"); return 0;}

Is this good OOP? Thanks.




不,对不起我在这个例子中根本没有看到任何OOP,它只是使用了很多

各种各样的不必要的C ++功能。作为一个简单的Fibonacci数字生成器函数,你可以完成整个事情




也许你可以考虑一个Fibonacci对象来生成Fibonacci

数字有效率

在计算新值时缓存中间数。在那个

的情况下,这将有一些

OOP,对象会有一些内部状态。


dave



No, sorry I don''t see any OOP at all in this example, its just uses a lot of
various unnecessary C++ features. You could have done the whole thing much
simpler as a simple Fibonacci number generator function.

Perhaps you could consider a Fibonacci object which would generate Fibonacci
numbers efficinetly
by caching the intermediate numbers when calculating a new value. In that
case this would have some
OOP, the object would have some internal state.

dave


你是什么意思?它使用类和东西。

What do you mean? It uses classes and stuff.




Protoman写道:

Protoman wrote:
这是一个好方法吗?利用OOP?另外,这是计算斐波纳契数的好方法吗?

代码:

#include< iostream>
#include< cstdlib>
使用命名空间std;

模板< class T>
类invalidArg
{
公开:
virtual void Write(T& arg )const {cout<< arg<< "是无效的。 " <<
endl;}
};

class fib
{
公开:
unsigned long operator()(unsigned long n)
{
if(n< = 0)
抛出invalidArg< unsigned long>();
if if(n == 1 || n == 2)
返回1;
unsigned long a = 1,b = 0;
unsigned long i = 0x8000;
while(i)
{
unsigned long aa ;
aa = n& i?(2 * b + a)* a:a * a + b * b;
b = n& i?a * a + b * b:b *( 2 * ab);
a = aa;
i>> = 1;
}
返回b;
}
};

类mainObj
公开:
静态mainObj *实例()
{
if(!instanceFlag)
{
main = new mainObj();
instanceFlag = true;
返回main;
}

返回main;
}
〜 mainObj()
{
instanceFlag = false;
}
私人:
mainObj()
{
for(;;)
{
cout<< 输入一个数字: << endl;
unsigned long x;
cin>> x;
尝试
{
cout<< 斐波纳契数的值: << fib()(x)<< endl;
}
catch(invalidArg< unsigned long>& inv)
{
inv.Write(x);
}
catch(。 ..)
{
cout<< 发生了错误。请与Microsoft联系以获取帮助。
<< endl;
}
}
静态bool instanceFlag;
静态mainObj * main;
};
bool mainObj :: instanceFlag = false;
mainObj * mainObj :: main = 0;
namespace {mainObj * Main = mainObj :: Instance();}
int main(){system(" PAUSE");返回0;}
这是一个好的OOP吗?谢谢。
Is this a good way of making use of OOP? Also, is this a good way of
computing Fibonacci numbers?

Code:

#include <iostream>
#include <cstdlib>
using namespace std;

template <class T>
class invalidArg
{
public:
virtual void Write(T& arg)const{cout << arg << " is invalid. " <<
endl;}
};

class fib
{
public:
unsigned long operator()(unsigned long n)
{
if(n<=0)
throw invalidArg<unsigned long>();
else if(n==1||n==2)
return 1;
unsigned long a=1, b=0;
unsigned long i=0x8000;
while (i)
{
unsigned long aa;
aa=n&i?(2*b+a)*a:a*a+b*b;
b=n&i?a*a+b*b:b*(2*a-b);
a=aa;
i>>=1;
}
return b;
}
};

class mainObj
{
public:
static mainObj* Instance()
{
if(!instanceFlag)
{
main=new mainObj();
instanceFlag=true;
return main;
}
else
return main;
}
~mainObj()
{
instanceFlag=false;
}
private:
mainObj()
{
for(;;)
{
cout << "Enter a number: " << endl;
unsigned long x;
cin >> x;
try
{
cout << "Value of fibonacci number: " << fib()(x) << endl;
}
catch(invalidArg<unsigned long>& inv)
{
inv.Write(x);
}
catch(...)
{
cout << "An error has occurred. Please contact Microsoft for
assistance. "
<< endl;
}
}
}
static bool instanceFlag;
static mainObj *main;
};
bool mainObj::instanceFlag = false;
mainObj* mainObj::main=0;
namespace{mainObj* Main=mainObj::Instance();}
int main(){system("PAUSE"); return 0;}

Is this good OOP? Thanks.




嗯,我觉得你对面向对象的

编程感到有点失望。并非所有东西都通过将其变成对象来改善。

例如,函数main并没有真正适合

面向对象的模型。因此MainObj(顺便说一句,让我们买一个

元音并向Vanna询问一些辅音,以便我们可以使用完整的

字作为名称:MainObject代替例如,MainObj)。 (还有一个

其他提示:键盘上有一个tab键。)


MainObj有两个问题:首先,实际上整个程序在MainObj的构造函数中执行。首先,没有人会知道在那里寻找程序。每个人都会在main()看到

。否则,没有任何公约要求

" MainObj"其他程序员将熟悉。想象一下,如果

这个程序有50个类和50个源文件,他们想通过它们看看

所有人都想知道主程序到底在哪里。


另一个问题是在main()之前执行程序意味着

其他文件中的全局对象可能尚未初始化

MainObj''调用s构造函数;结果可能会发生微妙的错误。在

短,主要和存在是有原因的 - 它是在设置完所有内容后执行的第一个函数

,以及每个C和
C ++程序员都会查找的函数。一个程序应该利用这两个事实的




Greg



Well, I think you got a bit carried away with the object-oriented
programming. Not everything is improved by turning it into an object.
For instance, the function "main" does not really lend itself to an
object-oriented model. Therefore MainObj (and, by the way, let''s buy a
vowel and ask Vanna for some consonants so that we can use complete
words as names: MainObject instead of MainObj, for example). (And one
other tip along those lines: there is a "tab" key on your keyboard.)

There are two problems with MainObj: the first is that practically the
entire program executes within MainObj''s constructor. First, no one is
going to know to look there to find the program. Everyone will look
first at main(). Otherwise, there is no convention of having a
"MainObj" with which other programmers will be familiar. Imagine if
this program had 50 classes and 50 source files, who would want to look
through them all wondering where the main routine really is.

The other problem is that executing the program before main() means
that global objects in other files may not yet be initialized when
MainObj''s constructor is called; subtle bugs can occur as a result. In
short, "main" exists for a reason - it is the first function executed
after everything has been set up, and a function with which every C and
C++ programmer will look for. A program should take advantage of both
of those facts.

Greg


这篇关于这是好OOP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆