Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program design class Sterling that represent British currency that include t

ID: 3821517 • Letter: C

Question

C++ program design class Sterling that represent British currency that include the following functionality: Private class variables shillings and pence of type int and pound of type long. Default constructor that initializes the class variables with 0. A 3-argument constructor that initializes the class variables. Constructor with 1 parameter of type double interpreted as an amount in pounds that initializes the class variables accordingly (1 pound = 20 shillings = 240 pence). Get- and Set-methods for each class variable. Conversion operator that converts the class instance into a value of type double that represent the amount represented by the instance into pounds. Overloaded binary operators "+" and "-". Overloaded "*" operator that modifies an amount represented by the current class multiplied by a constant passed as a parameter. Overloaded binary "/" operator that computes the ratio of amounts represented by 2 class instances as a floating-point number. toString() method that returns a C++ string representation of the amount represented by the class instance in the format "#p.s.py", where p, s, and py are the amounts of pounds, shilling, and pence, respectively. Add a main() method into your code that demonstrates usage of all methods of class Sterling.

Explanation / Answer


//Sterling.h
#ifndef STERLING_H
#define STERLING_H
#include<iostream>
#include<string>
using namespace std;
class Sterling
{
private:
   long pound;
   int shillings;
   int pence;

public:
   Sterling();
   Sterling(long,int ,int);
   Sterling(long);

   void setPounds(long pounds);
   void setShillings(int);
   void setpence(int);

   long getPounds();
   int getShillings();
   int getpence();


   friend Sterling operator+(Sterling &s1,Sterling &s2);
   friend Sterling operator-(Sterling &s1,Sterling &s2);
   friend Sterling operator*(Sterling &s1,const int val);
   friend float operator/(Sterling &s1,Sterling &s2);


   string toString();
};

#endif STERLING_H

------------------------------------------------------------------------------------------------------

//Sterling.cpp
#include<iostream>
#include<sstream>
#include "Sterling.h"
using namespace std;
Sterling::Sterling()
{
   pound=0;
   shillings=0;
   pence=0;
}
Sterling::Sterling(long pound,int sherling,int pence)
{
   this->pound=pound;
   this->shillings=sherling;
   this->pence=pence;
}
Sterling::Sterling(long pound)
{
   this->pound=pound;
   this->shillings=20*pound;
   this->pence=240*pound;
}

void Sterling::setPounds(long pounds)
{
   this->pound=pound;
}
void Sterling::setShillings(int sherling)
{
   this->shillings=sherling;
}
void Sterling::setpence(int pence)
{
   this->pence=pence;
}

long Sterling::getPounds()
{
   return pound;
}
int Sterling::getShillings()
{
   return shillings;
}
int Sterling::getpence()
{
   return pence;
}


Sterling operator+(Sterling &s1,Sterling &s2)
{
   int pence = s1.pence + s2.pence;
   int shillings = s1.shillings + s2.shillings;
   long pound = s1.pound + s2.pound;

   return Sterling(pound,shillings,pound);
}
Sterling operator-(Sterling &s1,Sterling &s2)
{
   int pence = s1.pence - s2.pence;
   int shillings = s1.shillings - s2.shillings;
   long pound = s1.pound - s2.pound;

   return Sterling(pound,shillings,pound);
}
Sterling operator*(Sterling &s1,const int num)
{
   int pence= s1.pence*num;
   int shillings = s1.shillings*num;
   long pounds = s1.pound*num;

   return Sterling(pence,shillings,pounds);
}
float operator/(Sterling &s1,Sterling &s2)
{

   float t1 = s1.pence + s1.shillings+s1.pound;
   float t2=   s2.pence + s2.shillings+ s2.pound;

   return (t1/t2);
}

string Sterling::toString()
{
   stringstream ss;
   ss<<pound<<" "<<shillings<<" "<<pence<<endl;
   return ss.str();
}

------------------------------------------------------------------------------------------------------


/**Test program for Sterling class*/
//main.cpp
//header files
#include<iostream>
#include<string>
#include "Sterling.h"
using namespace std;
int main()
{

   //create objects for Sterling class
   Sterling s1;
   Sterling s2(1,20,240);
   Sterling s3(1);

   cout<<"s1="<<s1.toString()<<endl;
   cout<<"s2="<<s2.toString()<<endl;
   cout<<"s3="<<s3.toString()<<endl;

   //test operators
   Sterling s4=s1+s2;
   cout<<"s4="<<s4.toString()<<endl;
   Sterling s5=s1-s2;
   cout<<"s5="<<s5.toString()<<endl;
   Sterling s6=s1*4;
   cout<<"s6="<<s4.toString()<<endl;

   cout<<"s4/s2="<<s4/s2<<endl;

   system("pause");
   return 0;
}

------------------------------------------------------------------------------------------------------

sample output:

s1=0 0 0

s2=1 20 240

s3=1 20 240

s4=1 20 1

s5=-1 -20 -1

s6=1 20 1

s4/s2=0.0842912

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote