C++ Object-Oriented Programming - Program using structs,
functions, and a little of overloaded functions. The finished
program should look exactly like the part at the bottom in gray
text.
Solution:
code:
#include <iostream>
#include <cmath>
#define MAX_SIZE 50
using namespace std;
// create a struct for Money which contains variables for
dollars and cents
struct Money {
int dollars;
int cents;
};
// create class Account which contains variables for Account
amount, interest rate and account name.
struct Account {
Money money;
double interestRate;
char name[MAX_SIZE];
};
// createAccount() is used to setup a new account from user.
Account createAccount() {
Account acc;
cout<<"\nLet's setup your account.";
cout<<"\nFirst, what's the name of the account? ";
cin.getline(acc.name, MAX_SIZE, '\n');
cout<<"\nWhat is the interest rate of your
"<<acc.name<<" account? ";
cin>>acc.interestRate;
cout<<"\nFinally, what's the starting balance of your
"<<acc.name<<" account? $";
double balance;
cin>>balance;
// after getting the decimal amount from the user, we need to
create a new Money object
Money mon;
// For xample, if the user has entered 100.5, then dollars = 100
and cents = 0.5 * 1100 = 50
mon.dollars = floor(balance);
mon.cents = (balance - mon.dollars)*100;
acc.money = mon;
return acc;
}
// deposit() function is used to deposit a given money into the
account.
Account deposit(Account acc, Money deposit) {
// if the amount to be deposited is -ve, print an error
message
if(deposit.dollars < 0 || deposit.cents < 0) {
cout<<"\nSorry, cannot deposit negative amount!!";
return acc;
}
// else add the money into the account
acc.money.dollars += deposit.dollars;
acc.money.cents += deposit.cents;
// if the value of cents becomes more than 100, increment dollar
value by 1
// and set to cents value to cents % 100.
// ex., if new amount becomes 100.140, then dollars = 101 and cents
= 40
if(acc.money.cents >= 100) {
acc.money.cents %= 100;
acc.money.dollars += 1;
}
cout<<"\n$"<<deposit.dollars<<"."<<deposit.cents<<"
deposited into "<<acc.name;
return acc;
}
// withdraw() function is used to withdraw a given money from
the account.
Money withdraw(Account &acc, Money withdraw) {
// if the amount to be withdrawn is -ve, print an error
message
if(withdraw.dollars < 0 || withdraw.cents < 0) {
cout<<"\nSorry, cannot withdraw negative amount!!";
withdraw.dollars = 0;
withdraw.cents= 0;
return withdraw;
}
// else withdraw the given amount
acc.money.dollars -= withdraw.dollars;
acc.money.cents -= withdraw.cents;
// if the value of cents becomes less than 100, decrement dollar
value by 1
// and set to cents value to cents + 100.
// ex., if new amount becomes 100.-40, then dollars = 99 and cents
= 60
if(acc.money.cents < 100) {
acc.money.cents += 100;
acc.money.dollars -= 1;
}
cout<<"\n$"<<withdraw.dollars<<"."<<withdraw.cents<<"
withdrawn from "<<acc.name;
return withdraw;
}
// accrue() method is used to display the account details.
void accrue(Account &acc) {
cout<<"\nAt "<<acc.interestRate<<"%, your
"<<acc.name<<" account earned $"
<<acc.money.dollars<<"."<<acc.money.cents;
}
void print(Account acc) {
cout<<"$"<<acc.money.dollars<<"."<<acc.money.cents;
}
void print(Money money) {
cout<<"$"<<money.dollars<<"."<<money.cents;
}
int main () {
Account account = createAccount();
Money dep;
dep.dollars = 45;
dep.cents = 50;
cout<<"\nTrying to deposit ";
print(dep);
cout<<"....";
account = deposit(account, dep);
dep.dollars = -9;
dep.cents = 50;
cout<<"\nTrying to deposit ";
print(dep);
cout<<"....";
account = deposit(account, dep);
Money with;
with.dollars = 10;
with.cents = 80;
cout<<"\nTrying to withdraw ";
print(with);
cout<<"....";
with = withdraw(account, with);
with.dollars = -9;
with.cents = 50;
cout<<"\nTrying to withdraw ";
print(with);
cout<<"....";
with = withdraw(account, with);
accrue(account);
}
C++ Object-Oriented Programming - Program using structs, functions, and a little of overloaded functions. The finished...
Assignment Description: We will implement a struct int Container similar to C++’s standard template library std::vector but without
using Object-Oriented Programming. The Container struct will only have member variables and user-defined helper functions that will assist with constructing and destructing instances of the Container type, and to
add items to the Container, check the length of the Container, etc. See below for function prototypes and
descriptions. User-Defined Helper-Functions and Container Member Variables
The helper-functions that operate on the Container data type...
Write a C++ program that demonstrates use of programmer-defined
data structures (structs), an array of structs,
passing an array of structs to a function, and returning a struct
as the value of a function. A function in the program should read
several rows of data from a text file. (The data file should
accompany this assignment.) Each row of the file contains a month
name, a high temperature, and a low temperature. The main program
should call another function which...
In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...
Program in C Student Data using Structs In this assignment you will create a program (using multiple .c files) to solve the following problem: You have been hired by the university to create a program that will read in input from a .txt file. This input will contain a student name, student id #, their age and their current gpa. You should use the following struct for your student data: struct Student{ char name[50]; int id; float...
Create two classes and name their types HotdogStand and Money • All relevant classes, functions, and data shall be placed in a namespace called MyAwesomeBusiness • Negative amounts of Money shall be stored by making both the dollars and cents negative • The Money class shall have four (4) constructors – A default constructor that initializes your Money object to $0.00 – A constructor that takes two integers, the first for the dollars and the second for the cents –...
Write a program that calculates the amount of money that you must invest In a savings account at a given interest rate for a given number of years to have a certain dollar amount at me end of the period Y our program will ask the user for the amount the user wants to have at the end of the term (future value). the number of years the user plans to let the money stay in the account, and the...
Create this c++ program. Create to classes and name their type
Account and Money. Follow the instruction listed below. Please read
and implement instructions very carefully
Money Class Requirements - Negative amounts of Money shall be stored by making both the dollars and cents negative The Money class shall have 4 constructors * A default constructor that initializes your Money object to $0.00 A constructor that takes two integers, the first for the dollars and the second for the cents...
Using C programming please
5. Write a multithreaded program to do the following: Accepts two positive integers from the command line as the amounts to withdraw a. and deposit from an account. b. Creates two threads to perform withdraw and deposit operations repectively. Passes the amount to withdraw/deposit as the parameter to thread's runner function c. Waits for both threads to terminate. Since both threads need to update the account balance, it is necessary to protect sections. You may use...
C++ Read and do as instructed on please
(C++Program *** use only condition statements, loops, functions, files, and arrays. Do NOT use material such as classes. Make sure to add comments** You are to write an ATM Program. The ATM should allow the user to do the following: 1. Create account 2. Log in 3. Exit When they press 1, they are asked for first name (capital first letter). Then it should ask for password. The computer should give the...
Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...