Class Iteam: Answer to one of the unsolved Stack Overflow Qes
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
I would like to improve my skill on C++ concepts and I came across the following question in StackOverflow, which has been directly asked by a StackOverflow-user without any preliminary attempts.(unfortunately, I have only the image of the question, since the actual post was an unsolved question, it has been removed from the site by the user itself)
Any tips or comments to optimize or improve the code and the correctness issues of the solution would be most welcome.
.
#include <iostream>
#include <string>
#include <unordered_map>
/** Class which has been asked in the question. In short Iteam class will act like
a struct for iteamList class, since I have declared iteamList class as a friend
of Iteam class **/
class iteamList; // forward delaration of iteamList class
class Iteam
private:
int m_code;
int m_price;
int m_quantity;
public:
Iteam() // Defualt
:m_code(0), m_price(0), m_quantity(0)
Iteam(const int& code, const int& price, const int& quntity)// Parameterized
:m_code(code), m_price(price), m_quantity(quntity)
Iteam(const Iteam& rhs) // Copy
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
Iteam(Iteam&& rhs) // Move
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
~Iteam()
//(i) input the data members
inline void inputData()
std::cout<<"Iteam code :"; std::cin>>m_code;
std::cout<<"Iteam price :"; std::cin>>m_price;
std::cout<<"Iteam quantity :"; std::cin>>m_quantity;
// life would be easy after making this statement :) !
friend iteamList;
//(ii) to display the data members
friend std::ostream& operator<<(std::ostream& output, const Iteam& obj);
;
std::ostream& operator<<(std::ostream& output, const Iteam& obj)
return output<<"Iteam code = "<<obj.m_code
<<"t price = "<<obj.m_price
<<"t quantity = "<<obj.m_quantity<<"n";
/******************************************************************************/
/** Implementation of the main class which is independent of the above one.
It can access all members of above class if it has instantiated an object of it.
I have used STL std::unordered_map to store the Items which will be easier to
look up(with an O(1)) at the time of queries of deletion.**/
class iteamList
private:
int m_totalStockValue = 0;
std::unordered_map<int, Iteam> m_iteamMap;
private:
// (iii) appending iteam to the list
inline void AppendIteams(const int& userInput_n)
for(auto i=0; i<userInput_n; ++i)
Iteam obj;
obj.inputData();
std::cout << obj << std::endl;
m_iteamMap.emplace(obj.m_code, obj);
public:
iteamList() = default;
iteamList(const int& userInput_n)
m_iteamMap.reserve(userInput_n);
AppendIteams(userInput_n);
~iteamList()
// (iv) delete the iteam from the list
void deleteIteam(const int& code)
if(m_iteamMap.find(code) != m_iteamMap.cend())
std::cout << m_iteamMap[code] << " has been deleted!n";
m_iteamMap.erase(m_iteamMap.find(code));
else
std::cout <<"Invalid Iteam code!" << std::endl;
// (v) total value of the stock
void claculateStockValue()
m_totalStockValue = 0;
for(const auto &it:m_iteamMap)
m_totalStockValue += (it.second.m_price * it.second.m_quantity);
std::cout <<m_totalStockValue<< std::endl;
;
/**********************************************************************************/
int main()
int number;
std::cout <<"Enter total number of iteams : ";
std::cin>>number;
iteamList obj(number);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
int code;
std::cout <<"Enter code to delete the iteam : ";
std::cin>>code;
obj.deleteIteam(code);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
return 0;
c++ object-oriented c++14 overloading
add a comment |Â
up vote
5
down vote
favorite
I would like to improve my skill on C++ concepts and I came across the following question in StackOverflow, which has been directly asked by a StackOverflow-user without any preliminary attempts.(unfortunately, I have only the image of the question, since the actual post was an unsolved question, it has been removed from the site by the user itself)
Any tips or comments to optimize or improve the code and the correctness issues of the solution would be most welcome.
.
#include <iostream>
#include <string>
#include <unordered_map>
/** Class which has been asked in the question. In short Iteam class will act like
a struct for iteamList class, since I have declared iteamList class as a friend
of Iteam class **/
class iteamList; // forward delaration of iteamList class
class Iteam
private:
int m_code;
int m_price;
int m_quantity;
public:
Iteam() // Defualt
:m_code(0), m_price(0), m_quantity(0)
Iteam(const int& code, const int& price, const int& quntity)// Parameterized
:m_code(code), m_price(price), m_quantity(quntity)
Iteam(const Iteam& rhs) // Copy
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
Iteam(Iteam&& rhs) // Move
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
~Iteam()
//(i) input the data members
inline void inputData()
std::cout<<"Iteam code :"; std::cin>>m_code;
std::cout<<"Iteam price :"; std::cin>>m_price;
std::cout<<"Iteam quantity :"; std::cin>>m_quantity;
// life would be easy after making this statement :) !
friend iteamList;
//(ii) to display the data members
friend std::ostream& operator<<(std::ostream& output, const Iteam& obj);
;
std::ostream& operator<<(std::ostream& output, const Iteam& obj)
return output<<"Iteam code = "<<obj.m_code
<<"t price = "<<obj.m_price
<<"t quantity = "<<obj.m_quantity<<"n";
/******************************************************************************/
/** Implementation of the main class which is independent of the above one.
It can access all members of above class if it has instantiated an object of it.
I have used STL std::unordered_map to store the Items which will be easier to
look up(with an O(1)) at the time of queries of deletion.**/
class iteamList
private:
int m_totalStockValue = 0;
std::unordered_map<int, Iteam> m_iteamMap;
private:
// (iii) appending iteam to the list
inline void AppendIteams(const int& userInput_n)
for(auto i=0; i<userInput_n; ++i)
Iteam obj;
obj.inputData();
std::cout << obj << std::endl;
m_iteamMap.emplace(obj.m_code, obj);
public:
iteamList() = default;
iteamList(const int& userInput_n)
m_iteamMap.reserve(userInput_n);
AppendIteams(userInput_n);
~iteamList()
// (iv) delete the iteam from the list
void deleteIteam(const int& code)
if(m_iteamMap.find(code) != m_iteamMap.cend())
std::cout << m_iteamMap[code] << " has been deleted!n";
m_iteamMap.erase(m_iteamMap.find(code));
else
std::cout <<"Invalid Iteam code!" << std::endl;
// (v) total value of the stock
void claculateStockValue()
m_totalStockValue = 0;
for(const auto &it:m_iteamMap)
m_totalStockValue += (it.second.m_price * it.second.m_quantity);
std::cout <<m_totalStockValue<< std::endl;
;
/**********************************************************************************/
int main()
int number;
std::cout <<"Enter total number of iteams : ";
std::cin>>number;
iteamList obj(number);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
int code;
std::cout <<"Enter code to delete the iteam : ";
std::cin>>code;
obj.deleteIteam(code);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
return 0;
c++ object-oriented c++14 overloading
Your code has a bug. You're supposed to multiply the quantity and the price when computing the total value.
â Snowbody
Mar 23 at 21:35
@Snowbody Thanks for pointing out this. m_totalStockValue += (it.second.m_price * it.second.m_quantity);
â user164525
Mar 23 at 21:40
I have rolled back the last edit. Please see What to do when someone answers. Your old version is still available here if you need to access it.
â Zeta
Mar 24 at 23:09
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I would like to improve my skill on C++ concepts and I came across the following question in StackOverflow, which has been directly asked by a StackOverflow-user without any preliminary attempts.(unfortunately, I have only the image of the question, since the actual post was an unsolved question, it has been removed from the site by the user itself)
Any tips or comments to optimize or improve the code and the correctness issues of the solution would be most welcome.
.
#include <iostream>
#include <string>
#include <unordered_map>
/** Class which has been asked in the question. In short Iteam class will act like
a struct for iteamList class, since I have declared iteamList class as a friend
of Iteam class **/
class iteamList; // forward delaration of iteamList class
class Iteam
private:
int m_code;
int m_price;
int m_quantity;
public:
Iteam() // Defualt
:m_code(0), m_price(0), m_quantity(0)
Iteam(const int& code, const int& price, const int& quntity)// Parameterized
:m_code(code), m_price(price), m_quantity(quntity)
Iteam(const Iteam& rhs) // Copy
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
Iteam(Iteam&& rhs) // Move
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
~Iteam()
//(i) input the data members
inline void inputData()
std::cout<<"Iteam code :"; std::cin>>m_code;
std::cout<<"Iteam price :"; std::cin>>m_price;
std::cout<<"Iteam quantity :"; std::cin>>m_quantity;
// life would be easy after making this statement :) !
friend iteamList;
//(ii) to display the data members
friend std::ostream& operator<<(std::ostream& output, const Iteam& obj);
;
std::ostream& operator<<(std::ostream& output, const Iteam& obj)
return output<<"Iteam code = "<<obj.m_code
<<"t price = "<<obj.m_price
<<"t quantity = "<<obj.m_quantity<<"n";
/******************************************************************************/
/** Implementation of the main class which is independent of the above one.
It can access all members of above class if it has instantiated an object of it.
I have used STL std::unordered_map to store the Items which will be easier to
look up(with an O(1)) at the time of queries of deletion.**/
class iteamList
private:
int m_totalStockValue = 0;
std::unordered_map<int, Iteam> m_iteamMap;
private:
// (iii) appending iteam to the list
inline void AppendIteams(const int& userInput_n)
for(auto i=0; i<userInput_n; ++i)
Iteam obj;
obj.inputData();
std::cout << obj << std::endl;
m_iteamMap.emplace(obj.m_code, obj);
public:
iteamList() = default;
iteamList(const int& userInput_n)
m_iteamMap.reserve(userInput_n);
AppendIteams(userInput_n);
~iteamList()
// (iv) delete the iteam from the list
void deleteIteam(const int& code)
if(m_iteamMap.find(code) != m_iteamMap.cend())
std::cout << m_iteamMap[code] << " has been deleted!n";
m_iteamMap.erase(m_iteamMap.find(code));
else
std::cout <<"Invalid Iteam code!" << std::endl;
// (v) total value of the stock
void claculateStockValue()
m_totalStockValue = 0;
for(const auto &it:m_iteamMap)
m_totalStockValue += (it.second.m_price * it.second.m_quantity);
std::cout <<m_totalStockValue<< std::endl;
;
/**********************************************************************************/
int main()
int number;
std::cout <<"Enter total number of iteams : ";
std::cin>>number;
iteamList obj(number);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
int code;
std::cout <<"Enter code to delete the iteam : ";
std::cin>>code;
obj.deleteIteam(code);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
return 0;
c++ object-oriented c++14 overloading
I would like to improve my skill on C++ concepts and I came across the following question in StackOverflow, which has been directly asked by a StackOverflow-user without any preliminary attempts.(unfortunately, I have only the image of the question, since the actual post was an unsolved question, it has been removed from the site by the user itself)
Any tips or comments to optimize or improve the code and the correctness issues of the solution would be most welcome.
.
#include <iostream>
#include <string>
#include <unordered_map>
/** Class which has been asked in the question. In short Iteam class will act like
a struct for iteamList class, since I have declared iteamList class as a friend
of Iteam class **/
class iteamList; // forward delaration of iteamList class
class Iteam
private:
int m_code;
int m_price;
int m_quantity;
public:
Iteam() // Defualt
:m_code(0), m_price(0), m_quantity(0)
Iteam(const int& code, const int& price, const int& quntity)// Parameterized
:m_code(code), m_price(price), m_quantity(quntity)
Iteam(const Iteam& rhs) // Copy
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
Iteam(Iteam&& rhs) // Move
:m_code(rhs.m_code), m_price(rhs.m_price), m_quantity(rhs.m_quantity)
~Iteam()
//(i) input the data members
inline void inputData()
std::cout<<"Iteam code :"; std::cin>>m_code;
std::cout<<"Iteam price :"; std::cin>>m_price;
std::cout<<"Iteam quantity :"; std::cin>>m_quantity;
// life would be easy after making this statement :) !
friend iteamList;
//(ii) to display the data members
friend std::ostream& operator<<(std::ostream& output, const Iteam& obj);
;
std::ostream& operator<<(std::ostream& output, const Iteam& obj)
return output<<"Iteam code = "<<obj.m_code
<<"t price = "<<obj.m_price
<<"t quantity = "<<obj.m_quantity<<"n";
/******************************************************************************/
/** Implementation of the main class which is independent of the above one.
It can access all members of above class if it has instantiated an object of it.
I have used STL std::unordered_map to store the Items which will be easier to
look up(with an O(1)) at the time of queries of deletion.**/
class iteamList
private:
int m_totalStockValue = 0;
std::unordered_map<int, Iteam> m_iteamMap;
private:
// (iii) appending iteam to the list
inline void AppendIteams(const int& userInput_n)
for(auto i=0; i<userInput_n; ++i)
Iteam obj;
obj.inputData();
std::cout << obj << std::endl;
m_iteamMap.emplace(obj.m_code, obj);
public:
iteamList() = default;
iteamList(const int& userInput_n)
m_iteamMap.reserve(userInput_n);
AppendIteams(userInput_n);
~iteamList()
// (iv) delete the iteam from the list
void deleteIteam(const int& code)
if(m_iteamMap.find(code) != m_iteamMap.cend())
std::cout << m_iteamMap[code] << " has been deleted!n";
m_iteamMap.erase(m_iteamMap.find(code));
else
std::cout <<"Invalid Iteam code!" << std::endl;
// (v) total value of the stock
void claculateStockValue()
m_totalStockValue = 0;
for(const auto &it:m_iteamMap)
m_totalStockValue += (it.second.m_price * it.second.m_quantity);
std::cout <<m_totalStockValue<< std::endl;
;
/**********************************************************************************/
int main()
int number;
std::cout <<"Enter total number of iteams : ";
std::cin>>number;
iteamList obj(number);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
int code;
std::cout <<"Enter code to delete the iteam : ";
std::cin>>code;
obj.deleteIteam(code);
std::cout <<"Total Value of stock = ";
obj.claculateStockValue();
return 0;
c++ object-oriented c++14 overloading
edited Mar 24 at 23:09
Zeta
14.3k23267
14.3k23267
asked Mar 23 at 21:08
user164525
Your code has a bug. You're supposed to multiply the quantity and the price when computing the total value.
â Snowbody
Mar 23 at 21:35
@Snowbody Thanks for pointing out this. m_totalStockValue += (it.second.m_price * it.second.m_quantity);
â user164525
Mar 23 at 21:40
I have rolled back the last edit. Please see What to do when someone answers. Your old version is still available here if you need to access it.
â Zeta
Mar 24 at 23:09
add a comment |Â
Your code has a bug. You're supposed to multiply the quantity and the price when computing the total value.
â Snowbody
Mar 23 at 21:35
@Snowbody Thanks for pointing out this. m_totalStockValue += (it.second.m_price * it.second.m_quantity);
â user164525
Mar 23 at 21:40
I have rolled back the last edit. Please see What to do when someone answers. Your old version is still available here if you need to access it.
â Zeta
Mar 24 at 23:09
Your code has a bug. You're supposed to multiply the quantity and the price when computing the total value.
â Snowbody
Mar 23 at 21:35
Your code has a bug. You're supposed to multiply the quantity and the price when computing the total value.
â Snowbody
Mar 23 at 21:35
@Snowbody Thanks for pointing out this. m_totalStockValue += (it.second.m_price * it.second.m_quantity);
â user164525
Mar 23 at 21:40
@Snowbody Thanks for pointing out this. m_totalStockValue += (it.second.m_price * it.second.m_quantity);
â user164525
Mar 23 at 21:40
I have rolled back the last edit. Please see What to do when someone answers. Your old version is still available here if you need to access it.
â Zeta
Mar 24 at 23:09
I have rolled back the last edit. Please see What to do when someone answers. Your old version is still available here if you need to access it.
â Zeta
Mar 24 at 23:09
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
- First off all: Please fix your spelling. The functionality you are trying to implement seems to be about items, so your classes should be called
Item
andItemList
, respectively. Also, there is at least one place in your code where you misspelled "quantity" as "quntity".claculateStockValue
should becalculateStockValue
. I know that it's hard for a non-native speaker to get such things right (I'm not a native speaker myself, so I understand your struggle). However, to make your code clean and easy to read, it is important to keep a certain level of cleanliness throughout naming and comment writing. - Speaking about name cleanliness, keep your capitalization consistent. Why does
iteamList
start with a lowercase i whileIteam
starts with an uppercase? Decide on one naming scheme and stick with it. The same applies to your method names. - Instead of defining a default constructor for
Iteam
that does nothing special, assign default member initializers and= default
that constructor (just as you did in your other class). - You do not need to define a copy and move constructor in
Iteam
. The compiler is nice enough to generate them for you automatically in most cases. You only need to define them yourself if your class does some fancy resource managing, or something alike. The same is true for the destructor. - Don't take
int
as a const reference. Take it by value instead. I don't know of a single architecture out there where andint
could not be passed efficiently in a register (or on the stack). - You do not need to forward declare
iteamList
. Instead, you can just turnfriend iteamList;
intofriend class iteamList;
. - Actually, this is a case of
friend
-declaration abuse. Instead of makingiteamList
a friend ofIteam
,Iteam
should define an actual public interface (i.e. getters and setters). As of now, the wholeIteam
class is somewhat useless. - Don't use
inline
unless you know exactly what you are doing. In most cases, the compiler is going to ignore it anyway. - Don't define
inputData
as a member ofIteam
; it violates the single responsibility principle. Reading and parsing data is not part of the responsibility ofIteam
. iteamList
manages it's content using a...std::unordered_map
?! Hopefully, you realize that this is a blatant mismatch between name and functionality: When I see a class that has "list" as part of its name, I expect it to use some kind of list to store its content.- That being said,
std::unordered_map
does not seem like a very good fit here. Indeed, a list would suggest itself very nicely if you want to keep the O(1) deletion and insertion property. Also, the identifier is already part of theIteam
s; it is redundant to add it as a key to a map. If you want to keep the map, you should extract the key from theIteam
class. - Again, methods such as
AppendIteams
violate the single responsibility principle and must be extracted from your class. The responsibility ofiteamList
is managing a list of items; reading in and creating items is the responsibility of a different piece of code. - Furthermore, don't output debug messages from your class directly. This, again, violates the SRP.
- Keep spacing around operators consistent, especially around
<<
and>>
. In general, it is considered good practice to leave a space before and after all operators. - As with
Iteam
,iteamList
severely lacks a public interface. Please add getter and setter methods in exchange for all those IO-methods. - Don't keep
m_totalStockValue
around as a member. Make it local toclaculateStockValue
instead and return it.
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
- First off all: Please fix your spelling. The functionality you are trying to implement seems to be about items, so your classes should be called
Item
andItemList
, respectively. Also, there is at least one place in your code where you misspelled "quantity" as "quntity".claculateStockValue
should becalculateStockValue
. I know that it's hard for a non-native speaker to get such things right (I'm not a native speaker myself, so I understand your struggle). However, to make your code clean and easy to read, it is important to keep a certain level of cleanliness throughout naming and comment writing. - Speaking about name cleanliness, keep your capitalization consistent. Why does
iteamList
start with a lowercase i whileIteam
starts with an uppercase? Decide on one naming scheme and stick with it. The same applies to your method names. - Instead of defining a default constructor for
Iteam
that does nothing special, assign default member initializers and= default
that constructor (just as you did in your other class). - You do not need to define a copy and move constructor in
Iteam
. The compiler is nice enough to generate them for you automatically in most cases. You only need to define them yourself if your class does some fancy resource managing, or something alike. The same is true for the destructor. - Don't take
int
as a const reference. Take it by value instead. I don't know of a single architecture out there where andint
could not be passed efficiently in a register (or on the stack). - You do not need to forward declare
iteamList
. Instead, you can just turnfriend iteamList;
intofriend class iteamList;
. - Actually, this is a case of
friend
-declaration abuse. Instead of makingiteamList
a friend ofIteam
,Iteam
should define an actual public interface (i.e. getters and setters). As of now, the wholeIteam
class is somewhat useless. - Don't use
inline
unless you know exactly what you are doing. In most cases, the compiler is going to ignore it anyway. - Don't define
inputData
as a member ofIteam
; it violates the single responsibility principle. Reading and parsing data is not part of the responsibility ofIteam
. iteamList
manages it's content using a...std::unordered_map
?! Hopefully, you realize that this is a blatant mismatch between name and functionality: When I see a class that has "list" as part of its name, I expect it to use some kind of list to store its content.- That being said,
std::unordered_map
does not seem like a very good fit here. Indeed, a list would suggest itself very nicely if you want to keep the O(1) deletion and insertion property. Also, the identifier is already part of theIteam
s; it is redundant to add it as a key to a map. If you want to keep the map, you should extract the key from theIteam
class. - Again, methods such as
AppendIteams
violate the single responsibility principle and must be extracted from your class. The responsibility ofiteamList
is managing a list of items; reading in and creating items is the responsibility of a different piece of code. - Furthermore, don't output debug messages from your class directly. This, again, violates the SRP.
- Keep spacing around operators consistent, especially around
<<
and>>
. In general, it is considered good practice to leave a space before and after all operators. - As with
Iteam
,iteamList
severely lacks a public interface. Please add getter and setter methods in exchange for all those IO-methods. - Don't keep
m_totalStockValue
around as a member. Make it local toclaculateStockValue
instead and return it.
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
 |Â
show 2 more comments
up vote
8
down vote
accepted
- First off all: Please fix your spelling. The functionality you are trying to implement seems to be about items, so your classes should be called
Item
andItemList
, respectively. Also, there is at least one place in your code where you misspelled "quantity" as "quntity".claculateStockValue
should becalculateStockValue
. I know that it's hard for a non-native speaker to get such things right (I'm not a native speaker myself, so I understand your struggle). However, to make your code clean and easy to read, it is important to keep a certain level of cleanliness throughout naming and comment writing. - Speaking about name cleanliness, keep your capitalization consistent. Why does
iteamList
start with a lowercase i whileIteam
starts with an uppercase? Decide on one naming scheme and stick with it. The same applies to your method names. - Instead of defining a default constructor for
Iteam
that does nothing special, assign default member initializers and= default
that constructor (just as you did in your other class). - You do not need to define a copy and move constructor in
Iteam
. The compiler is nice enough to generate them for you automatically in most cases. You only need to define them yourself if your class does some fancy resource managing, or something alike. The same is true for the destructor. - Don't take
int
as a const reference. Take it by value instead. I don't know of a single architecture out there where andint
could not be passed efficiently in a register (or on the stack). - You do not need to forward declare
iteamList
. Instead, you can just turnfriend iteamList;
intofriend class iteamList;
. - Actually, this is a case of
friend
-declaration abuse. Instead of makingiteamList
a friend ofIteam
,Iteam
should define an actual public interface (i.e. getters and setters). As of now, the wholeIteam
class is somewhat useless. - Don't use
inline
unless you know exactly what you are doing. In most cases, the compiler is going to ignore it anyway. - Don't define
inputData
as a member ofIteam
; it violates the single responsibility principle. Reading and parsing data is not part of the responsibility ofIteam
. iteamList
manages it's content using a...std::unordered_map
?! Hopefully, you realize that this is a blatant mismatch between name and functionality: When I see a class that has "list" as part of its name, I expect it to use some kind of list to store its content.- That being said,
std::unordered_map
does not seem like a very good fit here. Indeed, a list would suggest itself very nicely if you want to keep the O(1) deletion and insertion property. Also, the identifier is already part of theIteam
s; it is redundant to add it as a key to a map. If you want to keep the map, you should extract the key from theIteam
class. - Again, methods such as
AppendIteams
violate the single responsibility principle and must be extracted from your class. The responsibility ofiteamList
is managing a list of items; reading in and creating items is the responsibility of a different piece of code. - Furthermore, don't output debug messages from your class directly. This, again, violates the SRP.
- Keep spacing around operators consistent, especially around
<<
and>>
. In general, it is considered good practice to leave a space before and after all operators. - As with
Iteam
,iteamList
severely lacks a public interface. Please add getter and setter methods in exchange for all those IO-methods. - Don't keep
m_totalStockValue
around as a member. Make it local toclaculateStockValue
instead and return it.
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
 |Â
