c++ - 类划分头文件和定义文件

标签 c++

我得分了。此代码有效。但是,每个类定义在自己的头文件(.h)中,每个类的成员函数定义在自己的源文件(.C)中,程序应用程序在自己的源文件中。我不知道该怎么做。它没有完全连接。

我收到以下错误:

C:\Users\Jay Yoon\OneDrive - Parkland College\Desktop\project2.cpp|48|error: 'write_account' was not declared in this scope

#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include <stdlib.h>

using namespace std;

//  CLASS USED IN PROJECT

class account
{
    int acno;
    char name[50];
    int deposit;
    int ssn;
    char address[50];
    int intRate;
public:
    void create_account();  //function to get data from user
    void show_account() const;  //function to show data on screen
    void dep(int);  //function to accept amount and add to balance amount
    void draw(int); //function to accept amount and subtract from balance amount
    void report() const;    //function to show data in tabular format
    int retacno() const;    //function to return account number
    int retdeposit() const; //function to return balance amount
    int retssn() const; //function to return ssn of account
    int retintRate() const;
};         //class ends here

void account::create_account()
{
    cout<<"\nEnter The account No. :";
    cin>>acno;
    cout<<"\n\nEnter The Name of The account holder : ";
    cin.ignore();
    cin.getline(name,50);
    cout<<"\n\nEnter The Address of The account holder : ";
    cin.ignore();
    cin.getline(address,50);
    cout<<"\nEnter SSN of The account : ";
    cin>>ssn;
    ssn=toupper(ssn);
    cout<<"\nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : ";
    cin>>deposit;
    cout<<"\nEnter The Interest Rates : ";
    cin>>intRate;
    cout<<"\n\n\nAccount Created..";
}

void account::show_account() const
{
    cout<<"\nAccount No. : "<<acno;
    cout<<"\nAccount Holder Name : "<<name;
    cout<<"\nAccount Holder's address : "<<address;
    cout<<"\nSSN of Account : "<<ssn;
    cout<<"\nBalance amount : "<<deposit;
    cout<<"\nInterest Rate : "<<intRate;
    cout<<"\nTotal Balance : "<<deposit+deposit*intRate;
}



void account::dep(int x)
{
    deposit+=x;
}

void account::draw(int x)
{
    deposit-=x;
}

void account::report() const
{
    cout<<acno<<setw(8)<<" "<<name<<setw(6)<<" "<<ssn<<setw(6)<<" "<<address<<setw(17)<<" "<<deposit+deposit*intRate/100<<setw(16)<<intRate<<endl;
}


int account::retacno() const
{
    return acno;
}

int account::retdeposit() const
{
    return deposit;
}

int account::retssn() const
{
    return ssn;
}


int account::retintRate() const
{
    return intRate;
}

//   function declaration

void write_account();   //function to write record in binary file
void display_sp(int);   //function to display account details given by user
void display_all();     //function to display all account details
void deposit_withdraw(int, int); // function to desposit/withdraw amount for given account

//   Main function

int main()
{
    char ch;
    int num;
    do
    {
        system("cls");
        cout<<"----------------------------------------";
        cout<<"\nWelcome to the First National Bank\n" << "of Parkland - Account transaction system ";
        cout<<"\n----------------------------------------";
        cout<<"\n\n\t01. Create Account";
        cout<<"\n\n\t02. Deposit";
        cout<<"\n\n\t03. Withdraw";
        cout<<"\n\n\t04. Select Account";
        cout<<"\n\n\t05. Print Accounts";
        cout<<"\n\n\t06. Quit";
        cout<<"\n\n\tSelect Your Option (1-6) ";
        cin>>ch;
        system("cls");
        switch(ch)
        {
        case '1':
            write_account();
            break;
        case '2':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 1);
            break;
        case '3':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 2);
            break;
        case '4':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            display_sp(num);
            break;
        case '5':
            display_all();
            break;
         case '6':
            cout<<"\n\n\tThanks for using bank managemnt system";
            break;
         default :cout<<"\a";
        }
        cin.ignore();
        cin.get();
    }while(ch!='8');

    return 0;
}


//    function to write in file

void write_account()
{
    account ac;
    ofstream outFile;
    outFile.open("account.dat",ios::binary|ios::app);
    ac.create_account();
    outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
    outFile.close();
}

//    function to read specific record from file

void display_sp(int n)
{
    account ac;
    bool flag=false;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\nBALANCE DETAILS\n";

        while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        if(ac.retacno()==n)
        {
            ac.show_account();
            flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nAccount number does not exist";
}


//    function to display all accounts deposit list

void display_all()
{
    account ac;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
    cout<<"=========================================================================================\n";
    cout<<"A/c no.    NAME          SSN         Address             Total Balance     Interest Rate\n" ;
    cout<<"=========================================================================================\n";

    while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        ac.report();
    }
    inFile.close();
}

//   function to deposit and withdraw amounts

void deposit_withdraw(int n, int option)
{
    int amt;
    bool found=false;
    account ac;
    fstream File;
    File.open("account.dat", ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    while(!File.eof() && found==false)
    {
        File.read(reinterpret_cast<char *> (&ac), sizeof(account));
        if(ac.retacno()==n)
        {
            ac.show_account();
            if(option==1)
            {
                cout<<"\n\n\tTO DEPOSITE AMOUNT ";
                cout<<"\n\nEnter The amount to be deposited";
                cin>>amt;
                ac.dep(amt);
            }
            if(option==2)
            {
                cout<<"\n\n\tTO WITHDRAW AMOUNT ";
                cout<<"\n\nEnter The amount to be withdraw";
                cin>>amt;
                int bal=ac.retdeposit()-amt;
                if((bal<500 && ac.retssn()=='S') || (bal<1000 && ac.retssn()=='C'))
                    cout<<"Insufficience balance";
                else
                    ac.draw(amt);
            }
            int pos=(-1)*static_cast<int>(sizeof(ac));
            File.seekp(pos,ios::cur);
            File.write(reinterpret_cast<char *> (&ac), sizeof(account));
            cout<<"\n\n\t Record Updated";
            found=true;
           }
         }
    File.close();
    if(found==false)
        cout<<"\n\n Record Not Found ";
}

我该如何解决这个问题?

最佳答案

就这么简单:

主要.cpp:

#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
#include <stdlib.h>

#include "account.h"

using namespace std;

//   function declaration

void write_account();   //function to write record in binary file
void display_sp(int);   //function to display account details given by user
void display_all();     //function to display all account details
void deposit_withdraw(int, int); // function to desposit/withdraw amount for given account

//   Main function

int main()
{
    char ch;
    int num;
    do
    {
        system("cls");
        cout<<"----------------------------------------";
        cout<<"\nWelcome to the First National Bank\n" << "of Parkland - Account transaction system ";
        cout<<"\n----------------------------------------";
        cout<<"\n\n\t01. Create Account";
        cout<<"\n\n\t02. Deposit";
        cout<<"\n\n\t03. Withdraw";
        cout<<"\n\n\t04. Select Account";
        cout<<"\n\n\t05. Print Accounts";
        cout<<"\n\n\t06. Quit";
        cout<<"\n\n\tSelect Your Option (1-6) ";
        cin>>ch;
        system("cls");
        switch(ch)
        {
        case '1':
            write_account();
            break;
        case '2':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 1);
            break;
        case '3':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            deposit_withdraw(num, 2);
            break;
        case '4':
            cout<<"\n\n\tEnter The account No. : "; cin>>num;
            display_sp(num);
            break;
        case '5':
            display_all();
            break;
        case '6':
            cout<<"\n\n\tThanks for using bank managemnt system";
            break;
        default :cout<<"\a";
        }
        cin.ignore();
        cin.get();
    }while(ch!='8');

    return 0;
}


//    function to write in file

void write_account()
{
    account ac;
    ofstream outFile;
    outFile.open("account.dat",ios::binary|ios::app);
    ac.create_account();
    outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
    outFile.close();
}

//    function to read specific record from file

void display_sp(int n)
{
    account ac;
    bool flag=false;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\nBALANCE DETAILS\n";

    while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        if(ac.retacno()==n)
        {
            ac.show_account();
            flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nAccount number does not exist";
}


//    function to display all accounts deposit list

void display_all()
{
    account ac;
    ifstream inFile;
    inFile.open("account.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
    cout<<"=========================================================================================\n";
    cout<<"A/c no.    NAME          SSN         Address             Total Balance     Interest Rate\n" ;
    cout<<"=========================================================================================\n";

    while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
    {
        ac.report();
    }
    inFile.close();
}

//   function to deposit and withdraw amounts

void deposit_withdraw(int n, int option)
{
    int amt;
    bool found=false;
    account ac;
    fstream File;
    File.open("account.dat", ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout<<"File could not be open !! Press any Key...";
        return;
    }
    while(!File.eof() && found==false)
    {
        File.read(reinterpret_cast<char *> (&ac), sizeof(account));
        if(ac.retacno()==n)
        {
            ac.show_account();
            if(option==1)
            {
                cout<<"\n\n\tTO DEPOSITE AMOUNT ";
                cout<<"\n\nEnter The amount to be deposited";
                cin>>amt;
                ac.dep(amt);
            }
            if(option==2)
            {
                cout<<"\n\n\tTO WITHDRAW AMOUNT ";
                cout<<"\n\nEnter The amount to be withdraw";
                cin>>amt;
                int bal=ac.retdeposit()-amt;
                if((bal<500 && ac.retssn()=='S') || (bal<1000 && ac.retssn()=='C'))
                    cout<<"Insufficience balance";
                else
                    ac.draw(amt);
            }
            int pos=(-1)*static_cast<int>(sizeof(ac));
            File.seekp(pos,ios::cur);
            File.write(reinterpret_cast<char *> (&ac), sizeof(account));
            cout<<"\n\n\t Record Updated";
            found=true;
        }
    }
    File.close();
    if(found==false)
        cout<<"\n\n Record Not Found ";
}

帐户.h:

#pragma once

class account
{
    int acno;
    char name[50];
    int deposit;
    int ssn;
    char address[50];
    int intRate;


public:
    void create_account();  //function to get data from user
    void show_account() const;  //function to show data on screen
    void dep(int);  //function to accept amount and add to balance amount
    void draw(int); //function to accept amount and subtract from balance amount
    void report() const;    //function to show data in tabular format
    int retacno() const;    //function to return account number
    int retdeposit() const; //function to return balance amount
    int retssn() const; //function to return ssn of account
    int retintRate() const;
};

帐户.cpp:

#include "account.h"

#include <iostream>
#include <iomanip>

using namespace std;

void account::create_account()
{
    cout<<"\nEnter The account No. :";
    cin>>acno;
    cout<<"\n\nEnter The Name of The account holder : ";
    cin.ignore();
    cin.getline(name,50);
    cout<<"\n\nEnter The Address of The account holder : ";
    cin.ignore();
    cin.getline(address,50);
    cout<<"\nEnter SSN of The account : ";
    cin>>ssn;
    ssn=toupper(ssn);
    cout<<"\nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : ";
    cin>>deposit;
    cout<<"\nEnter The Interest Rates : ";
    cin>>intRate;
    cout<<"\n\n\nAccount Created..";
}

void account::show_account() const
{
    cout<<"\nAccount No. : "<<acno;
    cout<<"\nAccount Holder Name : "<<name;
    cout<<"\nAccount Holder's address : "<<address;
    cout<<"\nSSN of Account : "<<ssn;
    cout<<"\nBalance amount : "<<deposit;
    cout<<"\nInterest Rate : "<<intRate;
    cout<<"\nTotal Balance : "<<deposit+deposit*intRate;
}



void account::dep(int x)
{
    deposit+=x;
}

void account::draw(int x)
{
    deposit-=x;
}

void account::report() const
{
    cout<<acno<<setw(8)<<" "<<name<<setw(6)<<" "<<ssn<<setw(6)<<" "<<address<<setw(17)<<" "<<deposit+deposit*intRate/100<<setw(16)<<intRate<<endl;
}


int account::retacno() const
{
    return acno;
}

int account::retdeposit() const
{
    return deposit;
}

int account::retssn() const
{
    return ssn;
}


int account::retintRate() const
{
    return intRate;
}

关于c++ - 类划分头文件和定义文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57084041/

相关文章:

c++ - 使用静态版本的 boost::filesystem 时出现有趣的 LNK1104 错误

c++ - 使用 Poco 在 C++ 中的 HTTPS 请求

c++ - 重复的c++模板实例化

c++ - 在智能指针容器中添加一个项目

c++ - 通过队列传递信息时如何避免向下转型?

正则表达式中的 C++ 特殊字符

c++ - 将 GCC 内联汇编 CMOV 转换为 Visual Studio 汇编器

c++ - 无法找到 c :\windows\system32\mfc100d. dll

c++ - 无法构造基模板类

C++ 性能挑战:整数到 std::string 的转换