Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ Construct an Item class that stores the code and price of an item. Anyone can solve it for me please?

armi111

New Coder
Construct an Item class that stores the code and price of an item. This class should create the possibility of creating objects with and without parameters, to return the data of an item and set new values for them. Placement of values can be realized only within the class or by the classes that inherit from it. Use the class with all its functionalities. C++
 
Hi there,

Can you share the code that you’ve started to work with? This can be useful for whoever is trying to help.
 
I'm not going to give you a code answer. Not least because I don't have a C++ IDE handy to syntax check it.

However the question you pose has quite a few facets to it.

* Encapsulation. Private/protected member variables with (or without) getter/setter functions.
* Initialisation and construction/destruction. You can use constructor cascading and/or constructors with initializers.
* Memory allocation. When given a "code" or "price", are they by value or by pointer? Who owns the memory? [1]
* Destructor. If you are allocating the memory inside the class (copy or by value), you will need to deallocate it in the destructor.
* Copy constructor. Again if use internal values. Otherwise you just copy the pointer to the same value.

[1] In all most all languages which use pointers/references true encapsulation is only available if you use by-value or copy mechanics and have the class own the memory. Technically, this is most often ignored and "getter" functions will happily return pointer to a private member. Of course, if you have a pointer to the memory, it's fairly trivial to modify it regardless of the class protections. Thus, if you want proper mutability protection from encapsulation you will need to consider public, shared and private pointers and the management of. It would depend on the level you are expected to be at as to whether this discussion on true internal encapsulation is required or not.
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom