Hi,
Ok, some concepts ..... :+)
Name your files the same as the Class name. I use .hpp for header files, meaning a header file with cpp code in it.
I Put a leading
C
on the class names, a leading
m_
on member variables and a leading
p
for pointers. This is purely a personal preference, but it works well for me.
So for you classes I would have these files:
CGrocery.hpp
,
CGrocery.cpp
,
CApple.hpp
,
CApple.cpp
and
GroceryApp
(has the
main()
function in it)
Don't include cpp files, only header files.
Investigate using initialiser lists - this means you will not need lots of trivial set functions (a bad idea). Call your base class constructors from this initialiser list, so you don't have object slicing. This means you need to provide constructors for your classes.
This leads to deciding what your interface is going to look like. What functions should you have? Think about what the object can do (verbs)
Don't have protected data members, I know it's tempting - instead make all data members
private:
As it stands, you don't actually need you Apple class. All the data is stored in the base class, there is nothing new in the Apple class. Is there something you can add to your class? A fruit specialisation?
If you want to have a "Database" , you need to have some kind container which holds multiple objects. The simplest is probably a
std::vector
, but there are others that could come in handy - like
std::map
,
std::unordered_set
for example.
You could have a
std::vector
of pointers to
CGrocery
There is a whole subject called Polymorphism which you could read about in the tutorials / Articles section at the top left of this page.
Have a read of this:
You could adapt this idea, so you could add up the sales for Fruit, Veges, Meat, Dairy etc
You could have a
std::vector
of pointers to
CGrocery
that way you can have a container which holds multiple types of objects. Then write functions which take pointers to
CGrocery
.
Hope this all helps, maybe quite a bit for you to think about - hope all goes well :+)