Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal Practice using the following: Simple file documentation (at minimum your name, date, filename and purpose). Not necessary to document members or functions. Using namespace

Goal

Practice using the following:

Simple file documentation (at minimum your name, date, filename and purpose). Not necessary to document members or functions.

Using namespace to organise your code.

Using header and source files (must have the extensions hpp and cpp respectively).

Using a static member in a class

Using raw pointers and dynamic memory allocation and deallocation (using the new and delete operators).

Using arrays, passing to function and return from functions.

Implementing a sort algorithm. (You will use one from the internet and benchmark it)

We will use smart pointers in a subsequent lab.

Requirements

Implement an Account class using a C++ program. Match the output against the sample output given the provided test harness.

The Account Class

The members must be declared in a header file and implemented in a source file. You may also have another source file containing the test harness.

Account

Class

Fields

$- LAST_NUMBER : int = 1000

- account_number_ : std::string

- balance_ : double

Methods

+ constructor Account( balance : double)

+ destructor ~Account()

+ get_balance() : double

+ print_info() : void

Description of class members

Fields

There are three int fields that are private. One of the fields is also static.

balance_ this is a private double field that represents the balance of this object.

account_number_ this is a private string field that represents the unique identifier of this object.

LAST_NUMBER this is a private static int field that the last number that was used to generate an account number. This is initially set to 1000 and is incremented in the constructor. Hence, each successive object will have a unique number.

Constructors

Account(double balance) This public constructor takes one double argument and does the following:

Assigns the argument to the appropriate field.

It also assigns the static field LAST_NUMBER to the field account_number_ (you will have to convert the int to a string).

Increments the static field LAST_NUMBER, so that future objects will access to a unique value.

Finally, it outputs a suitable text to the console that the constructor was invoked. This statement maybe commented out to reduce screen clutter during development.

Destructors

~Date() This public destructor does not implement any logic at this time. It simply output a suitable text to the console indicating that the destructor was invoked. This statement maybe also be commented out to reduce screen clutter during development.

Methods

There are two public methods:

void Account::print_info() This method outputs the two private fields (account_number_ and balance_) to the console. See the program output for clues on implementing this method.

double Account::get_balance() This method returns the private field balance_. This method is used when sorting the collection of accounts.

In your main.cpp file

The following methods must be declared and implemented to successfully run the test harness.

void test_account() This method is call in the main(). Copy and paste the following statements to exercise your application:

srand(123456789); //use your student number

const int MAX_BALANCE = 100000;

const int SIZE = 20; //change this number to investigate performance

//Week03::Account a1(rand() % MAX_BALANCE);

//Week03::Account a2(rand() % MAX_BALANCE);

//Week03::Account a3(rand() % MAX_BALANCE);

//Week03::Account a4(rand() % MAX_BALANCE);

//a1.print_info();

//a2.print_info();

//a3.print_info();

//a4.print_info();

std::cout << " Creating the list of accounts: ";

Week03::Account** accounts = create_accounts(SIZE, MAX_BALANCE);

std::cout << " Printing the list of accounts: ";

//print_accounts(accounts, SIZE);

std::cout << " Sorting the list of accounts: ";

sort_accounts(accounts, SIZE);

std::cout << " Printing the list of accounts: ";

print_accounts(accounts, SIZE);

std::cout << " Deleting the accounts: ";

for (int i = 0;i < SIZE;i++)

{

delete(accounts[i]);

}

Week03::Account** create_accounts(int size, int max_balance) This method does the following:

Creates an array to store the appropriate number of Account pointers.

Initialize each item to an account object. Use the value rand() % max_balance as argument to the constructor.

Return the array.

void print_accounts(Week03::Account* accounts[], int size) This method will invoke the print_info() method on all of the items in the first argument.

void sort_accounts(Week03::Account** accounts, int size) This method will sort the first arguments according to balances. It does not outputs anything to the screen. This method also emits some performance information. The number of swaps and the time taken to complete the sort.

The timing will be covered in class. The code will be in the appendix of this document.

The members must be declared in a header file and implemented in a source file. You must also have another source file containing the test harness and the helper functions.

Test harness

Call the test_accounts() in your main method.

Sample output

Try to understand why and when constructors and destructor are called.

Creating the list of accounts:

++ Account constructor: 1000

++ Account constructor: 1001

++ Account constructor: 1002

++ Account constructor: 1003

++ Account constructor: 1004

++ Account constructor: 1005

++ Account constructor: 1006

++ Account constructor: 1007

++ Account constructor: 1008

++ Account constructor: 1009

++ Account constructor: 1010

++ Account constructor: 1011

++ Account constructor: 1012

++ Account constructor: 1013

++ Account constructor: 1014

++ Account constructor: 1015

++ Account constructor: 1016

++ Account constructor: 1017

++ Account constructor: 1018

++ Account constructor: 1019

Printing the list of accounts:

List of accounts:

1000: balance: 13259

1001: balance: 26974

1002: balance: 13551

1003: balance: 30354

1004: balance: 18709

1005: balance: 15861

1006: balance: 16906

1007: balance: 21981

1008: balance: 8603

1009: balance: 12911

1010: balance: 18110

1011: balance: 3228

1012: balance: 27918

1013: balance: 17989

1014: balance: 22768

1015: balance: 23599

1016: balance: 7712

1017: balance: 15601

1018: balance: 7038

1019: balance: 21512

Sorting the list of accounts:

86swaps

130s (0.13ms)

Printing the list of accounts:

List of accounts:

1003: balance: 30354

1012: balance: 27918

1001: balance: 26974

1015: balance: 23599

1014: balance: 22768

1007: balance: 21981

1019: balance: 21512

1004: balance: 18709

1010: balance: 18110

1013: balance: 17989

1006: balance: 16906

1005: balance: 15861

1017: balance: 15601

1002: balance: 13551

1000: balance: 13259

1009: balance: 12911

1008: balance: 8603

1016: balance: 7712

1018: balance: 7038

1011: balance: 3228

Deleting the accounts:

-- Account destructor: 1003

-- Account destructor: 1012

-- Account destructor: 1001

-- Account destructor: 1015

-- Account destructor: 1014

-- Account destructor: 1007

-- Account destructor: 1019

-- Account destructor: 1004

-- Account destructor: 1010

-- Account destructor: 1013

-- Account destructor: 1006

-- Account destructor: 1005

-- Account destructor: 1017

-- Account destructor: 1002

-- Account destructor: 1000

-- Account destructor: 1009

-- Account destructor: 1008

-- Account destructor: 1016

-- Account destructor: 1018

-- Account destructor: 1011

Appendix

Code for the timer.hpp file.

/**

* Author : Yan Chernokow

* Filename: timer.hpp

* Purpose : provide a simple way to time code

* Date : February 2, 2022

*/

#pragma once

#include

#include

class Timer

{

public:

Timer()

{

m_StartTimepoint = std::chrono::high_resolution_clock::now();

}

~Timer()

{

Stop();

}

void Stop()

{

auto endTimepoint = std::chrono::high_resolution_clock::now();

auto start = std::chrono::time_point_cast(m_StartTimepoint).time_since_epoch().count();

auto end = std::chrono::time_point_cast(endTimepoint).time_since_epoch().count();

auto duration = end - start;

double ms = duration * 0.001;

std::cout << duration << "us (" << ms << "ms) ";

}

private:

std::chrono::time_point m_StartTimepoint;

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions

Question

please dont use chat gpt or other AI 3 5 .

Answered: 1 week ago