help using class in program

Hello,

I am working on a project and it seems to work really well but I would like to use a class to write the program. I have watched numerous videos on class and am having some trouble understanding how to convert it to a class. The reason I want to change it is because I have read its bad practice to have everything in your main function and would like to learn how to make more professional looking programs.

Thanks for any help or input.

Ryan

#include <iostream>
using namespace std;

void production(int);

int main()
{
int i, ts1, sd1, production1, sd2, pc2, production2, cc3, ts3, production3, sd4, pc4, ts4, production4;
// Plant 1
cout << "Enter production for plant 1 in thousands" << endl;
cout << "teaspoon dept: ";
cin >> ts1;
cout << "soup spoon dept: ";
cin >> sd1;
production1 = ts1 + sd1;
// Plant 2
cout << "Enter production for plant 2 in thousands" << endl;
cout << "soup spoon dept: ";
cin >> sd2;
cout << "plain cocktail spoon dept: ";
cin >> pc2;
production2 = sd2 + pc2;
// Plant 3
cout << "Enter production for plant 3 in thousands" << endl;
cout << "colored cocktail spoon dept: ";
cin >> cc3;
cout << "teaspoon dept: ";
cin >> ts3;
production3 = cc3 + ts3;
// Plant 4
cout << "Enter the production for plant 4 in thousands" << endl;
cout << "soup spoon dept: ";
cin >> sd4;
cout << "plain cocktail spoon dept: ";
cin >> pc4;
cout << "teaspoon dept: ";
cin >> ts4;
production4 = sd4 + pc4 + ts4;

cout << "Plant #1 ";
production(production1);
cout << endl << "Plant #2 ";
production(production2);
cout << endl << "Plant #3 ";
production(production3);
cout << endl << "Plant #4 ";
production(production4);

cout << endl << endl;

system("pause");
return 0;

}

void production(int productionNo)
{
int i;
for (i = 0; i < productionNo; i++)
cout << "*";
}
It is indeed bad to have everything inside your main function. If you want to avoid bad programming practice, I also suggest not using system("pause");. The system command is OS dependent. If you gave your code to another person who used a different OS, it probably will not work. The statement using namespace std should be avoided as well. Your int i doesn't seem to be used anywhere in the program, and you should always give variables well-meaning name.
When getting started with classes, you should identify what needs to be a part of that class. Take this class for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

class store
{
    public:
        store(const std::string &);
        void m_calculate() const;
    private:
        std::string m_item;
        float m_itemPrice;
        unsigned int m_itemsSold;
};

/* This is a user defined constructor. This user defined constructors sets a string value so we can
 * identify what item is being sold at this store. It also defines the value of the item being sold
 * and how many items were sold.*/
store::store(const std::string & itemName)
{
    m_item = itemName;
    std::cout << "Value of " << m_item << ": ";
    std::cin >> m_itemPrice;
    std::cout << "Quantity of " << m_item << " sold: ";
    std::cin >> m_itemsSold;
}

void store::m_calculate() const
{
    std::cout << "Amount made from selling " << m_item << " is: " << m_itemsSold*m_itemPrice << std::endl;
}

int main()
{
    store storeObject1("Play-Doh");
    store storeObject2("Puma Hats");
    store storeObject3("TI Calculators");
    store storeObject4("LIFE WTR");

    storeObject1.m_calculate();
    storeObject2.m_calculate();
    storeObject3.m_calculate();
    storeObject4.m_calculate();
    return EXIT_SUCCESS;
}
Hi Ryan,

First up, a tip for future posts: Use code tags - select your code, press the <> button on the format menu. It makes it easier for everyone, there are line numbers, and we can compile the code if a complete program is posted.
http://www.cplusplus.com/articles/z13hAqkS/

rjblair87 wrote:
The reason I want to change it is because I have read its bad practice to have everything in your main function and would like to learn how to make more professional looking programs.


Yes that is right, but there is more than one way of doing it.

The first way is to write code with functions, this what happens in lots of languages, obviously in those which don't have classes at all. Functions should only do 1 conceptual thing, and be quite short, including the main function. One could have a program with more than 1 MLOC (Millions of Lines Of Code) with a main function 10 LOC.

The reason I mention this is that sometimes it's not worth it to put code into classes. Even when there are classes, some of the functions can be free functions (functions outside a class). However that doesn't mean that one gravitates into writing C code all the time.

Next is realize the basic concept of a class: Member variables (the data), and functions which operate on that data, are all bundled together into a class. This generally makes it easier to model real world and abstract situations. Sounds easy enough, but OOP does get complicated when there are multiple classes.

Anyway, there is so much to talk about, better start with a tutorial:

http://www.cplusplus.com/doc/tutorial/classes/

Also I have some personal preferences about the code I write, in the following I don't mean to dis fiji885's style. However I do have reasons why I do these things.

I don't pre-pend member variables with m_ or any other common appends such as an underscore. I prefer to put an Arg on the end of the function parameter identifiers, this separates them from the member variables.

A function declaration doesn't have to have const for the parameters, but do put them in in the function definitions;
I put the & for the reference next to the type, because C++ revolves around types;
I always put a variable name in the function declaration, the same as the one in the definition:


1
2
   public:
        store(const std::string& itemNameArg);


store::store(const std::string& itemNameArg)

I always declare 1 variable per LOC. Ideally wait until you have a sensible value to assign, then do the declaration and assignment all in one line:

int production1 = ts1 + sd1;

Some good reading:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

Last edited on
Topic archived. No new replies allowed.