Question

Complete this in C++ and explain what is being done. 1      Introduction The functions in the...

Complete this in C++ and explain what is being done.

1      Introduction

The functions in the following subsections can all go in one big file called pointerpractice.cpp.

1.1     Basics

Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to.

Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its square.

Write a function, void square 3(int& p), that takes an int by reference and replaces it with its square.

Write a function void display square () that reads a number from the user and displays its square three times, once using each of the above functions. Write a main that calls display square as a simple “test”.

1.2     Arrays

Write a function, void replace with increment (int∗ p, size t n) that takes the base pointer of an array of size n as an argument and increments each of the items in the array.

Write a function, void display increment array (), that makes an array with 10, 20, and 30 in it, passes it to replace with increment , and prints its elements after that call. Modify main to call this function and test it.

1.3      Dynamic Allocation

Write a function, counter∗ make broken counter(), that creates a counter as a local variable and returns its address. Compile your program with all warnings enabled (-Wall for command line folks, otherwise look in compiler settings in your IDE). Note the warnings/errors.

Write a function, void display broken counter (), that uses the function above to make a counter, increments and prints its count. Then have it call a few other functions (doesn’t matter what functions as long as they some local variables), then try to increment the counter and print its count again. Call this function from your main. What happens?

Write a function, counter∗ make counter(), that creates a counter using new and returns the resulting pointer.

Write a function, void display counter (), that uses the function above to make a counter, increments and prints its count. Then have it call a few other functions (doesn’t matter what functions as long as they some local variables), then try to increment the counter and print its count again. Call this function from your main. What happens?

Write a function, counter∗ make counter(int start ), that creates a counter using new, passing start to its constructor, and returns the resulting pointer.

Write a function, void display counter 2 (), that calls this function and displays the resulting counter’s count. Modify main to call this function...

Write a function, counter∗ bunch of counters ( size t number), that uses new to create an array of number counters. Return the base pointer of the array.

Write a function, void display counters (), that uses the function above to make an array of 25 counters, increments them each 5 times and then prints each of their counts. Modify main to call this function.

2      Bank Account

In the following subsections you will create an account class that manages a dynamically-sized collection of transactions. As we have not learned about dealing with some aspects of dynamic memory allocation in classes, your code will leak memory. Do not use this class as a reference for future projects (please put a note in it saying just that). The goal is to learn the basics before cluttering your mind and code with C++-isms.

2.1     The transaction class

Create a class called transaction with the following instance variables:

int amount

string type

Create a two-argument constructor that allows the caller to specify values for all of these variables. Create (const) “getter” methods for each of these variables.

2.2     Test first!

Remember to write unit tests for each public method before you implement it.

2.3     The account class basics

Create an account class with the following instance variables:

int balance – balance in cents

size t           transactions cap – capacity of transactions list

size t           transactions size – number of “used” slots in transaction array

transaction ∗∗ transactions – base pointer to dynamically allocated array of pointers to transactions

Write a constructor that initializes balance to zero, transactions size to zero, transactions cap to 30 and dynamically allocates a collection of transaction cap transaction pointers.

Write methods deposit and withdraw that simply update the balance variable for now. Include assertions to make sure that the amount deposited/withdrawn was valid. Will also need a get_balance method should be a constant that returns an int.

2.4      Managing the transactions array

Implement the following private methods:

void add transaction ( transaction ∗t) that:

– Checks if there is enough room in the transactions array and, if not, calls resize transactions () – Adds the transaction ∗t to the “end” of the partially filled transactions array. – Increments transactions size

void resize transactions () – doubles the capacity of the transactions array, copying old elements over to the new array and updating instance variables as appropriate.

Implement the following public methods:

size t           get transactions size () const – returns the number of elements in the transactions array

transaction ∗ get transaction ( size t i) const – (Precondition: i < transactions cap ) returns the transaction at index i in the transactions collect

Modify withdraw and deposit so that they create transaction objects describing the transaction and add them to the transaction list (use type ”deposit” or ”withdraw” as appropriate). Add unit tests for withdraw and deposit to make sure that these transactions are created and that they contain the correct data.

2.5     Interactive demo

Create a demo that works as the example shows below.

---

Current balance: 0

Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: d amount to deposit: 1000

---

Current balance: 1000

Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: v

There are 1 transactions. Give transaction you wish to view: 1

Transaction 1: amount = 1000, type = deposit

---

Current balance: 1000

Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: w amount to withdraw: 50

---

Current balance: 950

Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: v

There are 2 transactions. Give transaction you wish to view: 9 Invalid transaction number. Enter a number between 1 and 2.

There are 2 transactions. Give transaction you wish to view: 2

Transaction 2: amount = 50, type = withdraw

---

Current balance: 950

Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: x Invalid command.

---

Current balance: 950

