【C++ Primer Plus习题】10.2
问题:
解答:
main.cpp
#include <iostream>
#include "Person.h"
using namespace std;int main()
{Person one;Person two("Smythecraft");Person three("Dimwiddy", "Sam");one.FormalShow();one.Show();cout << endl;two.FormalShow();two.Show();cout << endl;three.FormalShow();three.Show();return 0;
}
Person.h
#pragma once
#include <iostream>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;
};
Person.cpp
#include "Person.h"Person::Person(const string& ln, const char* fn )
{strcpy_s(this->fname, fn);this->lname = ln;
}
void Person::Show() const
{cout << "名:" << this->fname << endl;cout << "姓:" << this->lname << endl;
}
void Person::FormalShow() const
{cout << "姓:" << this->lname << endl;cout << "名:" << this->fname << endl;
}
运行结果:
考查点:
- 类和对象
2024年9月3日20:16:27