class in c

Posted under C++ Course On By mohammed.mutawe

Class in c

Sometimes you may need to write the classes or objects on one page and perform the execution on the functions externally when needed. Is this possible?

Yes, it is possible. One of the characteristics of functions is that they may be implemented in the Declaration and Definition method. In this lesson, we will put a code for the classes we have in a specific place, and then implement the functions outside the boundaries of the object by direct contact with the object in the C++ language.

Reasons for postponing

Sometimes we need some organization within our code, and the programming solutions are many, and we are currently with a powerful programming language that allows us to many solutions, so it was easy for programmers to make the classes pages in a separate header and call the functions of the objects and implement them in another place, for example, the main.

In this lesson, we will make an object and call it on one page, which is the main to facilitate understanding and understanding.

 

Make a class(object)

Copy this class above the main like this.

class calculate {
private:
    int x = 10;
    int y = 15;
    int res;

public:

    void process();
    int getResult();

};

 

Note here that we made a class here called calculate and we gave it internal values ​​for the variables x, y, and the variable res will return the value, don’t have to explain it, things will be fine in the final code.

 

Calling class functions outside

Copy and perform the calculations for these functions above the main and below the object you copied earlier.

void calculate::process()
{
    res = x + y;
}

int calculate::getResult()
{
    return res;
}

 

 

Note here that we have called the functions of the previous class. In the calculate function, we collected the value of x,y and put it in a variable called res, which we will call in the main, and we processed the second function of type integer to return the value of res that we talked about now.

 

Create object in main

We come now to the last stage, which is the connection to the object that we built, so that the main code becomes as follows:

int main() {
    calculate test;

    test.process();
    int val = test.getResult();

    std::cout << "The Result Is : " << val << endl;

    return 0;
}

In the first line, we defined the object, which is calculate, then we named it test, and we called the process function, and since the getResult function is a regressive function, we received its value inside a variable that we defined in the main, which is val of type val as the function type Exactly so that the value of the variable appears in the console screen.

 

Example

Please, to understand the classes well, copy the following code into your code editor.

#include<iostream>
using namespace std;



class calculate {
private:
    int x = 10;
    int y = 15;
    int res;

public:

    void process();
    int getResult();

};


void calculate::process()
{
    res = x + y;
}

int calculate::getResult()
{
    return res;
}


int main() {
    calculate test;

    test.process();
    int val = test.getResult();

    std::cout << "The Result Is : " << val << endl;

    return 0;
}

 

 

Leave a comment

Your email address will not be published. Required fields are marked *