Many variables or a class ?

Introduction
If you are interested in programming, in the future you will meet something like OOP. What does it mean ? It is Object-Oriented Programming and professional programmers or by people who know more about programming and want to develop own skills use that. During coding big project you must use OOP because it gives us more options to coding more complicated assignment. Basics like loops, functions, arrays will be insufficient so if you are going to apply for a job – know more than basics.

Till now I showed structural programming – basics on the specific projects and if you are still interested in programming – follow this series. I am going to explain you OOP by small projects using Code::Blocks.
First class
In this post I will show a first issue – building classes. Earlier you created variables to describe something.

You could create more variables to describe anything, but… is it professional ? Is it used by programmers at companies ? Single variable – yes, however to write more informations about objects, we used to use classes. Let’s go to practice and create class which will just describe an object:

oop-objectsclasses-10-638

Capital letter in naming classes doesn’t require but many people used to use this. Furthermore you should define accessibility to class. You should write ‘public:‘ or ‘private:‘ or ‘protected:’. If you choose first option, you will have a possiblity to use data from this class in another place of a project. Whereas second option won’t allow to take any data from there. If you don’t define an accessibility to class, it will set default for ‘private’. So let’s add some variables in the class.
Look for naming. I wrote about this a post. Now you create any object of class ‘Car’.

You have already created object ‘Vehicle’ from class ‘Car’ and now you have a possibility to describe this object.
You definied (described) first object. You can display one of feature. Add #include <string> and see:
Can you create next object ? Of course ! Just use class ‘Car’ to describing an object.
You can also create pointers to modifying objects:

What’s happened right now ? By the pointer we have changed a name of ‘Old_car.brand’ and instead see ‘Mercedes’ you saw ‘Renault’. It is possible to changing names by pointers. You have second option to change any feature of object. You can do that by reference. Let’s see how to do that:

Summation
Can you deduce anything from this lesson ? Did you see advantages of classes ? You have a moment, just think….

Okay,  my turn right now:
+ classes are a way to organize your code into generic, reusable way,
+ at their best they are generic blueprints for things that will be used over and over again with little modification,
+ you don’t have to write new hundreds variables.

I think that by this post you will use, practice new knowledge.

#1 More informations about classes
#2 More informations about classes

Leave a comment