Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a C++ coding assignment that I need help with in my Computer Science class. It deals with pointers. Details of the assignment are

This is a C++ coding assignment that I need help with in my Computer Science class. It deals with pointers. Details of the assignment are below.

Assignment 3: Smart Pointers

Purpose

In this lab, you will get some practice using modern C++ smart pointers.

What to Do

Write a program that uses your class from Lab 2 that reports when it is created, copied, and destroyed. In this program you should

  1. Create a raw pointer to a dynamically allocated object of your class. Is this object ever destroyed?
  2. Create a unique_ptr to an object of your class. Note when the object is created, and that it is eventually destroyed. This object should be created with your "other constructor," which takes a parameter.
  3. Transfer ownership of that object to a different unique_ptr. Note that the object itself is not copied.
  4. Demonstrate the calling of a member function of your object through the unique_ptr.
  5. Make a shared_ptr to a dynamically allocated object of your class.
  6. Make another shared_ptr that points at the same object. Note that the object does not get destroyed until both shared_ptrs go out of scope.

Whenever possible, use make_shared and make_unique, rather than calling the constructors for shared_ptr and unique_ptr directly. Ensure that comments exist in main.cpp thatclearlyindicate which portion of the assignment is being implemented.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Class files from lab 2.

Class .cpp file:

//Gator.cpp

#include "Gator.hpp" #include

using std::cout; using std::endl;

int Gator::getSnap(){ return _snap; }

void Gator::setSnap(int chomp){ _snap = chomp; }

//calling Default Constructor Gator::Gator(){ _snap = 0; cout }

//calling Copy Constructor Gator::Gator(const Gator & obj){ _snap = obj._snap; cout }

//calling Other Constructor Gator::Gator(int snap){ _snap = snap; cout }

//Deconstructing Gator::~Gator(){ cout }

//end of file

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class .hpp file: //Gator.hpp

//Gator header file

#ifndef GATOR_HPP #define GATOR_HPP //class gator class Gator{ public: int getSnap(); void setSnap(int chomp); Gator(); //default constructor Gator(const Gator & obj); //copy constructor Gator(int snap); //other constructor ~Gator(); //destructor private: int _snap; };

#endif

//end of file

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

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions