定义一个描述学生通讯录的类AddressBook并输出

题目:定义一个描述学生通讯录的类AddressBook,数据成员包括:姓名studentName、学校studentSchool、电话号码phoneNum和邮编mailBox;成员函数包括:输出各个数据成员的值print(),分别获取各个数据成员的值GetName(),GetScool(),GetNum(),GetBox(),构造函数和析构函数。

(注意:要灵活应用string处理字符串的便利性)

1)创建一个对象(王五  上海师范大学  021—12312345  0123344),验证各个成员函数的有效性

要求:将程序分为一个头文件和一个CPP文件,并且成员函数在类外定义

2)将成员函数的权限改为私有的,观察现象。说明出现问题原因。

3)增加一个默认构造函数,在函数体内设置一些默认值。然后用两种不同的方式实例化AddressBook对象

4)定义一个对象数组AddressBook  stu[4],采用默认构造函数初始化,用cin接受相应的输入,完成数组对象的私有成员的赋值,并用print输出通讯录

5)将默认构造函数删掉,观察创建对象数组stu[4]时出现什么问题?为什么会这样?

此处仅作答1、3、4

代码:

#include<iostream>
#include<string>
using namespace std;
string a1,a2,a3,a4;
class AddressBook{
	private:
		string studentName;
		string studentSchool;
		string phoneNum;
		string mailBox;
	public:
		AddressBook(string studentName,string studentSchool,string phoneNum,string mailBox);
		string GetName();
		string GetSchool();
		string GetNum();
		string GetBox();
		void show();
		~AddressBook();
		AddressBook(){}
		void SetName(string name);
		void SetSchool(string school);
		void SetNum(string num);
		void SetBox(string box);
}; 

AddressBook::AddressBook(string str1,string str2,string str3,string str4){
	studentName=str1;
	studentSchool=str2;
	phoneNum=str3;
	mailBox=str4;
}

string AddressBook::GetName(){
	return studentName;
}

string AddressBook::GetSchool(){
	return studentSchool;
}		

string AddressBook::GetNum(){
	return phoneNum;
}
		
string AddressBook::GetBox(){
	return mailBox;
}

void AddressBook::show(){
	cout<<"姓名:"<<studentName<<endl;
	cout<<"学校:"<<studentSchool<<endl;
	cout<<"电话号码:"<<phoneNum<<endl;
	cout<<"邮编:"<<mailBox<<endl;
}

AddressBook::~AddressBook(){
}

void AddressBook::SetName(string name){
			studentName=name;
		}
		
void AddressBook::SetSchool(string school){
			studentSchool=school;
		}
		
void AddressBook::SetNum(string num){
			phoneNum=num;
		}
		
void AddressBook::SetBox(string box){
			mailBox=box;
		}
		
int main()
{
	cout<<"******************************"<<endl;
	AddressBook example("王五","上海师范大学","021-12312345","0123344");
	example.show();
	cout<<"******************************"<<endl;
	//实例1
	AddressBook example2("张三","伊库尼斯大学","110","111111");
	example2.show();
	cout<<"******************************"<<endl;
	//实例2
	AddressBook example3;
	example3.SetName("田所浩二");
	example3.SetSchool("下北泽大学");
	example3.SetNum("1919810");
	example3.SetBox("114514");
	example3.show();
	cout<<"******************************"<<endl;
	AddressBook stu[4];
	for(int i=0;i<=3;i++){
		cin>>a1>>a2>>a3>>a4;
		stu[i].SetName(a1);
		stu[i].SetSchool(a2);
		stu[i].SetNum(a3);
		stu[i].SetBox(a4);
	}
	for(int i=0;i<=3;i++){
	cout<<"******************************"<<endl;	
	stu[i].show();
	cout<<"******************************"<<endl;
	}
	return 0;
}
  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值