C++学习(六)——类的学习—PhoneList对象数组程序示例
2016-10-30 21:05
399 查看
main.cpp
#include <fstream>
#include <string>
#include <iostream>
//#include <stdlib.h>
#include <cstdlib>
#include "PhoneList.h"
using namespace std;
#define FILE_IN "D:\\C++projects\\Book_Project\\9_5\\phonebook.txt"
int main(void)
{
PhoneList mylist[5];
ifstream Input;
Input.open(FILE_IN, ios::in);
if (!Input)
{
cout<<"文件不存在"<<endl;
exit(1);
}
int i, a, ex, num;
char buf[35];
for (i =0; i<5; ++i)
{
Input.getline(buf, 25);
mylist[i].SetName(buf);
Input.getline(buf, 30);
mylist[i].SetAddress(buf);
Input.getline(buf, 4);
a = atoi(buf);
Input.clear();
Input.getline(buf, 5);
ex = atoi(buf);
Input.clear();
Input.getline(buf, 7);
num = atoi(buf);
Input.clear();
mylist[i].SetPhone(a, ex, num);
}
cout.setf(ios::left);
cout<<"你的地址薄有下列信息:"<<endl;
for (i = 0; i<5; ++i)
{
mylist[i].ShowListing();
}
cout<<endl;
Input.close();
cout<<"bye"<<endl;
return 0;
}
PhoneList.h
#ifndef _PHONELIST_H_
#define _PHONELIST_H_
class PhoneList
{
public:
PhoneList();
void SetName(char n[]);
void SetAddress(char addr[]);
void SetPhone(int a, int e, int num);
void ShowListing();
private:
char name[25], address[30];
int areacode, exchange, number;
};
#endif // _PHONELIST_H_
PhoneList.cpp
#include "PhoneList.h"
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
PhoneList::PhoneList()
{
name[0] = '\0';
address[0] = '\0';
areacode = 0;
exchange = 0;
number = 0;
}
void PhoneList::SetName(char n[])
{
strcpy(name, n);
}
void PhoneList::SetAddress(char addr[])
{
strcpy(address, addr);
}
void PhoneList::SetPhone(int a, int e, int num)
{
areacode = a;
exchange = e;
number = num;
}
void PhoneList::ShowListing()
{
cout<<endl<<setw(25)<<name<<setw(30)<<address;
cout<<areacode<<"-"<<exchange<<"-"<<number;
}
#include <fstream>
#include <string>
#include <iostream>
//#include <stdlib.h>
#include <cstdlib>
#include "PhoneList.h"
using namespace std;
#define FILE_IN "D:\\C++projects\\Book_Project\\9_5\\phonebook.txt"
int main(void)
{
PhoneList mylist[5];
ifstream Input;
Input.open(FILE_IN, ios::in);
if (!Input)
{
cout<<"文件不存在"<<endl;
exit(1);
}
int i, a, ex, num;
char buf[35];
for (i =0; i<5; ++i)
{
Input.getline(buf, 25);
mylist[i].SetName(buf);
Input.getline(buf, 30);
mylist[i].SetAddress(buf);
Input.getline(buf, 4);
a = atoi(buf);
Input.clear();
Input.getline(buf, 5);
ex = atoi(buf);
Input.clear();
Input.getline(buf, 7);
num = atoi(buf);
Input.clear();
mylist[i].SetPhone(a, ex, num);
}
cout.setf(ios::left);
cout<<"你的地址薄有下列信息:"<<endl;
for (i = 0; i<5; ++i)
{
mylist[i].ShowListing();
}
cout<<endl;
Input.close();
cout<<"bye"<<endl;
return 0;
}
PhoneList.h
#ifndef _PHONELIST_H_
#define _PHONELIST_H_
class PhoneList
{
public:
PhoneList();
void SetName(char n[]);
void SetAddress(char addr[]);
void SetPhone(int a, int e, int num);
void ShowListing();
private:
char name[25], address[30];
int areacode, exchange, number;
};
#endif // _PHONELIST_H_
PhoneList.cpp
#include "PhoneList.h"
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
PhoneList::PhoneList()
{
name[0] = '\0';
address[0] = '\0';
areacode = 0;
exchange = 0;
number = 0;
}
void PhoneList::SetName(char n[])
{
strcpy(name, n);
}
void PhoneList::SetAddress(char addr[])
{
strcpy(address, addr);
}
void PhoneList::SetPhone(int a, int e, int num)
{
areacode = a;
exchange = e;
number = num;
}
void PhoneList::ShowListing()
{
cout<<endl<<setw(25)<<name<<setw(30)<<address;
cout<<areacode<<"-"<<exchange<<"-"<<number;
}
相关文章推荐
- C++ 标准库 vector list map 基本使用方法 示例程序
- C++对象数组的实例学习
- 学习笔记_JavaSE_15_对象数组、Collection、list
- C/C++小程序学习:将大于且靠近m的t个素数存入指定数组
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
- C++学习(构造函数与析构函数、对象数组与对象指针)
- JNI学习笔记 C++传递结构体、String、数组对象给JavaC++传递结构体、String、数组对象给Java
- 【C++再学习】【06】程序以及对象在内存中的布局
- 【C++基础学习】成员对象与对象数组
- C++ 标准库 vector list map 基本使用方法 示例程序
- 【c++程序】在数组对象中初始化成员变量
- C++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- VS2010 C++学习(6): ListCtl用法及ADO方式读写Access程序
- java学习(12)对象数组初始化 java与C++的区别
- C++学习笔记21 多态遇上对象数组
- 记录学习过程中碰到的json对象数组字符串转成list方法
- C++学习笔记(六)-- 类和对象 构造函数和析构函数 const成员函数 this指针 对象数组 堆栈管理变量
- PHP学习笔记之显示数组与对象示例
- C/C++学习之C提高----C程序数组排序、数据类型(大小,别名,封装)、变量、程序的内存四区模型(栈,堆,全局,代码区)
- 改善C++ 程序的150个建议学习之建议34:用智能指针管理通过new创建的对象