Code Monkey home page Code Monkey logo

ap1400-2-hw4's Introduction

Advanced Programming - HW4

Homework 4 - Spring 2022 Semester
Deadline: Tuesday Ordibehesht 13st - 11:59 pm

Outline

In this homework we are going to implement our own smart pointers. Specifically we want to implement our custom SharedPtr and UniquePtr classes with almost all functionality of std::shared_ptr and std::unique_ptr.

We want to implement 2 class templates called UniquePtr and SharedPtr with the functions described in the following sections.

note. You are only allowed to alter unique_ptr.hpp/h, shared_ptr.hpp/h and only the debug section of main.cpp.


UniquePtr Class

Define a template class named UniquePtr and add the following functions to the class.

This class should use a member variable called T* _p (T is a template argument) to store a given pointer.

  • Constructor Implement a constructor for your class so the below code would work. Your constructor should be able to store the given dynamic pointer properly inside the class using _p variable.

     UniquePtr<int> ptr{new int{10}};
  • make_unique (outside the class) The prefered way to construct a std::unique_ptr is to use a function called std::make_unique. implement a similar function and make this code work:

     UniquePtr<int> ptr{make_unique<int>(10)};
  • Default Constructor Implement a default constructor for your class so the below code works and assign nullptr to _p.

     UniquePtr<int> ptr;
  • Destructor As you know when dealing a with dynamic pointer in a class, implementing destructor is a neccessaty so implement a proper destructor and delete your dynamic pointer (hint: assign nullptr after deletion).

     ~UniquePtr()
  • Copy Constructor As you already know you cannot copy a UniquePtr, make arrangements so the following code would cause a compile error.

     UniquePtr<int> ptr1{new int{10}};
     UniquePtr<int> ptr2{ptr1};
  • Operator= Exactly like the previous section we should not be able to write the following code as well. Make the copiler to produce an error for this code.

     UniquePtr<int> ptr1{new int{10}};
     UniquePtr<int> ptr2{new int{11}};
     ptr2 = ptr1;
  • get The get() function should return the raw pointer stored in the class.

     UniquePtr<int> ptr{new int{10}};
     std::cout << ptr.get() << std::endl; // output: raw pointer stored in the class
  • Operator* Smart pointers should be able to be dereferenced exactly like raw pointers. make this code work:

     UniquePtr<int> ptr{new int{10}};
     std::cout << *ptr << std::endl; // output: 10
  • Operator-> Smart pointers can use the arrow operator like normal pointers. make this code work as well:

     UniquePtr<std::string> ptr{new std::string{"hello"}};
     std::cout << ptr->length() << std::endl; // output: 5
  • reset The reset() function will delete the ponter and assign nullptr to it:

     void reset();
  • reset The reset() function can have a input and make a new pointer with it after deleting the old pointer:

     UniquePtr<std::string> ptr{new std::string{"hello"}};
     ptr.reset(new std::string{"nice"});
     std::cout << *ptr << std::endl; // output: nice
  • release The release() function returns the stored pointer in the class (like get) with two differences: The UniquePtr class won't store the pointer anymore and also deleting the pointer should not be done by UniquePtr class after calling release().

     UniquePtr<double> ptr{new double{1.567}};
     double *tmp{ptr.release()};
     std::cout << *tmp << std::endl; // output: 1.567
     delete tmp; // manual deletion

SharedPtr Class

Define a template class named SharedPtr and add the following functions to the class.

This class should use a member variable called T* _p (T is a template argument) to store a given pointer.

  • Constructor Implement a constructor for your class so the below code would work. Your constructor should be able to store the given dynamic pointer properly inside the class using _p variable.

     UniquePtr<int> ptr{new int{10}};
  • make_shared (outside the class) The prefered way to construct a std::shared_ptr is to use a function called std::make_shared. implement a similar function and make the code below work.

     SharedPtr<int> ptr{make_shared<int>(10)};
  • Default Constructor Implement a default constructor for your class so the below code works and assign nullptr to _p.

     SharedPtr<int> ptr;
  • Destructor As you know when dealing with dynamic pointers inside a class implementing destructor is a neccessaty so implement a proper destructor and delete your dynamic pointers (do not forget to assign nullptr after deletion).

     ~SharedPtr()
  • Copy Constructor As you already know A key difference between SharedPtr and UniquePtr classes is that we can use copy constrctor and make a copy of SharedPtrs. so the code below should run smoothly.

     SharedPtr<int> ptr1{new int{10}};
     SharedPtr<int> ptr2{ptr1};
  • Operator= Exactly like the previous section we can have operator= for SharedPtrs. again the code below should run without any errors.

     SharedPtr<int> ptr1{new int{10}};
     SharedPtr<int> ptr2{new int{11}};
     ptr2 = ptr1;
  • use_count In SharedPtrs we should have the ability to count the number of instances pointing to a same place. to do this you have to define another member variabel for your SharedPtr class and keep track of this number.

    note. you may have to make some adjusments in the previous functions (such as constructor and ...) to do this.

     SharedPtr<int> ptr1{make_shared<int>(10)};
     std::cout << ptr1.use_count() << std::endl; // output: 1
     SharedPtr<int> ptr2{ptr1};
     std::cout << ptr1.use_count() << std::endl; // output: 2
     std::cout << ptr2.use_count() << std::endl; // output: 2
  • get The get() function should return the raw pointer stored in the class.

     SharedPtr<int> ptr{new int{10}};
     std::cout << ptr.get() << std::endl; // output: raw pointer of the class
  • Operator* Smart pointers should be able to be dereferenced exactly like raw pointers. make this code work:

     SharedPtr<int> ptr{new int{10}};
     std::cout << *ptr << std::endl; // output: 10
  • Operator-> Smart pointers can use the arrow operator like raw pointers. make this code work:

     SharedPtr<std::string> ptr{new std::string{"hello"}};
     std::cout << ptr->length() << std::endl; // output: 5
  • reset The reset() function will delete the pointer and assigns nullptr to it:

     void reset();
  • reset The reset() function can have a input and make a new pointer with it after deleting the old pointer:

     SharedPtr<std::string> ptr{new std::string{"hello"}};
     ptr.reset(new std::string{"nice"});
     std::cout << *ptr << std::endl; // output: nice

Challenge

  • If you reached this section congratulations, there is only one part left. Make arrangements so you can use your custom smart pointers in an if condition, the condition should return false if your smart pointer contains a nullptr and otherwise it should return true.

     UniquePtr<double> ptr{new double{1.567}};
     if(ptr) // => true
         // something
     ptr.reset();
     if(ptr) // => false
         // some other thing

    Make this arrangement for both UniquePtr and SharedPtr classes.


Finally

As mentioned before, do not alter other files already populated except otherwise indicated. In case you want to test your code you may use the debug section of the main.cpp.

if (true) // make false to run unit tests  
{ 
	// debug section 
}  
else  
{  
	::testing::InitGoogleTest(&argc, argv);  
	std::cout << "RUNNING TESTS ..." << std::endl;  
	int ret{RUN_ALL_TESTS()};  
	if (!ret)  
		std::cout << "<<<SUCCESS>>>" << std::endl;  
	else  
	  std::cout << "FAILED" << std::endl;  
}  
return 0;

GOOD LUCK

ap1400-2-hw4's People

Contributors

kianbehzad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.