Enter operation: (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit: e Bye!

header file:

counter.h

#ifndef COUNTER_H_
#define COUNTER_H_
#include <ostream>

namespace csis3700 {
class counter {
private:
int count;
public:
counter();
counter(int initial_count);
void increment(int amount=1);
int get_count() const;
};

std::ostream& operator<<(std::ostream& outs, counter c);
counter operator+(counter first, counter second);
}

#endif

counter.cpp

#include "counter.h"
#include <ostream>

namespace csis3700 {
counter::counter() {
count=0;
}

counter::counter(int initial_count) {
count = initial_count;
}
  
void counter::increment(int amount) {
count += amount;
}


int counter::get_count() const {
return count;
}

std::ostream& operator<<(std::ostream& outs, counter c) {
outs << "counter(" << c.get_count() << ")";
return outs;
}

counter operator+(counter first, counter second) {
return counter(first.get_count() + second.get_count());
}


}

Homework Answers

Answer #1

Answer 1

// Example program
#include <iostream>
#include <string>
using namespace std;
int square1(int* p)
{
return ((*p)*(*p));
}
void square2(int* p)
{
*p=((*p)*(*p));
}
void square3(int& p)
{
p=p*p;
}
void displaySquare()
{
int num;
cout<<"\nEnter number : ";
cin>>num;
int *point;
point=&num;
cout<<"\nNum is : "<<*point;
cout<<"\nSquare1 of Num is : "<<square1(point);
cout<<"\nNum is : "<<*point;
square2(point);
cout<<"\nSquare2 of Num is : "<<*point;
cout<<"\nNow Num is : "<<*point;
square3(num);
cout<<"\nSquare3 of Num is : "<<num;
}
void replace_with_increment(int* p,size_t n)
{
for(int i=0;i<n;i++)
*(p+i)=*(p+i)+1;
}
void display_increment_array()
{
int arr[]={10,20,30};
int *point;
point=arr;
cout<<"\nArray is : \n";
for(int i=0;i<3;i++)
cout<<arr[i]<<" ";
replace_with_increment(point,3);
cout<<"\nArray is : \n";
for(int i=0;i<3;i++)
cout<<arr[i]<<" ";
  
}
int main()
{
displaySquare();
display_increment_array();
return 0;
}

OUTPUT

Answer 2

#include <iostream>
#include <string>
using namespace std;
class transaction
{
public:
int amount;
string type;
transaction(int am,string ty)
{
amount=am;
type=ty;
}
int getterAmount()
{
return amount;
}
string getterType()
{
return type;
}
};

class account
{
public:
int balance; // balance in cents
size_t transactions_cap;// capacity of transactions list
size_t transactions_size;// number of “used” slots in transaction array
transaction *transactions;
account()
{
balance=0;
transactions_cap=30;
transactions_size=0;
transactions=new transaction[transactions_cap];
for(int i=0;i<transactions_cap;i++)
transactions[i]=new transaction(0," ");
}
void deposit(int amount)
{
if(amount>=0)
{
balance+=amount;
transaction obj(amount,"deposit");
add_transaction(&obj);
}
else
cout<<"\n\tIncorrect amount deposit attempt..!";
}
void withdraw(int amount)
{
if(amount>=0&&amount<=balance)
{
balance-=amount;
transaction obj(amount,"withdraw");
add_transaction(&obj);
}
else
cout<<"\n\tIncorrect amount withdraw attempt..!";
}
int get_balance()
{
return balance;
}
void add_transaction(transaction *t)
{
if(transactions_size<30)
{
transactions[transactions_size]=*t;
transactions_size++;
}
else
{
resize_transactions();
transactions[transactions_size]=*t;
transactions_size++;
}
}
void resize_transactions()
{
transaction *transactions2=new transaction[transactions_cap*2];
for (int i=0; i<transactions_cap; i++)
{
transactions2[i] = transactions[i];
}
transactions_cap=transactions_cap*2;
delete [] transactions;
transactions=transactions2;
}
size_t get_transactions_size()
{
return transactions_size;
}
transaction get_transaction(size_t i)
{
if(i<transactions_cap)
return transactions[i-1];
}
};
int main()
{
account abc;
char choice;
do
{
cout<<"\n--------------";
cout<<"\nCurrent balance : "<<abc.get_balance();
cout<<"\nEnter operation -> (d)eposit, (w)ithdraw, (v)iew transaction or (e)xit : ";
cin>>choice;
if(choice=='d'||choice=='D')
{
int amount;
cout<<"\nAmount to deposit : ";
cin>>amount;
abc.deposit(amount);
}
else if(choice=='w'||choice=='w')
{
int amount;
cout<<"\nAmount to withdraw : ";
cin>>amount;
abc.withdraw(amount);
}
else if(choice=='v'||choice=='V')
{
int tNum;
cout<<"\nThere are "<<abc.get_transactions_size()<<" transactions.";
cout<<"\nGive transaction number you wish to view : ";
cin>>tNum;
transaction obj(0,"");
obj=abc.get_transaction(tNum);
cout<<"\nTransaction = "<<tNum<<"\tamount = "<<obj.getterAmount()<<" type = "<<obj.getterType();
}
else if(choice=='e'||choice=='E')
{
cout<<"\n\tExiting from Program...";
}
else
{
cout<<"\n\tWrong Choice Input..Try Again";
}
}while(choice!='e'||choice!='E');
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT