# OOP classes



## GSquadron (May 20, 2012)

Hi all!
I am having a hard time understanding classes with the OOP.
At least the book, doesn't explain them well to me!
Ok so here we start with a simple header file:
stock.h

```
// stock00.h -- Stock class interface
// version 00
#ifndef STOCK00_H_
#define STOCK00_H_
#include <string>
class Stock // class declaration
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
void acquire(const std::string & co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
}; // note semicolon at the end
#endif
```

How am i supposed to use these data types and functions in a stock.cpp file so i can access them in the main.cpp file?
This is what i am not really getting
I do understand what is the difference between the classes and structs
Pls help!


----------



## GSquadron (May 20, 2012)

Edit: Fortunately i found it out how to use it!
If you have smth like this:

```
[COLOR="darkgreen"]public:
stock();
void manage();
void update();[/COLOR]
```
You can use them on the stock.cpp file like this:
void stock::manage()
and you can access as much as you want now on main.cpp!!!!
This was one of the toughest things i have ever learned on cpp and hope you understand too


----------



## Aquinus (May 20, 2012)

I don't think I would consider C++ a good OO language. I tend to stay away from C++, and when I really need that level of control I will just use C.


----------



## GSquadron (May 20, 2012)

Why you don't think man?
C is a good language and C++ is made for people who created millions of lines on C and from their
experience C++ is what was needed, so there is no reason why you would like C
(unless you know it better!)


----------



## Kreij (May 20, 2012)

Learning how classes work and understand their purpose is two different things.
Anyone learning OOP *must* have a good understanding of not only how to do things, but why. Without that knowledge the code will be a mess.
Questions like, "When should I use a class vs. a struct", and , "Do I want my class to be instantiated, or would a static class be better for it's functionality" are basic OOP fundamentals.
Do I want my class to be public, private, protected, static, abstract, internal, etc.
How accessable do I want the properties and methods of my class to be?

You have to get a solid understanding of the basics first. Then code.


----------



## GSquadron (May 20, 2012)

So the basics are?

I understand things when i start making them and only when i know what i am doing,
so i think this is when i will understand it what oop is, but yet, i have to learn for like
2 years, at least. I am learning web development too in the meantime so i have no time
to waste with C++. I will leave it for later, unfortunately!


----------



## Kreij (May 20, 2012)

Aleksander Dishnica said:


> I am learning web development too in the meantime so i have no time
> to waste with C++



Then there is not much point in continuing this thread.


----------

