Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**CODE TO BE IN C++** 1. Create a new project named Lab5. *** Choose Empty Project, NOT Windows Console Application *** 2. Create an application

**CODE TO BE IN C++**

1. Create a new project named Lab5.

*** Choose Empty Project, NOT Windows Console Application ***

2. Create an application within your project also named Lab5. (If you are using

Microsoft Visual Studio, this will be done automatically for you).

3. Ensure you have a single program file in your application name Lab5.cpp.

Project -> Add New Item -> C++ File (set name to Lab5.cpp)

4. Make the necessary changes to your file to print a test message to the screen

(cout) and ensure your window stays open to view the output.

#include

using namespace std;

int main (int argc, char *argv[]) {

cout << Hello << endl;

char c;

cin >> c;

}

5. Create a Pet class. A string (not the string c++ class), is

simply an array of chars with the last char being 0, not 0.

This last 0 is called the null terminator. Your pet will have a

name, which is a char *, and will point to a dynamic array of

chars. It will also have a static int representing the number of

pets that are in existence. Both of these members will be

private. Review the slides on declaring and defining a static

member variable. Initialize your count in the Pet.cpp file as

follows: int Pet::number_pets = 0;

6. Create an inline static getter function to return the number of

pets in existence.

7. Create an inline name getter function. Make sure it returns a

static char *, and that the function itself is also const!!!

8. A helper function is a private function that is only usable

internally in our class functions. Add the following private method to your class, int getNameLength() const, this function

will count the number of letters in the name (not including the

null terminator).

9. Add the following public function to your Pet class void

setName(const char *nm). The first thing your function should

do is test if your pet already has a name, and if so, you must

delete it. (HINT: if name!=nullptr, then delete[] name)

Next, if the function parameter nm is a nullptr, your new name

will be an empty string, ie. array length of 1, with the only

char being the null terminator.

Finally, if nm is not the nullptr, you need to count the size of

the nm string (including the null terminator), allocate your name

to be large enough to hold the copy, and then copy each letter

from nm to name.

(Hint:this function can easily be done in 11 lines)

10.Next you will create a specialized constructor which will also

serve as a default constructor. Recall the default constructor

must take 0 parameters. Modify your constructor to look like the

following Pet(const char *nm=nullptr). This mean if no name is

provided in the constructor, the nm parameter will be a nullptr.

Your constructor should set the pets name to a null pointer, and

simply call the setName function you just created.

You will also want to increment the static number of pets

variable, and print out a message saying Constructor called.

11.Now you will add a copy constructor which takes a const Pet

&src as the only parameter. It should be very similar to your

original constructor. Set name to nullptr, call the setname

function, increment number of pets and print out Copy

constructor

12.Next you will add a move constructor. Recall from lecture and

slides that this form of constructor takes an r-value object

reference, which means the object is not going to be used again.

This allows our constructor to steal the dynamic memory of the

source object. The prototype for the move constructor is as

follows: Pet(Pet &&obj).

In the function, you can assign your name pointer to point to the

objs dynamic name memory. Then change the objs name to be a

null pointer (we do this so that the destructor for obj does not

try to free the memory we stole). Lastly, increment the static

number of pets and print out Move constructor.

13.You will now modify the destructor of Pet to free the dynamic

name memory (delete[]), ONLY IF name!=nullptr! Also, you will

decrement the number of pets and print out Destructor.

14.Next you will overload the assignment operator for the Pet class.

The prototype for this function is: Pet operator=(const Pet

&rhs). This function will automatically be called if you have

P1 and P2 (both objects of class Pet), and you write P1=P2; The

rhs parameter represents a reference to the right hand side

object to the equal sign. Your code should free your name,

allocate enough memory to hold the rhs objects name, and then

copy each letter (and null terminator) over. Finally you must

return yourself, so that you could code P1=P2=P3, etc. You can

refer to yourself by dereferencing the this pointer: *this.

15.Finally you will overload the addition operator, so if you wrote

P3=P1+P2, the name of P3 would be P2s name concatenated after

P1s. The prototype for this function is as follows: Pet

operator+(const Pet &rhs). Remember that you are not modifying

yourself, you are creating and returning a new Pet that has the

this Pets name concatenated with the rhs Pets name.

16.In your main program, create multiple pets and verify that your

static count of pets in existence works correctly. Also ensure

that each constructor and overloaded operator (+ and =) has been

called.

17.You do NOT need to do the following for Lab, but this WILL help

for exam studying!!!

Overload the stream operator, this will allow you to write cout

<< P1 << endl; This function is NOT a method of your class, but

a global function (similar to you main function or any other

function not associated with a class). The prototype looks like

this: ostream& operator<<(ostream& os, const Pet& obj).

Place this function in Pet.cpp. Within the function you want to

treat os like cout.

os << obj.name;

Dont forget to return os from the function!

The problem is that obj.name is private, you could use the

getName function, but in practice you may want to allow direct

access. In order to do that we need to declare our stream

function to be a friend. Add the following friend statement

anywhere in the Pet class declaration: friend ostream&

operator<<(ostream& or, const Pet&obj);

You should be able to write cout << P1 << endl; in your main

program.

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_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

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions

Question

What was the source of the problem here?

Answered: 1 week ago

Question

1. Explain why evaluation is important.

Answered: 1 week ago