What are Classes and Objects in C++

In this tutorial of our C++ course, we will learn in a very colloquial and simple way, what classes and objects are, the basis of object-oriented programming.

What is a Class

Class is nothing more than a blueprint, a mold, a diagram.
For example, let's imagine a class Car.

This class will describe all the details of this car: its size, number of doors, engine power, whether it is manual or automatic, whether it has a sunroof or not, its utilities, etc.

That's basically it, it's a 'form', which explains what the ... objects are like.
To make a car, we need the details of the Car class to create a car. This car is going to be an object.
Yes, to explain class, we have to talk about object ...

What is an Object

... and to talk about Object, we need to talk about Class. It is not possible to separate or explain one of the two things well, something is 'vague', and I hate these technical and vague explanations.

Everything you know is an object. You are an object, you live in an object, you eat objects, you move around objects, you are accessing this page / booklet through an object, in fact, this course is an object.

Everything is an object.

Understand object as the real, day-to-day things that actually exist. And class, like something abstract.

Class and its Objects

A good way to explain something is through real examples. So come on.

Let's say you completed the Progressive C++ course and became a hell of a C++ programmer, with an excellent salary, and decided to buy a car.

You won't come to the dealership and say, "Hey, man, I want a car".
And the salesman won't say, "Okay, here's a car".
Nor will you answer, "Thanks, I have a car now"

When you go to buy a car, you will want a Civic, a Camry, a Corolla or a BMW (if you are a C++ programmer). That is, you will buy something specific. That specific car is an object. An object of the class Car.

The class Car will say: "These objects have x doors, such a engine, transmission, such power, such color ...", that is, the common characteristics that all cars have. All cars have doors and engines, for example.

A Volkswagen Beetle has two doors and a weak engine. Your Audi has 4 doors and a huge power. But both have a certain number of ports and an exact power value.

In other words, a Beetle, a BMW, a Mercedes ... they are all objects of the Car class.

Got it?

Class and objects in C++


Attributes and Functions of Classes and Objects

Let's go to one more example. You don't know or deal with a 'human'. You deal with your father, your mother, your friends ... that is, 'specific' humans.
Let's create the Human class. What are the characteristics of a human? Hey, he has a name, age, height, weight, heart, lung, etc. etc.

We call these attributes details, their details, their information.

This class will also 'do' some things, such as: breathing, beating the heart, walking, sleeping ... that is, the Human class, in addition to having characteristics (values, numbers), it will also have some actions ... these actions are functions.

Specific functions that only exist in the Human class. After all, it does not have a Breathe() function in the Car class, nor does it have a Shifting() function in the Human class.

That is, each Class has its characteristics (attributes) and specific actions that occur there (functions). This is what defines a class: attributes and functions.

The cool thing about object-oriented programming is that the functions of the Car can only act on objects of the Car type, and the actions of the Human class will only be able to act on objects of the Human class.

Instantiate (create) objects from a Class

When you are going to create something, that something was created from a class. That is, we say that the object was instantiated from a class.

For example, suppose you were hired by a company to work in the HR industry. At first, you will create a class called Employee.

What are the characteristics of an Employee? Hey, he has a name, position, salary, identification number ...

When someone new is hired, you need to create a specific object for that new employee. We say that you will instantiate an object of the Employee class.

This object has a name "Neil Peart", age (32), salary ($ 10,000.00/month- after all, it is a C++ programmer), identification number (2112) ...

That is, objects are real things, based on abstract stuff (classes).

From the next tutorial, we will really learn how to program classes, how to instantiate objects, how to define their characteristics and actions.

No comments:

Post a Comment