您的位置:首页 > 编程语言

第十章编程练习(2)

2016-02-01 23:34 316 查看
ff.h
#pragma once
#ifndef ff_H_
#define ff_H_
#include <string>
using namespace std;
class Person {
private:
static const int LIMIT = 25;
string lname;
char fname[LIMIT];
public:
Person(){lname = "", fname[0] = '\0';};
Person(const string &ln, const char *fn = "Heyyou");
void Show()const;
void FormalShow()const;
};
#endif

function.cpp
#include <iostream>
#include "ff.h"
using namespace std;
void Person::Show() const
{
cout << lname << endl;
cout << fname;
}
void Person::FormalShow()const
{
cout << fname<<endl;
cout << lname << endl;
}
Person::Person(const string & ln, const char * fn)
{
lname = ln;
int i = 0;
for (; i<LIMIT&&*fn != '\0'; i++, fn++)
{
fname[i] = *fn;
}
fname[i] = '\0';
}

main.cpp
#include <iostream>
#include "ff.h"
#include <cstdlib>
#include <string>
int main()
{
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
cout << endl;
one.FormalShow();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: