Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

A class called Pair has already been declared, but the constructors have not been implemented yet. Pair has two public member variables: int *pa,*pb; These

A class called Pair has already been declared, but the constructors have not been implemented yet. Pair has two public member variables:

int *pa,*pb;

These two "pointers to int" are intended to point to heap memory locations that store integers. The remainder of the Pair class expects the following functionality.

A single constructor Pair(int a, int b): This should set up pa and pb to point to newly allocated memory locations on the heap. The integers at those memory locations should be assigned values according to the constructor's integer arguments a and b.

A copy constructor Pair(const Pair& other): This takes as its argument a read-only reference to another Pair. It should set up the newly constructed Pair as a "deep copy," that is, it should create a new Pair that is equivalent to the other Pair based on dereferenced values but does not reuse any of the same memory locations. In other words, the copy constructor should set up its own instance's member variables pa and pb to point to newly allocated memory locations for integers on the heap; those memory locations must be new, not the same locations pointed to by the other Pair, but the integers at these new locations should be assigned values according to the integers that the other Pair is pointing to.

A destructor ~Pair() that de-allocates all of the the heap memory that had previously been allocated for this Pair's members.

The types of these member functions have already been declared in the declaration of Pair. Now you need to provide the implementation of each of these three member functions.

(Note: The function declarations shown in the code comment below do not include parameter names for the arguments. They show only the types of the arguments. This is allowed for a declaration, but when you define the implementation of those functions, you should give names to the parameters so that you can refer to them.)

(Note: The function declarations shown in the code comment below do not include parameter names for the arguments. They show

(Note: The function declarations shown in the code comment below do not include parameter names for the arguments. They show only the types of the arguments. This is allowed for a declaration, but when you define the implementation of those functions, you should give names to the parameters so that you can refer to them.) 1 2 3 4- 5 6 10 11 12 13 * Class Pair has already been declared * as shown in the following comments: * * class Pair { public: * 7 8 * 9 27 28 E E3 int *pa, *pb; Pair(int, int); Pair(const Pair &); ~Pair()); * }; * * Implement its member functions below. 14 15 16 17 18 19 20 21 22 int main() { 23 24 25 26 29 30 31 32 } *Here is a main() function you can use * to check your implementation of the * class Pair member functions. * Pair p(15,16); Pair q(p); Pair *hp = new Pair(23,42); delete hp; std::cout < < "If this message is printed," < < at least the program hasn't crashed yet!" < < < "But you may want to print other diagnostic messages too." < < std::endl; return 0;

Step by Step Solution

3.36 Rating (168 Votes )

There are 3 Steps involved in it

Step: 1

C Language Include Using namespace std Class pair Public int pa pb ... 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_2

Step: 3

blur-text-image_3

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

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students explore these related Programming questions