show 2 more comments
up vote
8
down vote
accepted
up vote
8
down vote
accepted
- First off all: Please fix your spelling. The functionality you are trying to implement seems to be about items, so your classes should be called
Item
andItemList
, respectively. Also, there is at least one place in your code where you misspelled "quantity" as "quntity".claculateStockValue
should becalculateStockValue
. I know that it's hard for a non-native speaker to get such things right (I'm not a native speaker myself, so I understand your struggle). However, to make your code clean and easy to read, it is important to keep a certain level of cleanliness throughout naming and comment writing. - Speaking about name cleanliness, keep your capitalization consistent. Why does
iteamList
start with a lowercase i whileIteam
starts with an uppercase? Decide on one naming scheme and stick with it. The same applies to your method names. - Instead of defining a default constructor for
Iteam
that does nothing special, assign default member initializers and= default
that constructor (just as you did in your other class). - You do not need to define a copy and move constructor in
Iteam
. The compiler is nice enough to generate them for you automatically in most cases. You only need to define them yourself if your class does some fancy resource managing, or something alike. The same is true for the destructor. - Don't take
int
as a const reference. Take it by value instead. I don't know of a single architecture out there where andint
could not be passed efficiently in a register (or on the stack). - You do not need to forward declare
iteamList
. Instead, you can just turnfriend iteamList;
intofriend class iteamList;
. - Actually, this is a case of
friend
-declaration abuse. Instead of makingiteamList
a friend ofIteam
,Iteam
should define an actual public interface (i.e. getters and setters). As of now, the wholeIteam
class is somewhat useless. - Don't use
inline
unless you know exactly what you are doing. In most cases, the compiler is going to ignore it anyway. - Don't define
inputData
as a member ofIteam
; it violates the single responsibility principle. Reading and parsing data is not part of the responsibility ofIteam
. iteamList
manages it's content using a...std::unordered_map
?! Hopefully, you realize that this is a blatant mismatch between name and functionality: When I see a class that has "list" as part of its name, I expect it to use some kind of list to store its content.- That being said,
std::unordered_map
does not seem like a very good fit here. Indeed, a list would suggest itself very nicely if you want to keep the O(1) deletion and insertion property. Also, the identifier is already part of theIteam
s; it is redundant to add it as a key to a map. If you want to keep the map, you should extract the key from theIteam
class. - Again, methods such as
AppendIteams
violate the single responsibility principle and must be extracted from your class. The responsibility ofiteamList
is managing a list of items; reading in and creating items is the responsibility of a different piece of code. - Furthermore, don't output debug messages from your class directly. This, again, violates the SRP.
- Keep spacing around operators consistent, especially around
<<
and>>
. In general, it is considered good practice to leave a space before and after all operators. - As with
Iteam
,iteamList
severely lacks a public interface. Please add getter and setter methods in exchange for all those IO-methods. - Don't keep
m_totalStockValue
around as a member. Make it local toclaculateStockValue
instead and return it.
- First off all: Please fix your spelling. The functionality you are trying to implement seems to be about items, so your classes should be called
Item
andItemList
, respectively. Also, there is at least one place in your code where you misspelled "quantity" as "quntity".claculateStockValue
should becalculateStockValue
. I know that it's hard for a non-native speaker to get such things right (I'm not a native speaker myself, so I understand your struggle). However, to make your code clean and easy to read, it is important to keep a certain level of cleanliness throughout naming and comment writing. - Speaking about name cleanliness, keep your capitalization consistent. Why does
iteamList
start with a lowercase i whileIteam
starts with an uppercase? Decide on one naming scheme and stick with it. The same applies to your method names. - Instead of defining a default constructor for
Iteam
that does nothing special, assign default member initializers and= default
that constructor (just as you did in your other class). - You do not need to define a copy and move constructor in
Iteam
. The compiler is nice enough to generate them for you automatically in most cases. You only need to define them yourself if your class does some fancy resource managing, or something alike. The same is true for the destructor. - Don't take
int
as a const reference. Take it by value instead. I don't know of a single architecture out there where andint
could not be passed efficiently in a register (or on the stack). - You do not need to forward declare
iteamList
. Instead, you can just turnfriend iteamList;
intofriend class iteamList;
. - Actually, this is a case of
friend
-declaration abuse. Instead of makingiteamList
a friend ofIteam
,Iteam
should define an actual public interface (i.e. getters and setters). As of now, the wholeIteam
class is somewhat useless. - Don't use
inline
unless you know exactly what you are doing. In most cases, the compiler is going to ignore it anyway. - Don't define
inputData
as a member ofIteam
; it violates the single responsibility principle. Reading and parsing data is not part of the responsibility ofIteam
. iteamList
manages it's content using a...std::unordered_map
?! Hopefully, you realize that this is a blatant mismatch between name and functionality: When I see a class that has "list" as part of its name, I expect it to use some kind of list to store its content.- That being said,
std::unordered_map
does not seem like a very good fit here. Indeed, a list would suggest itself very nicely if you want to keep the O(1) deletion and insertion property. Also, the identifier is already part of theIteam
s; it is redundant to add it as a key to a map. If you want to keep the map, you should extract the key from theIteam
class. - Again, methods such as
AppendIteams
violate the single responsibility principle and must be extracted from your class. The responsibility ofiteamList
is managing a list of items; reading in and creating items is the responsibility of a different piece of code. - Furthermore, don't output debug messages from your class directly. This, again, violates the SRP.
- Keep spacing around operators consistent, especially around
<<
and>>
. In general, it is considered good practice to leave a space before and after all operators. - As with
Iteam
,iteamList
severely lacks a public interface. Please add getter and setter methods in exchange for all those IO-methods. - Don't keep
m_totalStockValue
around as a member. Make it local toclaculateStockValue
instead and return it.
answered Mar 23 at 22:41
Ben Steffan
4,85011234
4,85011234
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
 |Â
show 2 more comments
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Thanks for your detailed review. Definitely, I will keep all this in mind next time.
â user164525
Mar 23 at 23:20
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
Könntest du über meine neue Implementierung kommentieren?ideone.com/UonX7I
â user164525
Mar 24 at 23:11
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
or here: codereview.stackexchange.com/revisions/190335/4
â user164525
Mar 24 at 23:17
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
@NiceBuddy Sure, but please post it a follow-up question.
â Ben Steffan
Mar 24 at 23:35
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
here is the new one: codereview.stackexchange.com/questions/190413/â¦
â user164525
Mar 25 at 1:02
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190335%2fclass-iteam-answer-to-one-of-the-unsolved-stack-overflow-qes%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Your code has a bug. You're supposed to multiply the quantity and the price when computing the total value.
â Snowbody
Mar 23 at 21:35
@Snowbody Thanks for pointing out this. m_totalStockValue += (it.second.m_price * it.second.m_quantity);
â user164525
Mar 23 at 21:40
I have rolled back the last edit. Please see What to do when someone answers. Your old version is still available here if you need to access it.
â Zeta
Mar 24 at 23:09