这道题翻遍了互联网,无法找到答案。
题描述:
// 2. 下面是一个非常简单的类的定义:
// person.h
#ifndef PERSON_H_
#define PERSON_H_
class Person {
private:
// static const int LIMIT = 25;
enum {LIMIT = 25};
string lname; // Person's last name
char fname[LIMIT]; // Person's first name
public:
Person(void) {lname = ""; fname[0] = '\0';}
Person(const string & ln, const char * fn = "Heyyou"); // #2
~Person(void) {}
// the following methods display lname and fname
void Show(void) const; // firstname lastname format
void FormalShow(void) const; // lastname, firstname format
};
#endif//PERSON_H_
/*
* 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
*
*/
// person.cpp -- 类方法的实现
#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
Person::Person(const string & ln, const char * fn)
{
lname = ln;
strncpy(fname, fn, LIMIT - 1);
fname[LIMIT] = '\0';
return;
}
void Person::Show(void) const
{
std::cout << fname << " " << lname;
return;
}
void Person::FormalShow(void) const
{
std::cout << lname << ", " << fname;
return;
}
// main.cpp -- 主函数
#include <iostream>
#include "person.h"
int main(void)
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam"); // use 2#, no defaults
one.Show();
std::cout << std::endl;
one.FormalShow();
std::cout << std::endl;
two.Show();
std::cout << std::endl;
two.FormalShow();
std::cout << std::endl;
three.Show();
std::cout << std::endl;
three.FormalShow();
std::cout << std::endl;
return 0;
}
题描述:
// 2. 下面是一个非常简单的类的定义:
// person.h
#ifndef PERSON_H_
#define PERSON_H_
class Person {
private:
// static const int LIMIT = 25;
enum {LIMIT = 25};
string lname; // Person's last name
char fname[LIMIT]; // Person's first name
public:
Person(void) {lname = ""; fname[0] = '\0';}
Person(const string & ln, const char * fn = "Heyyou"); // #2
~Person(void) {}
// the following methods display lname and fname
void Show(void) const; // firstname lastname format
void FormalShow(void) const; // lastname, firstname format
};
#endif//PERSON_H_
/*
* 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
*
*/
// person.cpp -- 类方法的实现
#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
Person::Person(const string & ln, const char * fn)
{
lname = ln;
strncpy(fname, fn, LIMIT - 1);
fname[LIMIT] = '\0';
return;
}
void Person::Show(void) const
{
std::cout << fname << " " << lname;
return;
}
void Person::FormalShow(void) const
{
std::cout << lname << ", " << fname;
return;
}
// main.cpp -- 主函数
#include <iostream>
#include "person.h"
int main(void)
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam"); // use 2#, no defaults
one.Show();
std::cout << std::endl;
one.FormalShow();
std::cout << std::endl;
two.Show();
std::cout << std::endl;
two.FormalShow();
std::cout << std::endl;
three.Show();
std::cout << std::endl;
three.FormalShow();
std::cout << std::endl;
return 0;
}