Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Assuming the following class definition: class Order { private: float purcha

ID: 3915488 • Letter: C

Question

C++

Assuming the following class definition: class Order { private: float purchase_price_; public: Order ( ) ; float get_purchase_price ( ) const; } ; Order::Order ( ) : purchase_price_ (0.0) { } int Order::get_purchase_price ( ) const { return purchase_price_; }

1. Declare a pointer to an Order object namely p_order_list and properly initialize it at declaration

2. Dynamically allocate an array of 1024 Order objects and assign it to p_order_list. NOTE: this is a dynamic array of Order objects, not an array of pointers to Order objects 3. Write a loop to go thru the entire the array and count the number of Order objects that have purchase price over 1,000 (one thousand). You must use pointer syntax (not array syntax [ ]) by declaring an additional p_order pointer. Also pointer syntax such as (p_order + i) is not allowed meaning you must increment the pointer to move from one array element to another.

4. Finally de-allocate the array memory and reset p_order_list to NULL.

Explanation / Answer

-----------------------------Code-----------------------------

#include<iostream>

using namespace std;

class Order

{

    private:

       

        float purchase_price_;

       

        public:

       

            Order ( ) ;

            float get_purchase_price ( ) const;

           

};

Order::Order ( ) : purchase_price_ (0.0)

{ }

float Order::get_purchase_price ( ) const

{

    return purchase_price_;

}

int main()

{

    // create an Object array

    Order **p_order_list = new Order*[ 1024 ];

   

    int i;

   

    for( i = 0 ; i < 1024 ; i++ )

        // allocate memory to current object

        *(p_order_list + i) = new Order();

   

    int count = 0;

   

    for( i = 0 ; i < 1024 ; i++ )

        // if the current order has purchase price over 1,000

        if( (*(p_order_list + i))->get_purchase_price() > 1000.0 )

            count++;

       

    p_order_list = NULL;

   

    cout<<count;

       

    return 0;

}

Sample Output

0

(a)

    // create an Object array

    Order **p_order_list = new Order*[ 1024 ];

(b)

    int i;

    for( i = 0 ; i < 1024 ; i++ )

        // allocate memory to current object

        *(p_order_list + i) = new Order();

(c)

int count = 0;

    for( i = 0 ; i < 1024 ; i++ )

        // if the current order has purchase price over 1,000

        if( (*(p_order_list + i))->get_purchase_price() > 1000.0 )

            count++;

(d)

p_order_list = NULL;