现在的位置: 首页 > 综合 > 正文

【CPP Training】Data Abstraction and Hiding the Implementation PA

2013年04月23日 ⁄ 综合 ⁄ 共 12685字 ⁄ 字号 评论关闭

      培训内容简要回顾

      1)数据类型

            a)什么是数据类型

            b)C语言数据类型的回顾

            c)引用类型

      2)类与对象

            a)类的概念

            b)类的定义

            c)类的属性

            d)类的方法

            e)类的成员访问

            f)类成员的访问控制

      3)构造函数与析构函数

            a)构造函数

            b)析构函数

            c)构造函数与析构函数的执行顺序

      4)动态对象的创建

            a)程序运行时内存划分的区域

            b)C语言中动态分配内存的方法

            c)C++语言中动态分配内存的方法

            d)使用new运算符函数

            e)使用delete运算符函数

      参考资料(Links)

      1)数据抽象

      2)隐藏代码的实现

      3)初始化与清理

      4)动态创建对象

      编程练习题目




      程序的输入

1 GUOHONGYUN 15XXXXXXXXX 6XXXXXXXXX@qq.com
1 LICHAO 15XXXXXXXXX 1XXXXXXXXX@qq.com
1 XIAYUZHE 15XXXXXXXXX 5XXXXXXXXX@qq.com
1 POSONG 13XXXXXXXXX 3XXXXXXXXX@qq.com
1 LIUBO 15XXXXXXXXX 6XXXXXXXXX@qq.com
1 LEIYU 18XXXXXXXXX 1XXXXXXXXX@qq.com
1 ZHANGCANGJU 13XXXXXXXXX 3XXXXXXXXX@qq.com
1 XIANGKAILIN 18XXXXXXXXX 2XXXXXXXXX@qq.com
2 HUANGHAO 15XXXXXXXXX 1XXXXXXXXX@qq.com
2 ZHUJIAJUN 15XXXXXXXXX 5XXXXXXXXX@qq.com
2 ZHONGCHENG 13XXXXXXXXX 5XXXXXXXXX@qq.com
3 LEIJUNAN 13XXXXXXXXX 1XXXXXXXXX@163.com
3 YANGBO 15XXXXXXXXX 9XXXXXXXXX@qq.com
4 WEIWEI 15XXXXXXXXX hXXXXXXXXX@gmail.com
4 LIUTINGWEI 15XXXXXXXXX tXXXXXXXXX@gmail.com
4 OUYANGYU 15XXXXXXXXX oXXXXXXXXX@163.com
5 ZHANGXINHAO 15XXXXXXXXX 9XXXXXXXXX@qq.com
5 HOUTINGWU 15XXXXXXXXX 8XXXXXXXXX@qq.com
6 KONGQIAO 13XXXXXXXXX 1XXXXXXXXX@qq.com
6 SIXU 13XXXXXXXXX sXXXXXXXXX@163.com
6 ZHANGJINJING 18XXXXXXXXX 6XXXXXXXXX@qq.com
7 YANJUNHUA 13XXXXXXXXX 9XXXXXXXXX@qq.com
7 HUANGRUI 13XXXXXXXXX 1XXXXXXXXX@qq.com

      一种实现的方式

      main.cpp

#include <iostream>

// Informing the complier the
// contact book you have written 
#include "ContactBook.h"

// Test script for the class
int main()
{
    // Declare a object representing the contact book

    ContactBook myContactBook;
    const std::string szContactBookPath = "./Contact.txt";
    const std::string szOutputContactBookPath = "./ContactModified.txt";

    // Load the contact book

    std::cout << "Load contact book from file " 
	          << szContactBookPath << "..." << std::endl;

    myContactBook.loadContactBook( szContactBookPath );

    std::cout << std::endl;

    //////////////////////////////////////////////////////////////////////////

    // Display all contact infomation

    std::cout << "Display contact book..." << std::endl;

    myContactBook.displayAll();

    std::cout << std::endl;

    //////////////////////////////////////////////////////////////////////////

    // Display sigle contact infomation

    std::cout << "Display POSONG's contact information..." << std::endl;

    myContactBook.displaySingle( "POSONG" );

    std::cout << std::endl;

    //////////////////////////////////////////////////////////////////////////

    // Update POSONG's contact information

    std::cout << "Update POSONG's contact information..." << std::endl;

    ContactInfo infoToUpdate;
    infoToUpdate.nInstructorNO = 1;
    infoToUpdate.szName = "POSONG";
    infoToUpdate.szPhoneNO = "12345678900";
    infoToUpdate.szEmail = "123456789@qq.com";

    myContactBook.update( infoToUpdate );

    // Display POSONG's contact infomation to see if
    // it is successfully updated

    std::cout << "Display POSONG's contact information after updating it..." 
	          << std::endl;

    myContactBook.displaySingle( "POSONG" );

    std::cout << std::endl;

    //////////////////////////////////////////////////////////////////////////

    // Add someone else's contact information

    std::cout << "Adding RC's contact information..." << std::endl;

    ContactInfo infToAdd;
    infToAdd.nInstructorNO = 5;
    infToAdd.szName = "RC";
    infToAdd.szPhoneNO = "00987654321";
    infToAdd.szEmail = "987654321@qq.com";

    myContactBook.add( infToAdd );

    // Display RC's contact infomation to see if
    // it is successfully updated

    std::cout << "Display RC's contact information after adding it..." << std::endl;

    myContactBook.displaySingle( "RC" );

    std::cout << std::endl;

    //////////////////////////////////////////////////////////////////////////

    // Display the contact book before saving it

    std::cout << "Display contact book before saving it..." << std::endl;

    myContactBook.displayAll();

    std::cout << std::endl;

    // Save the contact book

    std::cout << "Save the contact book to file " 
	          << szOutputContactBookPath << "..." << std::endl;

    myContactBook.saveContactBook( szOutputContactBookPath );

    std::cin.get();

    return 0;
}

      ContactBook.h

