将代码拆分为头文件和主代码

问题描述

我尝试将代码拆分为头文件和主代码,但我不知道该怎么做。 具体来说,当我在头文件中编写运算符时遇到问题。我总是得到“重载运算符必须是二元运算符”。我试图对重载运算符使用友元函数,但它不起作用(我在上面遇到了同样的错误) 这是我的代码

#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
class Cash{
        private:
                int dollars;  //private members of the class
                int cents;
                double cash;
                string amt="$";
        public: 
        Cash(int dollars,int cents )  // parameterised constructor accepting two int inputs
        {
                this->dollars=dollars;
                this->cents=cents;
                setcash();         //Calling setcash method to set the cash value
        }
        Cash(int dollars)  //parameterised constructor accepting one int input
        {
                this->dollars=dollars;
                cents=0;
                setcash();  //Calling setcash method to set the cash value
        }
        Cash()  //default constructor
        {
                dollars=0;
                cents=0;
                setcash();  //Calling setcash method to set the cash value
        }
        int getdollar()  //method to return dollar value
        {
                return dollars;
        }
        int getcents() //method to return cents value
        {
                return cents;
        }
        void setcash()  //setcash method deFinition
        {   if (cents==0)
            {
                cash=(double)dollars;
                return;}
                /* converting cent value into dollar to get total cash amount */
                double centindouble=(double)cents;
                double centsindollar=centindouble/pow(10,floor(log10(abs(cents)) + 1)); 
                cash=(double)dollars + centsindollar;
            
        }
        double getcash()  //method to return net cash
        {   
                return cash;
        }
        string toString() //converting cash into string as per question
        {
                
            amt=amt+to_string(cash);
            return amt;
        }
        bool operator ==(Cash &c) //overloading == operator to compare two cash obj
        {
                if(getcash()==c.getcash())
                return true;
                else
                return false;
        }
        bool operator >(Cash &c)//overloading > operator to compare two cash obj
        {
                if(getcash()>c.getcash())
                return true;
                else 
                return false;
        }
        bool operator <(Cash &c)//overloading < operator to compare two cash obj
        {
                if(getcash()<c.getcash())
                return true;
                else 
                return false;
        }
        Cash operator+(Cash c) //overloading + operator to add values of two cash obj
        {
                int dlr=dollars+c.getdollar();
                int cts=cents+c.getcents();
                Cash c1(dlr,cts);
                return c1;
        }

        Cash operator-(Cash &c) //overloading - operator to subtract values of two cash obj
        {  int dlr=dollars-c.getdollar();
                int cts=cents-c.getcents();
                Cash c1(dlr,cts);
                return c1;
        }
        void operator *(int n)//overloading * operator to multiply values of cash obj
        {
          dollars=dollars*n;
          cents=cents*n;
          setcash();    
        }
};

/* Driver Method to Test our Program */
int main()
{
    Cash c(100,5);
    Cash a(100,5);
    if(c==a)
    cout<<"Equal "<<endl;
    Cash d=c+a;
    Cash f=c-a;
    cout<<c.getcash()<<endl;
    cout<<d.getcash()<<endl;
    cout<<f.getcash()<<endl;
    cout<<d.toString();
}

解决方法

在您提供的代码中,您的所有方法都已声明和定义。

要拆分为 .h/.cpp,您需要 .h 中的声明和 .cpp 中的定义

.h

#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
class Cash{
        private:
          int dollars;  //private members of the class
          int cents;
          double cash;
          string amt="$";
        public: 
          Cash(int dollars,int cents );
          Cash(int dollars);
          Cash();
          int getdollar();
          int getcents();
          void setcash();
          double getcash();
          string toString();
          bool operator ==(Cash &c);
          bool operator >(Cash &c);
          bool operator <(Cash &c);
          Cash operator+(Cash c);
          Cash operator-(Cash &c);
          void operator *(int n);   
};

.cpp(示例中只给出了一种方法)

#include "whateveryourname.h"

    Cash::Cash()  //default constructor
    {
            dollars=0;
            cents=0;
            setcash();  //Calling setcash method to set the cash value
    }

此外,我会在构造函数中初始化字符串 amt