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());
}
}
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=#
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;
}
Get Answers For Free
Most questions answered within 1 hours.