| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3626 人关注过本帖
标题:大家来帮我看一下如何把.h文件和.cpp文件,分别放到头文件和源文件中去
只看楼主 加入收藏
liuguanglei
Rank: 2
等 级:论坛游民
帖 子:41
专家分:26
注 册:2012-8-15
结帖率:80%
收藏
已结贴  问题点数:10 回复次数:4 
大家来帮我看一下如何把.h文件和.cpp文件,分别放到头文件和源文件中去
//大家来帮我看一下如何把.h文件和.cpp文件,分别放到头文件和源文件中去
//这个程序没有问题的
//对银行账户的对象划分
这个作为.h文件
class Accout
{
private:
    int data;
    int balance;
    int password;
    int password2;
    int ID;//这里应该用数组表示
    double annualInterestRate;
    string name;
public:
    Accout()
    {
        password=0;
        balance=0;
        annualInterestRate=0;
    }
    //对字符操作有点问题
/*    char setname(string myname)
    {
        name=myname;
    }*/
    void setpassword(int mypassword2)
    {
       password2=mypassword2;
    }
    void setdata(int mydata)   
    {
        data=mydata;
    }
    void setID(int myid)
    {
        ID=myid;
    }
    void setdeposit(int mydeposit)
    {
        balance=mydeposit;
    }
    void setmessage(int mypassword)
    {
        password=mypassword;
        
    }
    void setwithdrawals(int mywithdrawals)
    {
        
        if(mywithdrawals>=balance)
            cout<<"您的余额不足!\n";
        else
        {
            cout<<"成功!";
            balance-=mywithdrawals;
        }
    }
    void setannualInterestRate(double myannualInterestRate)
    {
        annualInterestRate=myannualInterestRate;
    }
    int getID()
    {
        return ID;
    }
    int getbalance()
    {
        return balance;
    }
    int getdeposit()
    {
        return balance+(balance*annualInterestRate)*data;
    }
    int getpassword()
    {
        return password;
    }
    int getpassword2()
    {
        return password2;
    }
    //char getname()
    //{
    //    return name;
    //}
};
这个作为.cpp文件
void main()
{
     Accout ti;
     int n,m;
     int k;
     ti.setannualInterestRate(0.005);
     cout<<"****************************个人银行管理系统*****************************"<<endl;
     cout<<endl;
     cout<<endl;
     cout<<"*****************************创建一个用户********************************"<<endl;
     cout<<"请输入您要创建的账号:";
     cin>>n;
     ti.setID(n);
     int i;
     int j;
     printf("请创建您的密码:");
     cin>>i;
     ti.setmessage(i);
     cout<<"您的账户已经创建成功!"<<endl;
//     ti.setname("liuguanglei");
     cout<<"你的当前账户余额为0!"<<"\n";
     cout<<endl;
   while(1)
     {
        cout<<"请您再次输入密码!\n";
        cin>>j;
        ti.setpassword(j);
        if(ti.getpassword()!=ti.getpassword2())
         cout<<"密码错误!";
             else
             {
                  cout<<"请您选择您要操作的菜单:"<<endl;
                  cout<<"1-存款\n"<<"2-取款\n"<<"3-查看余额\n"<<"4-转账操作\n";
                  cout<<endl;
    while(1)
     {
          cin>>m;
          switch(m)
          {
              case 1:cout<<"请输入要存款的数目:\n";
                     cin>>n;
                     cout<<"请输入存款的天数:";
                     cin>>k;
                     ti.setdeposit(n);
                     ti.setdata(k);
                     cout<<"成功!\n"<<"请选择其他功能:\n";break;
              case 2:
                     cout<<"请你输入要取款的金额:";
                     cin>>m;
                     ti.setwithdrawals(m);
                     cout<<"请选择其他功能:\n";break;
              case 3:
                     cout<<"你当前的账户余额为:"<<ti.getdeposit()<<"\n";
                     cout<<"返回!\n";break;
              case 4:
                     cout<<"目前本ATM机在不支持转账功能!!!\n"<<"请选择其他功能:";
          }
    }
             }
   }
}
搜索更多相关主题的帖子: password private balance 
2012-11-20 23:36
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:1 
这个还蛮有使用价值的啊 ~~先看看   有些地方还没学透的  
这里只能帮顶了

