1. Trang chủ
  2. » Lịch sử lớp 11

Lập trình hướng đối tượng: Workshop 3 - Telephone Directory

4 13 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 80,62 KB

Nội dung

Include in your design the following public member functions: • a modifier that loads the data from an open file.. bool loadFileData(FILE* fp);.[r]

(1)

Part B – C++ Foundation Playing with file data

Workshop 3: Telephone Directory LEARNING OUTCOME

Upon successful completion of this workshop, you will have demonstrated the ability

• to design a derived type that includes instances of another derived type, and

• to use dynamic memory allocation

EXERCISE Listing struct

Design a C++ struct named Listing that holds personal information for a single individual The information includes their given name, surname, home address and telephone number You may assume that each data item contains no more than

15 characters

Include in your design the following public member functions: • a modifier that loads the data from an open file

bool loadFileData(FILE* fp);

receives a pointer (fp) to an open file and reads a single record from that file The record ends with a newline delimiter The fields are the given name, the surname, the home address and the telephone number, in that order, and are separated by '|' characters Your function returns true, if it reads the record successfully; your function returns false and stores a safe empty state if it fails to read a record for any reason

• a query for the given name

const char* given() const;

(2)

returns the given name as a null-terminated C-style string • a query for the surname

const char* surname() const;

returns the surname as a null-terminated C-style string • a query for the address

const char* address() const;

returns the home address as a null-terminated C-style string • a query for the telephone number

const char* telephone() const;

returns the telephone number as a null-terminated C-style string TDirectory struct

Design a second struct named TDirectory that holds the personal information for a number of Listings The number of listings is unspecified at compile time

Include in your design the following public member functions: • a modifier that loads the listing data from a file

bool loadFileData(const char* filename);

receives the name of the file from which to load the data Each record on this file is delimited by a newline character Your function returns true if successful; false otherwise

• a query that displays the listings

void display() const;

inserts each directory listing into the standard output stream Your function uses the member functions of your Listing data type to extract the data

(3)

from each listing object To define a field width of 15 on the standard output stream, use the width(int) member function on the cout object For example, the following code produces the output on the right side

The width(int) function only sets the field width for the next piece of data to be inserted into the output stream

• a wrap-up modifier

void close();

cleans up the TDirectory object in whatever way is necessary Module Design

Place your struct declarations in a header file named TDirectory.h Place your function definitions in an implementation file named TDirectory.cpp

(4)

The main program that uses an instance of your TDirectory derived type is:

This main program prompts for and reads the name of the file that contains the listings The program assumes that the file name contains no more than 60 characters

The output from the program when used with your module looks something like:

Ngày đăng: 09/03/2021, 05:26

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w