#ifndef _CONTACT_BOOK_
#define _CONTACT_BOOK_

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

/**
* @brief Structure representing one's contact information
*/
typedef struct tagContactInfo
{
    // Instructor #
    int nInstructorNO;

    // Name
    std::string szName;

    // Phone Number
    std::string szPhoneNO;

    // Email
    std::string szEmail;

} ContactInfo;

class ContactBook
{
public:

    /** Constructor */
    ContactBook();

    /** Deconstructor */
    ~ContactBook();

public:

    /** Load contact book from file. 
    * Read the file with the specified name and
    * insert the contact infomation into the memory 
    * (i.e. the m_contactBook property of the class).
    *
    * @param const std::string & szContactFilePath File name of the contact book
    * @return bool Indicating whether the operation is successful
    * @see
    * @note
    */
    bool loadContactBook( const std::string & szContactFilePath );

    /** Save the contact book to file.
    * Save the contact book stored in the memory 
    * (i.e. the m_contactBook property of the class) to file.
    *
    * @param const std::string & szContactFilePath File name of the contact book
    * @return bool Indicating whether the operation is successful
    * @see
    * @note
    */
    bool saveContactBook( const std::string & szContactFilePath);

    /** Display all the contact book
    * Display all the contact infomation stored in the MEMORY.
    * Since we just display what is already loaded in the m_contactBook,
    * NO file reading operation is necessary.
    *
    * @param void
    * @return void
    * @see
    * @note
    */
    void displayAll( void );

    /** Display one's contact information
    * Display one's contact information stored in the MEMORY.
    * Since we just display what is already loaded in the m_contactBook,
    * NO file reading operation is necessary. If the name does not exist
    * in the contact book, please prompt it to us as well.
    *
    * @param const std::string & szName The name who we want to know the contact information
    * @return void
    * @see
    * @note
    */
    void displaySingle( const std::string & szName );

    /** Update one's contact information
    * Update one's contact information stored in the MEMORY.
    * Since we just update what is already loaded in the m_contactBook,
    * NO file reading or writing operation is required. If you want to
    * update file at the same time, just call the method 'saveContactBook'
    * in the main function after calling this method.
    *
    * @param const ContactInfo & contactInfo Contact infomation required to be updated
    * @return void
    * @see saveContactBook
    * @note
    */
    void update( const ContactInfo & contactInfo );

    /** Add one's contact information
    * Add one's contact information stored in the MEMORY.
    * Since we just add to what is already loaded in the m_contactBook,
    * NO file reading or writing operation is required. If you want to
    * update file at the same time, just call the method 'saveContactBook'
    * in the main function after calling this method.
    *
    * @param const ContactInfo & contactInfo Contact infomation required to be added
    * @return void
    * @see saveContactBook
    * @note
    */
    void add( const ContactInfo & contactInfo );

private:

    /** Property for contact book storage.
    * The 'm_' prefix means that the variable is 
    * a member variable of the class.
    */
    std::vector<ContactInfo> m_contactBook;
};

#endif

      ContactBook.cpp

#include "ContactBook.h"

/** Constructor */
ContactBook::ContactBook()
{
    std::cout << "Constructor of the class \'ContactBook\'." << std::endl;
}

/** Deconstructor */
ContactBook::~ContactBook()
{
     std::cout << "Deconstructor of the class \'ContactBook\'." << std::endl;
}

/** Load contact book from file. 
* Read the file with the specified name and
* insert the contact infomation into the memory 
* (i.e. the m_contactBook property of the class).
*
* @param const std::string & szContactFilePath File name of the contact book
* @return bool Indicating whether the operation is successful
* @see
* @note
*/
bool ContactBook::loadContactBook( const std::string & szContactFilePath )
{
    std::ifstream inFile( szContactFilePath );

    if ( !inFile.is_open() )
    {
        return false;
    }

    m_contactBook.clear();

    ContactInfo info;

    while ( true )
    {
        bool bSuccessful = false;
        bSuccessful = ( inFile >> info.nInstructorNO >> info.szName >> info.szPhoneNO >> info.szEmail );

        if ( !bSuccessful )
        {
            break;
        }
        
        m_contactBook.push_back( info );
    }

    inFile.close();

    return true;
}

/** Save the contact book to file.
* Save the contact book stored in the memory 
* (i.e. the m_contactBook property of the class) to file.
*
* @param const std::string & szContactFilePath File name of the contact book
* @return bool
* @see
* @note
*/
bool ContactBook::saveContactBook( const std::string & szContactFilePath )
{
    std::ofstream outFile( szContactFilePath );

    if ( !outFile.is_open() )
    {
        return false;
    }

    for ( int i = 0; i < m_contactBook.size(); i++ )
    {
        outFile << m_contactBook[i].nInstructorNO << " ";
        outFile << m_contactBook[i].szName << " ";
        outFile << m_contactBook[i].szPhoneNO << " ";
        outFile << m_contactBook[i].szEmail << std::endl;
    }

    outFile.close();

    return true;
}

/** Display all the contact book
* Display all the contact infomation stored in the MEMORY.
* Since we just display what is already loaded in the m_contactBook,
* NO file reading operation is necessary.
*
* @param void
* @return void
* @see
* @note
*/
void ContactBook::displayAll( void )
{
    if ( m_contactBook.empty() )
    {
        std::cout << "There is no information stored in the contact book." << std::endl;
        return;
    }
    
    for ( int i = 0; i < m_contactBook.size(); i++ )
    {
        std::cout << m_contactBook[i].nInstructorNO << " ";
        std::cout << m_contactBook[i].szName << " ";
        std::cout << m_contactBook[i].szPhoneNO << " ";
        std::cout << m_contactBook[i].szEmail << std::endl;
    }
}

/** Display one's contact information
* Display one's contact information stored in the MEMORY.
* Since we just display what is already loaded in the m_contactBook,
* NO file reading operation is necessary. If the name does not exist
* in the contact book, please prompt it to us as well.
*
* @param const std::string & szName The name who we want to know the contact information
* @return void
* @see
* @note
*/
void ContactBook::displaySingle( const std::string & szName )
{
    bool bFound = false;

    for ( int i = 0; i < m_contactBook.size(); i++ )
    {
        if ( szName == m_contactBook[i].szName )
        {
            std::cout << m_contactBook[i].nInstructorNO << " ";
            std::cout << m_contactBook[i].szName << " ";
            std::cout << m_contactBook[i].szPhoneNO << " ";
            std::cout << m_contactBook[i].szEmail << std::endl;

            bFound = true;

            break;
        }
    }

    if ( !bFound )
    {
        std::cout << "There is no contact information associated with name " << szName << "." << std::endl;
    }
}

/** Update one's contact information
* Update one's contact information stored in the MEMORY.
* Since we just update what is already loaded in the m_contactBook,
* NO file reading or writing operation is required. If you want to
* update file at the same time, just call the method 'saveContactBook'
* in the main function after calling this method.
*
* @param const ContactInfo & contactInfo Contact infomation required to be updated
* @return void
* @see saveContactBook
* @note
*/
void ContactBook::update( const ContactInfo & contactInfo )
{
    bool bFound = false;

    for ( int i = 0; i < m_contactBook.size(); i++ )
    {
        if ( contactInfo.szName == m_contactBook[i].szName )
        {
            m_contactBook[i] = contactInfo;

            bFound = true;

            break;
        }
    }

    if ( !bFound )
    {
        std::cout << "There is no contact information associated with name " << contactInfo.szName << "." << std::endl;
    }
}

/** Add one's contact information
* Add one's contact information stored in the MEMORY.
* Since we just add to what is already loaded in the m_contactBook,
* NO file reading or writing operation is required. If you want to
* update file at the same time, just call the method 'saveContactBook'
* in the main function after calling this method.
*
* @param const ContactInfo & contactInfo Contact infomation required to be added
* @return void
* @see saveContactBook
* @note
*/
void ContactBook::add( const ContactInfo & contactInfo )
{
    m_contactBook.push_back( contactInfo );
}

      基于上述实现程序的输出

Constructor of the class 'ContactBook'.
Load contact book from file ./Contact.txt...