有心者,千方百计;无心者,千难万难。
2012-11-21 16:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:1 
在需要用到的cpp中#include相应的头文件
还能说什么呢?
2012-11-21 16:17
宋立鹏
Rank: 2
等 级:论坛游民
帖 子:30
专家分:16
注 册:2012-7-5
收藏
得分:2 
在cpp文件中加上#include<,h>文件就可以了啊
2012-11-21 18:03
天剑山
Rank: 4
等 级:业余侠客
威 望:3
帖 子:119
专家分:266
注 册:2012-8-3
收藏
得分:6 
C++Primer推荐把类的定义,变量的extern,函数的extern 还有模板定义放在h头文件中,他们的定义放在cpp中
//文件accout.h
#ifndef ACCOUT_H
#define ACCOUT_H
class Accout
{
private:
    int data;
    int balance;
    int password;
    int password2;
    int ID;//这里应该用数组表示
    double annualInterestRate;
    string name;
public:
    Accout()
    {
        password=0;
        balance=0;
        annualInterestRate=0;
    }
    //对字符操作有点问题

//下面这些最好放在cpp中,并包含头文件accout.h,这些楼主自己做吧
/*    char setname(string myname)
    {
        name=myname;
    }*/
    void setpassword(int mypassword2)
    {
       password2=mypassword2;
    }
    void setdata(int mydata)   
    {
        data=mydata;
    }
    void setID(int myid)
    {
        ID=myid;
    }
    void setdeposit(int mydeposit)
    {
        balance=mydeposit;
    }
    void setmessage(int mypassword)
    {
        password=mypassword;
        
    }
    void setwithdrawals(int mywithdrawals)
    {
        
        if(mywithdrawals>=balance)
            cout<<"您的余额不足!\n";
        else
        {
            cout<<"成功!";
            balance-=mywithdrawals;
        }
    }
    void setannualInterestRate(double myannualInterestRate)
    {
        annualInterestRate=myannualInterestRate;
    }
    int getID()
    {
        return ID;
    }
    int getbalance()
    {
        return balance;
    }
    int getdeposit()
    {
        return balance+(balance*annualInterestRate)*data;
    }
    int getpassword()
    {
        return password;
    }
    int getpassword2()
    {
        return password2;
    }
    //char getname()
    //{
    //    return name;
    //}
};
这个作为.cpp文件
//包含accout.h
#include <accout.h>
void main()
{
     Accout ti;
     int n,m;
     int k;
     ti.setannualInterestRate(0.005);
     cout<<"****************************个人银行管理系统*****************************"<<endl;
     cout<<endl;
     cout<<endl;
     cout<<"*****************************创建一个用户********************************"<<endl;
     cout<<"请输入您要创建的账号:";
     cin>>n;
     ti.setID(n);
     int i;
     int j;
     printf("请创建您的密码:");
     cin>>i;
     ti.setmessage(i);
     cout<<"您的账户已经创建成功!"<<endl;
//     ti.setname("liuguanglei");
     cout<<"你的当前账户余额为0!"<<"\n";
     cout<<endl;
   while(1)
     {
        cout<<"请您再次输入密码!\n";
        cin>>j;
        ti.setpassword(j);
        if(ti.getpassword()!=ti.getpassword2())
         cout<<"密码错误!";
             else
             {
                  cout<<"请您选择您要操作的菜单:"<<endl;
                  cout<<"1-存款\n"<<"2-取款\n"<<"3-查看余额\n"<<"4-转账操作\n";
                  cout<<endl;
    while(1)
     {
          cin>>m;
          switch(m)
          {
              case 1:cout<<"请输入要存款的数目:\n";
                     cin>>n;
                     cout<<"请输入存款的天数:";
                     cin>>k;
                     ti.setdeposit(n);
                     ti.setdata(k);
                     cout<<"成功!\n"<<"请选择其他功能:\n";break;
              case 2:
                     cout<<"请你输入要取款的金额:";
                     cin>>m;
                     ti.setwithdrawals(m);
                     cout<<"请选择其他功能:\n";break;
              case 3:
                     cout<<"你当前的账户余额为:"<<ti.getdeposit()<<"\n";
                     cout<<"返回!\n";break;
              case 4:
                     cout<<"目前本ATM机在不支持转账功能!!!\n"<<"请选择其他功能:";
          }
    }
             }
   }
}

饿....机器问题,没有测试,凑活看看吧....反正只有第一段话是重点....
2012-11-24 14:33
快速回复:大家来帮我看一下如何把.h文件和.cpp文件,分别放到头文件和源文件中去 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012424 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved