2.下面是一个非常简单的类定义:
class Person
{
private:
static const LIMIT=25;
string lname; //Person’s last name
char fname[LIMIT]; //Person’s first namepublic :
Person()(lname=“”;fname[0]=0’;}//#1
Person(const string&ln,constchar*fn=“Heyyou”);
//#2the following methods display lname and fname
void Show()consti //firstname lastname formatvoid Formal
Show()const; //lastname, firstname format
};
它使用了一个 string 对象和一个字符数组,让您能够比较它们的用法。请提供未定义的方法的代码以完成这个类的实现。再编写一个使用这个类的程序,它使用了三种可能的构造函数调用(没有参数、个参数和两个参数)以及两种显示方法。下面是一个使用这些构造函数和方法的例子:
Person one ; //use default constructor
Person two(“Smythecraft”);//use #2 with one default argument
Person three(“Dimwiddy”,“Sam”);//use #2,no defaults
one.Show();
cout <<endl;
one.FormalShow();
//etc.for two and three
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Person
{
public:
Person() { lname = "zhangyongjiang";fname[0] = '\0'; };
Person(const string& ln, const char* fn = "Heyyou");
~Person();
void show()const;
void formalshow()const;
private:
static const unsigned LIMIT = 25;
string lname;
char fname[LIMIT];
};
Person::Person(const string& ln, const char* fn)
{
lname = ln;
unsigned cntstr= strlen(fn) + 1;
strcpy_s(fname, cntstr, fn);
}
Person::~Person()
{
}
void Person::show() const
{
cout << lname;
}
void Person::formalshow() const
{
cout << fname;
}
int main()
{
Person one;
Person two("Smythecratft");
Person three("Dimwiddy", "Sam");
one.show();
cout << endl;
three.show();
cout << endl;
three.formalshow();
one.formalshow();
return 0;
}
标签:const,string,练习,C++,lname,Person,Plus,fname,cout
From: https://blog.csdn.net/zhyjhacker/article/details/139236413