Display contact book...
1 GUOHONGYUN 15XXXXXXXXX 6XXXXXXXXX@qq.com
1 LICHAO 15XXXXXXXXX 1XXXXXXXXX@qq.com
1 XIAYUZHE 15XXXXXXXXX 5XXXXXXXXX@qq.com
1 POSONG 13XXXXXXXXX 3XXXXXXXXX@qq.com
1 LIUBO 15XXXXXXXXX 6XXXXXXXXX@qq.com
1 LEIYU 18XXXXXXXXX 1XXXXXXXXX@qq.com
1 ZHANGCANGJU 13XXXXXXXXX 3XXXXXXXXX@qq.com
1 XIANGKAILIN 18XXXXXXXXX 2XXXXXXXXX@qq.com
2 HUANGHAO 15XXXXXXXXX 1XXXXXXXXX@qq.com
2 ZHUJIAJUN 15XXXXXXXXX 5XXXXXXXXX@qq.com
2 ZHONGCHENG 13XXXXXXXXX 5XXXXXXXXX@qq.com
3 LEIJUNAN 13XXXXXXXXX 1XXXXXXXXX@163.com
3 YANGBO 15XXXXXXXXX 9XXXXXXXXX@qq.com
4 WEIWEI 15XXXXXXXXX hXXXXXXXXX@gmail.com
4 LIUTINGWEI 15XXXXXXXXX tXXXXXXXXX@gmail.com
4 OUYANGYU 15XXXXXXXXX oXXXXXXXXX@163.com
5 ZHANGXINHAO 15XXXXXXXXX 9XXXXXXXXX@qq.com
5 HOUTINGWU 15XXXXXXXXX 8XXXXXXXXX@qq.com
6 KONGQIAO 13XXXXXXXXX 1XXXXXXXXX@qq.com
6 SIXU 13XXXXXXXXX sXXXXXXXXX@163.com
6 ZHANGJINJING 18XXXXXXXXX 6XXXXXXXXX@qq.com
7 YANJUNHUA 13XXXXXXXXX 9XXXXXXXXX@qq.com
7 HUANGRUI 13XXXXXXXXX 1XXXXXXXXX@qq.com

Display POSONG's contact information...
1 POSONG 13XXXXXXXXX 3XXXXXXXXX@qq.com

Update POSONG's contact information...
Display POSONG's contact information after updating it...
1 POSONG 12345678900 123456789@qq.com

Adding RC's contact information...
Display RC's contact information after adding it...
5 RC 00987654321 987654321@qq.com

Display contact book before saving it...
1 GUOHONGYUN 15XXXXXXXXX 6XXXXXXXXX@qq.com
1 LICHAO 15XXXXXXXXX 1XXXXXXXXX@qq.com
1 XIAYUZHE 15XXXXXXXXX 5XXXXXXXXX@qq.com
1 POSONG 12345678900 123456789@qq.com
1 LIUBO 15XXXXXXXXX 6XXXXXXXXX@qq.com
1 LEIYU 18XXXXXXXXX 1XXXXXXXXX@qq.com
1 ZHANGCANGJU 13XXXXXXXXX 3XXXXXXXXX@qq.com
1 XIANGKAILIN 18XXXXXXXXX 2XXXXXXXXX@qq.com
2 HUANGHAO 15XXXXXXXXX 1XXXXXXXXX@qq.com
2 ZHUJIAJUN 15XXXXXXXXX 5XXXXXXXXX@qq.com
2 ZHONGCHENG 13XXXXXXXXX 5XXXXXXXXX@qq.com
3 LEIJUNAN 13XXXXXXXXX 1XXXXXXXXX@163.com
3 YANGBO 15XXXXXXXXX 9XXXXXXXXX@qq.com
4 WEIWEI 15XXXXXXXXX hXXXXXXXXX@gmail.com
4 LIUTINGWEI 15XXXXXXXXX tXXXXXXXXX@gmail.com
4 OUYANGYU 15XXXXXXXXX oXXXXXXXXX@163.com
5 ZHANGXINHAO 15XXXXXXXXX 9XXXXXXXXX@qq.com
5 HOUTINGWU 15XXXXXXXXX 8XXXXXXXXX@qq.com
6 KONGQIAO 13XXXXXXXXX 1XXXXXXXXX@qq.com
6 SIXU 13XXXXXXXXX sXXXXXXXXX@163.com
6 ZHANGJINJING 18XXXXXXXXX 6XXXXXXXXX@qq.com
7 YANJUNHUA 13XXXXXXXXX 9XXXXXXXXX@qq.com
7 HUANGRUI 13XXXXXXXXX 1XXXXXXXXX@qq.com
5 RC 00987654321 987654321@qq.com

Save the contact book to file ./ContactModified.txt...
Deconstructor of the class 'ContactBook'.

抱歉!评论已关闭.