![]() |
#2
rjsp2013-05-15 08:37
|
2、对MFC编程了解的同学应该会发现在源代码中,恐怕找不到main或者winmain这些函数,据我了解,主函数是被封装在MFC类中。
我现在想面向对象的思想写一个C++程序,而且想学上面两个例子那样,把主函数main()也封装进类里面,如:

#include <string.h>
#include <iostream>
using namespace std;
class HelloWorld{
private:
char str[50];
public:
HelloWorld(char const str0[]){
cout<<"正在执行构造函数。"<<endl;
strcpy( str, str0 );
}
~HelloWorld(){
cout<<"正在执行析构函数。"<<endl;
}
void say_hello(){
cout<<str<<endl;
}
int main(){
say_hello();
return 0;
}
};
HelloWorld Main("Hello!!");
#include <iostream>
using namespace std;
class HelloWorld{
private:
char str[50];
public:
HelloWorld(char const str0[]){
cout<<"正在执行构造函数。"<<endl;
strcpy( str, str0 );
}
~HelloWorld(){
cout<<"正在执行析构函数。"<<endl;
}
void say_hello(){
cout<<str<<endl;
}
int main(){
say_hello();
return 0;
}
};
HelloWorld Main("Hello!!");
当然了,上面这种写法实际是行不通的。我希望能够达到把主函数封装在类中。应该如何编写我的代码呢?
[ 本帖最后由 gameboy70949 于 2013-5-14 23:04 编辑 ]