How to create a Class and Objects in C++

Now that we have a good idea of what the Object-Orientada programming paradigm is, as well as the Class and Object concepts (in a more abstract way), let's get our hands dirty and make code! Create such classes and real objects.

How to create a Class in C ++

Initially, the concept of class encoding is very reminiscent of structs. In this case, we will use the keyword class to create a Class, as follows:

#include <iostream>
using namespace std;

class MyClass
{
    //Informations
    //about your
    //Class
};

int main()
{
    return 0;
}

That is, we write class, choose a name for our class, open keys and put all the information about it inside. Don't forget to put a semicolon at the end of the class scope!

Ready, the MyClass class was created! Also note that it is not within the main() function, but at the same level as it.

Defining members of a Class: Variables and Functions

Shall we create a real class? We will create a class that will create squares. Our class is called Square.

What is an important data of a square? Hey, its side. Soon, it will have a variable that stores its side.

What other important 'thing' in a square? Its area. Let's create a function that calculates the area of that square. Our class looks like this:

#include <iostream>
using namespace std;

class Square
{
    double side;
    double area();
};

double Square::area()
{
    return side*side;
}

int main()
{
    return 0;
}

There, there is the declaration of the variable 'side' as well as the header of the area() function. And this function, where are we going to declare? It was declared after the scope of the class.

So far, in our course, if we wanted to declare an area() function in our programs, we would do:

double area()
{
     return side * side;
}

And this function is visible and accessible to our entire program, correct?

What if we want to create a function that is a Class and should be visible and accessible only by that class?
We do:

double Square::area()
{
    return side * side;
}

Understood? Put "ClassName::" before the function name, that says that function is of the ClassName class, and only it has access and can use that function!

How to create and instantiate objects in C++

Ok, our class is cute and done! Now let's create objects from it!
If you want to create information of the entire type, you do:

int num;
int age;
int ID;

If you want to create string information, you do:
string name;
string address;

And so on. If you want to create information like 'MyClass', you do:

MyClass example;

For example, to create an object of class Square, you would do:

Square q1;
Square square;

Let's call our object 'q', we create it like this:

#include <iostream>
using namespace std;

class Square
{
    double side;
    double area();
};

double Square::area()
{
    return side*side;
}

int main()
{
    Square q;

    return 0;
}

Ready. Now there is a real square, called Square, which has 'side' and 'area()' members. Nice, right?

When we create an object, we say that we are instantiating an object q of class Square. It is a new 'instance' of the class.

You can even create another object, or millions, just give them different names: q1, q2, ..., q2112 ... each one will have its characteristics: its 'side' values and its areas. Although they are of the same Square 'type' (that is, they all have side and area() in common), each has its specific values.

It's just like us, people ... everyone has a name, heart, lung capacity, age ... but each has its own specific values (specific name, specific age, specific physical condition). Now you understand better the concept of Class and Object, in C++?

In the next tutorial we will learn how to access, change and use these members of the Square class.

No comments:

Post a Comment