当前位置:天才代写 > C++/C代写,c语言代写代考-100%安全,包过 > 动态内存分配代写 C++代写 代码代写

动态内存分配代写 C++代写 代码代写

2022-07-01 11:22 星期五 所属: C++/C代写,c语言代写代考-100%安全,包过 浏览:462

C++ Practical Week 6

Dynamic Memory Allocation

动态内存分配代写 Part 1 – Dynamic Memory Allocation: Pie Factory Revisit This part of the practical extends last week’s Pie Factory exercise with dynamic memory

Part 1 – Dynamic Memory Allocation: Pie Factory Revisit

This part of the practical extends last week’s Pie Factory exercise with dynamic memory allocation.

 

1.Load the previous week’s solution or:

a. Create a new project.

b. Create the classes Pie, ApplePie, ApricotPie, and RaspberryPie.

The latter three must derive from Pie.

Pie itself should be abstract.

Each type of Pie must implement the polymorphic member function description() which returns a std::string that describes that type of Pie.

Implement an overloaded output operator << to output pies to an std::ostream using the description() function.

c. Add a class called PieFactory with the following functions:

ApplePie createApplePie() const;
ApricotPie createApricotPie() const;
RaspberryPie createRaspberryPie() const;

d. Test that everything is working by implementing the main function to create each type of pie using the PieFactory, and display each pie to the console.

 

2.  动态内存分配代写

In each constructor (no-arg and copy) and destructor of the Pie class hierarchy, add statements to output information to the console regarding for which class that the constructor/destructor is executing. For example,

Pie::Pie() {
  std::cout << "Creating Pie" << std::endl;
}

 

3.

Add a member function to PieFactory called makePie that takes a single std::string argument, which indicates the type of Pie to create, and that returns a bare/raw pointer to a new Pie Hint: pointer types are declared using the asterisk ‘*’.

 

4.  动态内存分配代写

In the main function: prompt the user for the type of pie they would like, read a string from the input, and use makePie to attempt to create the desired type of pie. Your code should have something like the following in main(): remember best practice for input and display a meaningful prompt to the user.

std::string type;
std::cin >> type;
PieFactory pf{};
Pie* pie = pf.makePie(type);
std::cout << pie->description();

 

5.

Run your code. You should see something like the following:

What type of pie would you like? (Apple, Apricot, Raspberry) > apple 
Creating Pie
Creating ApplePie
Here is your delicious Apple Pie
Destroying ApplePie
Destroying Pie

 

6.  动态内存分配代写

Are there any issues with your code? What happens if you enter a string that is not a correct type of pie?

When dealing with pointers you should remember to use, and check for, nullptr where appropriate: e.g, when an invalid type of pie is asked for, makePie should return the nullptr, the client code (i.e., the code in the main function in this case) should check for nullptr and behave appropriately (possibly provide a message to the user that the selection was invalid). Also, you must ensure you delete your pointers when you are finished with them, to free the allocated memory, as it will not be done for you when dealing with raw pointers.

 

7.  动态内存分配代写

Add a member function to PieFactory called makePieUnique that takes a single std::string argument, which indicates the type of Pie to create, and that returns an object of type std::unique_ptr<Pie>. Hint: remember to include the <memory> header and use the function std::make_unique<…>; the type parameter of the call to make_unique will be the specific type of Pie you are creating.

 

8.

In main() call the makePieUnique function and store it in a variable of typestd::unique_ptr<Pie>; display the type of Pie on the console by calling the description() function of the underlying Pie object. Hint: smart pointers (such as unique_ptr and shared_ptr) overload the dereference operators ( * and -> ).

 

 

9.  动态内存分配代写

Add the following code to your main function:

std::unique_ptr<Pie> transfer{}; // empty unique ptr

std::cout << "Transfer ownership to another unique_ptr" << std::endl;
transfer = std::move(pu); // 'pu' is the variable you created in step (9)
std::cout << "- Original: " << (pu ? pu->description() : "no pie") << std::endl;
std::cout << "- Transfer: " << (transfer ? transfer->description() : "no pie") << std::endl;

std::cout << "Transfer ownership back" << std::endl;
transfer.swap(pu); // make 'pu' take ownership of the pointer again.
std::cout << "- Original: " << (pu ? pu->description() : "no pie") << std::endl;
std::cout << "- Transfer: " << (transfer ? transfer->description() : "no pie") << std::endl;

{
  std::unique_ptr<Pie> temp{std::move(pu)};
}
std::cout << "End of local scope" << std::endl;

This code transfers the ownership of the underlying Pie pointer between two unique_ptr objects using two methods: std::move, which can be used with assignment to a unique_ptr, and .swap(), which switches the ownership between two unique_ptr objects. When the latter is used with an empty unique_ptr, it has the effect of transferring ownership and making the original unique_ptr object empty (i.e., equivalent to nullptr).

The last part of the code introduces a local scope in which ownership of the pointer is transferred to a new unique_ptr created within that scope. Therefore, when the scope ends the unique_ptr will be deallocated. What do you expect will happen to the contained Pie object? Run the code to find out.

 

10.  动态内存分配代写

Add a member function to PieFactory called makePieShared that takes a single std::string argument, which indicates the type of Pie to create, and that returns an object of type std::shared_ptr<Pie>. Hint: remember to use the function std::make_shared<…>; the type parameter of the call to make_shared will be the specific type of Pie you are creating.

 

11.

In main() call the makePieShared function and store it in a variable of type std::shared_ptr<Pie>; display the type of Pie on the console by calling thedescription() function of the underlying Pie

 

12.  动态内存分配代写

Add the following code to your main function:

std::shared_ptr<Pie> other{}; // empty shared ptr

// ps is the variable you created in step (12)
std::cout << "- Original: " << (ps ? ps->description() : "no pie") << std::endl;
std::cout << "- Transfer: " << (other ? other->description() : "no pie") << std::endl;

std::cout << "Copy to another shared_ptr" << std::endl;
other = ps; // make 'other' adopt (shared) ownership of the pointer
std::cout << "- Original: " << (ps ? ps->description() : "no pie") << std::endl;
std::cout << "- Transfer: " << (other ? other->description() : "no pie") << std::endl;

{
  std::shared_ptr<Pie> temp{ps};
}
std::cout << "End of local scope" << std::endl;

Like before, this code will create a second shared_ptr and will transfer ownership to another shared_ptr resulting in shared ownership of the underlying Pie pointer. Because of this, standard assignment and copy constructors can be used with shared_ptr. The last part creates a local scope, like before, and transfers ownership to a new shared_ptr. When the scope ends, the shared_ptr will be deallocated as before. What do you expect to happen to the contained Pie object? Run the code to find out.

 

13.  动态内存分配代写

At the end of the main() function write a statement to the console to indicate you have reached the end, e.g., “End of main.” Run the code, what do you notice about the output?

 

14.

Output the number of references that exist to the Pie Hint: call .use_count() on any of the shared_ptr objects themselves, not the underlying object.

 

15.

Try to explicitly destroy the Pie held by the shared_ptrs. Hint: You can use .reset() with no arguments to release a pointer from its ownership within a shared_ptr. Run the code, what is the difference in the output?

 

动态内存分配代写
动态内存分配代写

 

 

更多代写:C语言网课代修北美  网考代考  英国统计网课托管   美国物理report代写  哲学网课论文paper代写  assignment技巧代写

合作平台:essay代写 论文代写 写手招聘 英国留学生代写

 

天才代写-代写联系方式