<<C++语言程序设计>>中的第一题,我编了以下程序,可老出错,想请各位大侠帮帮忙!先谢谢了!
(1)设计要求:假设类名为Welcome,给出主程序如下:voidmain(){welcomewe;welcomeyou(we);You.display();Dyou.s...
(1)设计要求:
假设类名为Welcome,给出主程序如下:
void main()
{welcome we;
welcome you(we);
You.display();D
you.set("Thank you.");
cout<<you.Get()<<endl;
you.talk();
you.display();
}
下面是给出类的定义:
class Welcome{
private:
char str[128];
public:
Welcome(char s[]="welcome!");
Welcome(Welcome &);
void set(char []);
void display();
char *Get();
void talk();
~Welcome();
};
请编写合适的成员函数,使程序的运行结果为:
Welcome!
Thank you.
当输入:How about you?
就输出:How about you?
输入:OK
All right!
Goodbye!
OK
Goodbye!
Goodbye!
我自己编了一个程序,可是老出现下面这个错误,郁闷死我了!
Compiling...
h.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/44.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
我写的程序如下:
#include<iostream>
using namespace std;
class Welcome{
private:
char str[128];
public:
Welcome(char s[]="Welcome!");
Welcome (Welcome &);
void set(char[]);
void display();
char *Get();
void talk();
~Welcome();
};
class Welcome::Welcome(char s[]="Welcome!")
{cout<<"constructor Welcome"<<endl;
}
class Welcome::Welcome(Welcome &)
{cout<<"copy Welcome"<<endl;
}
void Welcome::set(char[])
{char[]="Thank you";
cout<<char[]<<endl;
}
void class Welcome::display()
{display()
{Welcome::You display();
Welcome::you display();
cout<<"Goodbye!"<<endl;
}
}
class Welcome::char*Get()
{*Get={"当输入:","How about you?","就输出:","How about you?","输入:","OK";
}
for(i=0;i<6;i++;)
cout<<Get(i)<<endl;
}
class Welcome::talk()
{cout<<"All right"<<endl;
cout<<"Goodbye!"<<endl;
cout<<"OK"<<endl;
cout<<"Goodbye!"<<endl;
}
class Welcome::~Welcome
{cout<<"destructor Welcome"<<endl;
}
void main()
{
welcome we;
welcome you(we);
You.display();
you.set("Thank you.");
cout<<you.Get()<<endl;
you.talk();
you.display();
};
望各位大侠多多指点,我已经想了很长时间,可是就是不知道错在哪!唉……,因为俺是个C++的初学者,所以不太会编写,有的地方可能错误多多,望各位大侠多多帮忙修改修改,谢谢了! 展开
假设类名为Welcome,给出主程序如下:
void main()
{welcome we;
welcome you(we);
You.display();D
you.set("Thank you.");
cout<<you.Get()<<endl;
you.talk();
you.display();
}
下面是给出类的定义:
class Welcome{
private:
char str[128];
public:
Welcome(char s[]="welcome!");
Welcome(Welcome &);
void set(char []);
void display();
char *Get();
void talk();
~Welcome();
};
请编写合适的成员函数,使程序的运行结果为:
Welcome!
Thank you.
当输入:How about you?
就输出:How about you?
输入:OK
All right!
Goodbye!
OK
Goodbye!
Goodbye!
我自己编了一个程序,可是老出现下面这个错误,郁闷死我了!
Compiling...
h.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/44.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
我写的程序如下:
#include<iostream>
using namespace std;
class Welcome{
private:
char str[128];
public:
Welcome(char s[]="Welcome!");
Welcome (Welcome &);
void set(char[]);
void display();
char *Get();
void talk();
~Welcome();
};
class Welcome::Welcome(char s[]="Welcome!")
{cout<<"constructor Welcome"<<endl;
}
class Welcome::Welcome(Welcome &)
{cout<<"copy Welcome"<<endl;
}
void Welcome::set(char[])
{char[]="Thank you";
cout<<char[]<<endl;
}
void class Welcome::display()
{display()
{Welcome::You display();
Welcome::you display();
cout<<"Goodbye!"<<endl;
}
}
class Welcome::char*Get()
{*Get={"当输入:","How about you?","就输出:","How about you?","输入:","OK";
}
for(i=0;i<6;i++;)
cout<<Get(i)<<endl;
}
class Welcome::talk()
{cout<<"All right"<<endl;
cout<<"Goodbye!"<<endl;
cout<<"OK"<<endl;
cout<<"Goodbye!"<<endl;
}
class Welcome::~Welcome
{cout<<"destructor Welcome"<<endl;
}
void main()
{
welcome we;
welcome you(we);
You.display();
you.set("Thank you.");
cout<<you.Get()<<endl;
you.talk();
you.display();
};
望各位大侠多多指点,我已经想了很长时间,可是就是不知道错在哪!唉……,因为俺是个C++的初学者,所以不太会编写,有的地方可能错误多多,望各位大侠多多帮忙修改修改,谢谢了! 展开
展开全部
我修改了下,下面的代码在Windows XP + VC++6.0下正常运行,直接复制粘贴就行了
有2点是你需要注意的
(1)题目的意思是:
"当输入:"后面的那句"How about you?"和"输入:"后的那句"OK"是由你从键盘中输入的,其它的都由程序自动输出
(2)你存在很多语法错误
比如C++中是分大小写的,可你的代码中大小写不分;
类成员函数的定义格式也不对
建议你先看一下C++基础教材,先掌握语法,模仿教材上的设计
#include<iostream>
#include<cstdio>//函数strcpy(),stcmy()包含在此头文件中
using namespace std;
class Welcome
{
private:
char str[128];
public:
Welcome(char s[]="Welcome!");
Welcome (Welcome &);
void set(char []);
void display();
char *Get();
void talk();
~Welcome();
};
Welcome::Welcome(char s[])//默认参数值在函数声明中已写了,此处若改为char s[]="Welcom!"错
{
strcpy(str,s);//把字符串数组s中的所有字符,拷贝到字符串数组str中
}
Welcome::Welcome(Welcome &c)//拷贝构造函数
{
strcpy(str,c.str);
}
void Welcome::set(char s[])
{
strcpy(str,s);
}
void Welcome::display()
{
cout<<str<<endl;
}
char* Welcome::Get()
{
return str;
}
Welcome::~Welcome()
{
cout<<"Goodbye!"<<endl;
}
void Welcome::talk()
{
char s[128];
cout<<"当输入:";
cin.getline(s,128);//不用cin>>s;因为输入为几个字符串
while(strcmp(s,"OK")!=0)//s不等于"OK"时,循环输出及输出
{
cout<<"就输出:"<<s<<endl;
cout<<"输入:";
cin.getline(s,128);
}
strcpy(str,"OK");//或strcpy(str,s);因为执行到这个指令时,必有s="OK"
cout<<"All right!"<<endl;
cout<<"Goodbye!"<<endl;
}
void main()
{
Welcome we;
Welcome you(we);
you.display();
you.set("Thank you.");
cout<<you.Get()<<endl;
you.talk();
you.display();
}
/*运行结果
Welcome!
Thank you.
当输入:How about you?
就输出:How about you?
输入:OK
All right!
Goodbye!
OK
Goodbye!
Goodbye!
Press any key to continue
*/
有2点是你需要注意的
(1)题目的意思是:
"当输入:"后面的那句"How about you?"和"输入:"后的那句"OK"是由你从键盘中输入的,其它的都由程序自动输出
(2)你存在很多语法错误
比如C++中是分大小写的,可你的代码中大小写不分;
类成员函数的定义格式也不对
建议你先看一下C++基础教材,先掌握语法,模仿教材上的设计
#include<iostream>
#include<cstdio>//函数strcpy(),stcmy()包含在此头文件中
using namespace std;
class Welcome
{
private:
char str[128];
public:
Welcome(char s[]="Welcome!");
Welcome (Welcome &);
void set(char []);
void display();
char *Get();
void talk();
~Welcome();
};
Welcome::Welcome(char s[])//默认参数值在函数声明中已写了,此处若改为char s[]="Welcom!"错
{
strcpy(str,s);//把字符串数组s中的所有字符,拷贝到字符串数组str中
}
Welcome::Welcome(Welcome &c)//拷贝构造函数
{
strcpy(str,c.str);
}
void Welcome::set(char s[])
{
strcpy(str,s);
}
void Welcome::display()
{
cout<<str<<endl;
}
char* Welcome::Get()
{
return str;
}
Welcome::~Welcome()
{
cout<<"Goodbye!"<<endl;
}
void Welcome::talk()
{
char s[128];
cout<<"当输入:";
cin.getline(s,128);//不用cin>>s;因为输入为几个字符串
while(strcmp(s,"OK")!=0)//s不等于"OK"时,循环输出及输出
{
cout<<"就输出:"<<s<<endl;
cout<<"输入:";
cin.getline(s,128);
}
strcpy(str,"OK");//或strcpy(str,s);因为执行到这个指令时,必有s="OK"
cout<<"All right!"<<endl;
cout<<"Goodbye!"<<endl;
}
void main()
{
Welcome we;
Welcome you(we);
you.display();
you.set("Thank you.");
cout<<you.Get()<<endl;
you.talk();
you.display();
}
/*运行结果
Welcome!
Thank you.
当输入:How about you?
就输出:How about you?
输入:OK
All right!
Goodbye!
OK
Goodbye!
Goodbye!
Press any key to continue
*/
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询