Question
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
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
- Create a raw pointer to a dynamically allocated object of your class. Is this object ever destroyed?
- 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.
- Transfer ownership of that object to a different unique_ptr. Note that the object itself is not copied.
- Demonstrate the calling of a member function of your object through the unique_ptr.
- Make a shared_ptr to a dynamically allocated object of your class.
- 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started