Classes-Wat

I have no idea where to begin with OO. My book spent 3 chapters explaining it, but I retained nothing. And whenever i try to go back and reread it, It still doesn't make sense. Can someone explain classes to me like you would to a 5 year old?
Last edited on
do you have other programming experience?

EDIT: and what exactly about classes do you not understand?

inheritance, constructors, destructors, etc. . . ?
Last edited on
For my experience,
Class is like, Representing things from the real world ...

Maybe like representing a person with his property

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Person {
public:

Person(){}
Person( const string& _name, int _age ){ age = _age; name = _name;}

int get_age( return age; }
const string& get_name(){ return name; }

private:
int age;
string name;


};


The main point in class is to hide the many detail in your program and simplified it IMO.
I'll start from the beginning of what is in a class. A class is a collection of variables and functions that a person uses in their program. The purpose of a function is designed to allow a user to put a series of code that are related to an operation. After coding each function you can just call for those functions in the int main() portion of your program.

A simplified example of what you would use classes for is if you wrote a calculator program.

You might make a class that contains 3 functions
1
2
3
4
5
6
7
8
9
10
11
12
13
class calc
{
private: //only accessable by members of its own class 
    float sum;
    int x;
    float mult;
    float sub;
    float avg;
public:
    calc(); //would be where you declare your initial values and have inital input from users.
    void calculations();//this could be where you put your formulas for manipulating the inputs the users does 
   void difference();//this just makes it so I subtract all my other values from my initial input instead of zero
   void outputs();//this could be where you write your code for your outputs and have them all set up  


*note that you do not actually put any of your statements inside the class. Like with the function void calculations(); you do not put your sum+=num1; there. (Hopefully my bad grammar isn't confusing you)

now after the class portion of your code, but before your int main() function you write out the functions of your class.
Here is an example with an old calculator program of mine

1
2
3
4
5
6
calc::calc()
{
    std::cout<<"Please Input Your Values"<<std::endl;
    std::cout<<"Up To 100 Values"<<std::endl;
    std::cout<<"Type 0 When Ready To Calculate"<<std::endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
void calc::calculations()
{
    for (int i=0;i<Size;i++)
    {
     cin>>int x;
        if(x==0)
        {
            break;
        }
    sum+=x;
    void difference();//this will call my difference function into my caluclation function to cut down on coding space
    mult*=x;
    avg = sum/i;


1
2
3
4
5
6
7
8
9
10
11
void calc::difference()
{
    if (i==0)
    {
        sub=x;
    }
    else
    {
        sub-=x;
    }
}


1
2
3
4
5
6
7
8
9
void calc::outputs()
{
    std::cout<<"Sum of the values are: "<<sum<<endl;
    std::cout<<"The Difference of the Values are: "<<sub<<endl;
    std::cout<<"Mult equals: "<<mult<<std::endl;
    std::cout<<"Minimum Value: "<<fmin<<endl;
    std::cout<<"Maxmimum Value: "<<fmax<<endl;
    std::cout<<"The Average is: "<<avg<<endl;
}


after you would do all this your main function would just look like

1
2
3
4
5
6
7
8
9
10
11
int main()
{
  calc.objectName;  //just using objectName as a reference so that code will come out properly
 
  objectName.calculations();

  objectName.outputs();

return 0;
}
  


so all that code used earlier is just condensed down to those 3 simple functions in your main portion =). Try to get the basics of writing classes where your functions don't require references or parameters. After you got that then try to implement those. GL and happy coding!
Last edited on
Classes are like patterns. Say for example you would like to create a rectangle then you define the pattern:

1
2
3
class rectangle {
int length, width;   // to have a rectangle, it must have a length and width
}


You now have a pattern to create a rectangle:

rectangle nameOfRectangle;

Last edited on
First; Object Oriented programming stands for designing software (or games) using objects. These programs use mainly only Objects which interact with each other. Let’s look at a car for example; you have the Motor object, the Transmission object, the Wheel Object, etc. Each of these work together, accessing or using functions from one another.

In a program, you write a class or a struct. These are sort of like blueprints. They are used at run time to create objects based on what the user or other parts of the program needs. Sort of like a factory would make car objects when needed.

The constructor builds the object based on the instructions you give it and the Destructor destroys it.
Hm, I think I get it. I'll try some stuff out when I get home
Topic archived. No new replies allowed.