刚学到类的修改器方法,别见笑。
我驱动后为什么答案都是1 ?可以运行,但出错了。可以直接看警告信息。
或者,应该怎样改正呢?
/*********************/
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class Name
{
public:
Name(){};
Name(string first,string middle,string last);
string getFirstName()const;
string getMiddleName()const;
string getLastName()const;
string getSignature()const;
void setFirstName(string newFirstName);
void setMiddleName(string newMiddleName);
void setLastName(string newLastName);
void print(ostream& out)const;
void read(istream& in);
private:
string myFirstName,
myLastName,
myMiddleName;
};
Name::Name(string first,string middle,string last)
{
myFirstName=first;
myLastName=last;
myMiddleName=middle;
}
inline string Name::getFirstName()const
{
return myFirstName;
}
inline string Name::getLastName()const
{
return myLastName;
}
inline string Name::getMiddleName()const
{
return myMiddleName;
}
//下面的吧?,不知道错在哪
inline void Name::setFirstName(string newFirstName)
{
myFirstName=newFirstName;
}
inline void Name::setMiddleName(string newMiddleName)
{
myMiddleName=newMiddleName;
}
inline void Name::setLastName(string newLastName)
{
myLastName=newLastName;
}
inline void Name::read(istream& in)
{
cin>>myFirstName>>myMiddleName>>myLastName;
}
inline void Name::print(ostream& out)const
{
cout<<setFirstName<<' '
<<setMiddleName<<' '
<<setLastName;//73
}
/***********************************/
#include <iostream>
#include "Name.h"
using namespace std;
void main()
{
cout <<"Enter a full name(first,middle,last): ";
Name theName;
theName.read(cin);
theName.print(cout);
cout<<"\n";
theName.setFirstName("yertle");
theName.setLastName("Turtle");
theName.print(cout);
cout<<"\n";
}
/*************************/
警告信息:
d:\学习软件\c++1\msdev98\myprojects\书本253页\name.h(73) : warning C4761: integral size mismatch in argument; conversion supplied
d:\学习软件\c++1\msdev98\myprojects\书本253页\name.h(73) : warning C4761: integral size mismatch in argument; conversion supplied
d:\学习软件\c++1\msdev98\myprojects\书本253页\name.h(73) : warning C4761: integral size mismatch in argument; conversion supplied
Linking...