Advertisement
riggnaros

investment class example

Feb 13th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. //investment class
  8.  
  9. class investment {
  10. private:  //attributes (almost always private)
  11.     double invAmt;
  12.     double intRate;
  13.  
  14. public: //methods (allow you to access attributes(private data)
  15.     investment();  //constructor
  16.  
  17.     investment(double amt, double rate); //constructor
  18.     void setIntRate(double rate);  //setters
  19.     void setInvAmt(double amt);  //setters
  20.  
  21.     double getIntRate();  //getters
  22.     double getInvAmt();  //getters
  23.  
  24.     long double futureValue(int years);
  25. };
  26.  
  27. //constructor definition -- set a starting point for your values.
  28.  
  29. investment::investment()
  30. {
  31.     invAmt = 0.0;
  32.     intRate = 0.0;
  33. };
  34. investment::investment(double amt, double rate)
  35. {
  36.     invAmt = amt;
  37.     intRate = rate;
  38. };
  39.  
  40. void investment::setIntRate(double rate)
  41. {
  42.     intRate = rate;
  43. };
  44.  
  45. double investment::getIntRate()
  46. {
  47.     return intRate;
  48. };
  49.  
  50.  
  51. void investment::setInvAmt(double amt)
  52. {
  53.     invAmt = amt;
  54. };
  55.  
  56. double investment::getInvAmt()
  57. {
  58.     return invAmt;
  59. };
  60.  
  61. long double investment::futureValue(int years)
  62. {
  63.     return (invAmt*pow((1 + intRate), years));  //using 'pow' includes math header.
  64. };
  65.  
  66.  
  67. int main()
  68. {
  69.     int years = 0;
  70.     double rate;
  71.     double amt;
  72.     char answer;
  73.  
  74.     cout << "Would you like to use the Investment Program?(Y/N) \n";
  75.     cin >> answer;
  76.  
  77.     while (answer == 'y' || answer == 'Y') {
  78.  
  79.  
  80.         cout << "Please input your interest rate, amount, and number of years: \n";
  81.         cout << "Interest Rate (in percent): \n";
  82.         cin >> rate;
  83.         cout << "Investment Amount: \n";
  84.         cin >> amt;
  85.         cout << "Number of years invested: \n";
  86.         cin >> years;
  87.  
  88.  
  89.         investment myInvestment(amt, rate);
  90.         myInvestment.setIntRate(rate / 100);
  91.         myInvestment.setInvAmt(amt);
  92.  
  93.         cout << "Your investment: " << myInvestment.futureValue(years);
  94.         cout << endl;
  95.  
  96.         cout << "Your interest rate: " << myInvestment.getIntRate();
  97.  
  98.         cout << endl;
  99.         cout << endl;
  100.  
  101.         cout << "Would you like to use the Investment Program?(Y/N) \n";
  102.         cin >> answer;
  103.  
  104.         if (answer == 'n' || answer == 'N')
  105.         {
  106.             cout << "Thank you and have a nice day!";
  107.         }
  108.  
  109.     }
  110.     return 0;
  111. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement