The point to using classes

I'm making a program that has a lot of functions, but my teacher told me to use a class. Why do I need a class?
C++ focuses on object oriented programming. Classes are part of OOP. They help organize your data and what functions that the objects should be able to call
I am bad at explaining this because I tend to get too complex to quick. C++ is an OOP language which allows you to take abstractions of entities or things known as objects. When you create a class you are actually defining something in your program world and you can give data members and functions. This is known as a type. Good example would be like describing cats in your program. First you would define everything all your cats will have. We will say they will have a name, number of legs, and will have the ability to meow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Cat
{
     string _name;
     int legs = 4;

     void setName(string n)
      {
            _name = n;  //this will set the name for your cat, since name is global
       }

     void meow()
     {
           std::cout << "Meow, my name is " << name << "." << std::endl;
      }
}


Now that the class is or type is defined, in your main you can create an instance of this class and they can each have their own name like so...

Cat myCat; Cat yourCat;

Now you can set the names for these cats using the . to access the methods in the class

myCat.setName("Peter") yourCat.setName("Piper")

Now you have two objects that are both cats but different names, when asked to meow(by calling the function meow on your object) it will print out meow with the name belonging to the object.

The class code the way I had did will probably not work since you have to set things up a little differently in C++, it was more to express this idea.

Hope this made sense and if I didn't just hang in there and explore other options to learning it. My goal is just to not scare you away!

erock

@erock 88

Thank you so much! I get it now. I know how to use them, but can't you just put everything in your int main? Or is it a way to organize your program, and it doesn't matter if you have it or not?
Last edited on
yes, you can totally put everything in main. It is a way to organize your code and to be object oriented. When you deal with larger applications it will be much more manageable to use class and then create instances(objects) of these classes.

erock
thank you
I want to add I totally can relate and that trying to get why OOD (object oriented design) is important, took me a while as well. It is not something you need to use for a simple program. But it is something that you should learn. It is another tool for solving problems programmatically.

OOD can enable you to write methods/functions before they are implemented.

Functional programing...
Think about how the proverbial Egyptian pyramid is made one brick at a time, starting from the bottom. Then how would you replace a bad brick that was in the middle of the pyramid after you were done, or move the pyramid a few feet to the right? A-lot of heavy lifting. In other words. Functional programing can be beautiful, pretty to look at and seem practical, but if you need to adjust anything it can be a real mess.


Object Orientated Programming...
Can be compared to modern manufacturing. Say a car / vehicle company. Or even a Baking Company.

First you design a base car / treat, given a set of properties.
e.g 4 wheels and a engine. or flour sugar, eggs and water.

Then you can extend this abstraction into a truck and a van, or twinke or a hoho.
adding the different properties. like body, doors or whoop cream, coloring, chocolate (or lack of)

Then you can extend this abstraction into color of cars, vans, different flavors of above. You get the point.

Then lets say you have an issue, type of flour or steel you are receiving to make your base product. You can then adjust that one property and it will extend into all extended (derived classes) items.

Also lets say you hire Gordan Ramsey and he shows you how can make hohos more efficiently. You can adjust how hohos are being made but it will not affect anything else.

And there is a lot more... but just trying to add to what has helped me feel better about OOD/P

It is on one level very simple, on another level it's main purpose is an abstraction tool. It helps you focus on design before implementation. And is much easier to troubleshoot and maintain.
Last edited on
Registered users can post here. Sign in or register to post.