高手来看下
#include"name.h"#include<string>
using std::string;
#include<iostream>
using std::ostream;
using std::istream;
#include<iomanip>
using std::setw;
using std::left;
#include<cstring>
using std::strlen;
using std::strcpy;
int Name::number = 0;
Name::Name(const char * const f , const char * const l)
{
setFirstName(f);
setLastName(l);
Name::number ++;
}
Name::~Name()
{
delete [] firstname;
delete [] lastname;
number--;
}
void Name::setFirstName( const char * const first)
{
firstname = new char[ strlen( first ) + 1 ];
strcpy( firstname , first );
}
void Name::setLastName( const char * const last)
{
lastname = new char[ strlen( last ) + 1 ];
strcpy( lastname , last );
}
char *Name::getFirstName()
{
return firstname;
}
char *Name::getLastName()
{
return lastname;
}
ostream &operator<<( ostream &output , const Name &m )
{
output <<"Welcome to GradeBood System" <<'\n'<<"Number is: "<<setw(2)<<left<< m.number << '\t'<<"Name is: " << m.firstname << " " << m.lastname;
return output;
}
istream &operator>>( istream &input , Name &n )
{
input >> setw(25) >> n.getFirstName() >> n.getLastName();
return input;
}
_____________________________________________________________________________________________________________________________________________
//name.h
//name class definition
#ifndef NAME_H
#define NAME_H
#include<iostream>
using std::ostream;
using std::istream;
#include<string>
using std::string;
class Name
{
friend ostream &operator<<( ostream & , const Name & );
friend istream &operator>>( istream & , Name & );
public:
Name(const char * const = "",const char * const = "");
~Name();
void setFirstName(const char * const);
char *getFirstName();
void setLastName( const char * const);
char *getLastName();
int getNumber(){return number;}
private:
char *firstname;
char *lastname;
static int number;
};
#endif
_____________________________________________________________________________________________________________________________________________
#include "name.h"
#include <iostream>
using std::cout;
using std::cin;
#include <string>
using std::string;
int main()
{
char *first = "";
char *last = "";
cout<< "Please enter the frist name or enter \"end\" to exit"<<'\n';
cin >> first;
cout << "Please enter the last name or enter \"end\" to exit"<<'\n';
cin >> last;
Name student(first , last);
cout << student;
cout<<'\n'<<"please inter the first name and the last of a new student:"<<'\n';
cin >> student;
cout << student;
return 0;
}
1>------ 已启动生成: 项目: GradeBook, 配置: Debug Win32 ------
1>正在编译...
1>name.cpp
1>.\name.cpp(40) : warning C4996: “strcpy”被声明为否决的
1> E:\公用软件\学习软件\Microsoft Visual Studio 8\VC\include\string.h(73) : 参见“strcpy”的声明
1> 消息:“This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”
1>.\name.cpp(46) : warning C4996: “strcpy”被声明为否决的
1> E:\公用软件\学习软件\Microsoft Visual Studio 8\VC\include\string.h(73) : 参见“strcpy”的声明
1> 消息:“This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”
1>正在链接...
1>正在嵌入清单...
1>生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\GradeBook\GradeBook\Debug\BuildLog.htm”
1>GradeBook - 0 个错误,2 个警告
运行的时候,一输入就出错。 。高手帮帮忙。。怎么回事,该怎么改,高手帮帮忙,感激不尽!!!!!