21xrx.com
2024-09-20 06:06:14 Friday
登录
文章检索 我的文章 写文章
C++多文件编程实例
2023-07-05 06:39:03 深夜i     --     --
C++语言 多文件编程 编程实例 模块化设计 面向对象编程

C++是现代编程语言中应用最广泛的一种,其可以用于开发各种类型的应用程序和系统软件。在C++编程中,常常需要使用多个文件来实现复杂的功能。本文将介绍C++多文件编程的一个实例。

在一个项目中,可能会有多个C++源文件和头文件,这些文件需要相互协作来完成某个功能。为了方便管理和组织代码,通常会将这些文件分为多个模块,并保存在不同的文件夹中。每个模块都可能包含一个或多个C++源文件和相应的头文件。

以一个简单的图书管理系统为例,假设需要实现以下功能:

1. 添加图书:输入图书名称、作者、出版社等信息,并将其保存到图书列表中。

2. 删除图书:输入要删除的图书的名称或ISBN号,从图书列表中删除该图书。

3. 显示图书列表:列出图书列表中已有的所有图书。

为了实现这些功能,需要创建三个不同的C++源文件,分别为:book.cpp、booklist.cpp和main.cpp,并创建相应的头文件book.h和booklist.h。

book.cpp负责定义图书的具体实现,包括名称、作者、ISBN号、价格等信息的存储和获取方法。其代码如下:


#include "book.h"

void Book::SetName(string strName)

  m_strName = strName;

string Book::GetName()

  return m_strName;

void Book::SetAuthor(string strAuthor)

  m_strAuthor = strAuthor;

string Book::GetAuthor()

  return m_strAuthor;

void Book::SetISBN(string strISBN)

  m_strISBN = strISBN;

string Book::GetISBN()

  return m_strISBN;

void Book::SetPrice(float fPrice)

  m_fPrice = fPrice;

float Book::GetPrice()

  return m_fPrice;

booklist.cpp负责定义图书列表的具体实现,包括添加图书、删除图书和列出图书列表等方法。其代码如下:


#include "booklist.h"

#include <algorithm>

using namespace std;

BookList::BookList()

BookList::~BookList()

void BookList::AddBook(Book book)

{

  m_lstBook.push_back(book);

}

bool BookList::DelBook(string strName)

{

  vector<Book>::iterator it;

  for(it=m_lstBook.begin(); it!=m_lstBook.end(); it++)

  {

    if(it->GetName() == strName)

    {

      m_lstBook.erase(it);

      return true;

    }

  }

  return false;

}

void BookList::ShowList()

{

  vector<Book>::iterator it;

  for(it=m_lstBook.begin(); it!=m_lstBook.end(); it++)

  {

    cout << "Book Name: " << it->GetName() << endl;

    cout << "Author: " << it->GetAuthor() << endl;

    cout << "ISBN: " << it->GetISBN() << endl;

    cout << "Price: " << it->GetPrice() << endl << endl;

  }

}

main.cpp是程序的主函数,负责调用其他两个源文件中的函数,以实现图书管理系统的基本功能。其代码如下:


#include <iostream>

#include "book.h"

#include "booklist.h"

using namespace std;

int main()

{

  BookList bookList;

  Book book;

  int nOp = 0;

  while(true)

  {

    cout << "1. Add Book" << endl;

    cout << "2. Delete Book" << endl;

    cout << "3. Show Book List" << endl;

    cout << "4. Exit" << endl;

    cout << "Please choose operation: ";

    cin >> nOp;

    if(nOp == 1)

    {

      string strName, strAuthor, strISBN;

      float fPrice;

      cout << "Please Input Book Name: ";

      cin >> strName;

      cout << "Please Input Book Author: ";

      cin >> strAuthor;

      cout << "Please Input Book ISBN: ";

      cin >> strISBN;

      cout << "Please Input Book Price: ";

      cin >> fPrice;

      book.SetName(strName);

      book.SetAuthor(strAuthor);

      book.SetISBN(strISBN);

      book.SetPrice(fPrice);

      bookList.AddBook(book);

      cout << "Add Book Success!" << endl << endl;

    }

    else if(nOp == 2)

    {

      string strName, strISBN;

      cout << "Please Input Book Name or ISBN: ";

      cin >> strName;

      if(!bookList.DelBook(strName))

      

        cout << "Book Not Exist!" << endl << endl;

      

      else

      

        cout << "Delete Book Success!" << endl << endl;

      

    }

    else if(nOp == 3)

    {

      bookList.ShowList();

    }

    else if(nOp == 4)

    

      break;

    

    else

    

      cout << "Invalid Operation!" << endl << endl;

    

  }

  return 0;

}

最后,需要将所有的源文件和头文件编译和链接为一个可执行文件。具体的编译和链接命令如下:


g++ -c book.cpp -o book.o

g++ -c booklist.cpp -o booklist.o

g++ -c main.cpp -o main.o

g++ book.o booklist.o main.o -o bookmanage

其中,-c表示编译为目标文件,-o用于指定输出文件名。最后一条命令将目标文件链接为可执行文件bookmanage。

通过这个例子,可以看到多文件编程的基本思路和实现方法。在实际的开发中,多文件编程可以帮助我们管理和组织代码,提高开发效率和可维护性。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复