Write a C++ program to add two dollars and cents.
Answers
Answer:
#include <iostream>
//Class prototype
class Cents;
class Dollars
{
private:
int m_DollarAmount;
public:
Dollars(int dollars = 1) { m_DollarAmount = dollars; }
void getDollar()
{
int x;
std::cin >> x;
void setDollar(int x);
}
void setDollar()
{
int y;
std::cin >> y;
m_DollarAmount = y;
}
//Overload to add Dollars and Cents
friend Dollars operator+(const Dollars &d1, const Cents &c1);
friend Dollars operator+(const Cents &c1, const Dollars &d1);
};
class Cents
{
private:
float m_CentsAmount;
public:
Cents(float cents = 25) { m_CentsAmount = cents; }
void getCents()
{
int cents;
std::cin >> cents;
void setCents(int cents);
}
void setCents()
{
int cents;
std::cin >> cents;
m_CentsAmount = cents;
}
friend Cents operator+(const Dollars &d1, const Cents &c1);
friend Cents operator+(const Cents &c1, const Dollars &d1);
};
Dollars operator+(const Dollars &d1, const Cents &c1)
{
//Confused here on how to add these correctly.
}
Dollars operator+(const Cents &c1, const Dollars &d1)
{
//Confused here on how to add these correctly.
}
Cents operator+(const Cents &c1, const Dollars &d1)
{
//Confused here on how to add these correctly.
}
Cents operator+(const Dollars &d1, const Cents &c1)
{
//Confused here on how to add these correctly.
}
int main()
{
//Initiating objects
Dollars UserCash;
Cents UserCoin;
std::cout << "Please enter the amount of dollars you have: ";
UserCash.getDollar();
std::cout << "Please enter the amount of cents you have: ";
UserCoin.getCents();
return 0;
}
Answer:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
std::cin>>a>>b>>c>>d;
int e=a+c;
int f=b+d;
while(f>100)
{
f=f-100;
break;
}
while(e>100)
{
e=e+1;
break;
}
std::cout<<e<<"\n"<<f;
return 0;
}
Explanation: