本文主要是介绍【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
这篇关于【C++ Primer Plus习题】10.2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!