⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10_332_2.cpp

📁 我学习C++ Primer Plus过程中写下的课后作业的编程代码
💻 CPP
字号:
/*
2.下面是一个非常简单的类定义:

class Person {
	private:
		static const int LIMIT = 25;
		string lname;				// Person's last name
		char   fname[LIMIT];		// Person's first name
	public:
		Person() { lname = ""; fname[0] = '\0'; }
		Person(const string & ln, const char * fn = "Heyyou");	// #2
		// the following methods display lname and fname
		void Show() const;		    // firstname lastname format
		void FormalShow() const;	// lastname, firstname format
};
通过提供未定义的方法的代码来完成这个类的实现.然后,编写一个使用这个类的程序,
它使用了三种可能的构造函数调用(没有参数,一个参数和两个参数)以及两种显示方法.
下面是一个使用这些构造函数和方法的例子:
	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>
#include <cstring>
#include <string>

class Person {
	private:
		enum { LIMIT = 25};
		std::string lname;				// Person's last name
		char        fname[LIMIT];		// Person's first name
	public:
		Person() { lname = ""; fname[0] = '\0'; }
		Person(const std::string & ln, const char * fn = "Heyyou");	// #2
		// the following methods display lname and fname
		void Show() const;		    // firstname lastname format
		void FormalShow() const;	// lastname, firstname format
};

int main()
{
	using namespace std;
	Person one;						// use default constructor
	Person two("Smythecraft");		// use #2 with one default argument
	Person three("Dimwiddy","Sam");	// use #2, no defaults

	cout<<"The person one: ";
	one.Show();
	one.FormalShow();
	cout<<endl;

    cout<<"The person two: ";
	two.Show();
	two.FormalShow();
	cout<<endl;

    cout<<"The person three: ";
	three.Show();
	three.FormalShow();
	cout<<endl;
	return 0;
}
Person::Person(const std::string & ln, const char * fn)
{
	lname = ln;
	strncpy(fname,fn,LIMIT-1);
	fname[LIMIT-1] = '\0';
}

void Person::Show()const
{
	std::cout<<fname<<" "<<lname<<std::endl;
}

void Person::FormalShow()const
{
	std::cout<<lname<<", "<<fname<<std::endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -