adplus-dvertising

C++ Class Member Functions

C++ Class Member Functions

Like any other variable, a member function of a class operates on any object of the class to which it belongs and has access to all members of the class for that object.

In order to access the class members using a member function, let us look at the previously defined class:

Example
#include <iostream>
using namespace std;

class Car{
    private:
        int Id;
        int NB_wheels;
        double speed;
        int mileage;
    public:
        Car(int myid, int mywheels, double myspeed, int mymile){
            Id = myid;
            NB_wheels = mywheels;
            speed = myspeed;
            mileage = mymile;
        }
        void ride(){
            cout << "Car " << Id << " is on its way with a speed of " << speed << endl;
        }
        void accelerate();
};

int main()
{
    Car c1(1, 4, 120, 150000);
    c1.ride();
    return 0;
}
Output
Car 1 is on its way with a speed of 120

A class definition may contain member functions, or they may be defined separately using the scope resolution operator "::".

The following syntax can be used to define the function outside of the class:

Syntax
return_type className::function_name( parameter_list ) {
    // function body
}
Example

The following code defines the function "accelerate()".

void Car::accelerate(double v){
    speed += v;
}

The standard practice when using classes is to separate the class declaration from the definition. As a result, the class declaration will be contained in a .h file, while the definition will be contained in a .cpp file.

Car.h
class Car{
    private:
        int Id;
        int NB_wheels;
        double speed;
        int mileage;
    public:
        Car(int, int, double, int);
        void ride();
        void accelerate(double);
};
Car.cpp
#include <iostream>
#include "Car.h"

using namespace std;

Car::Car(int myid, int mywheels, double myspeed, int mymile){
            Id = myid;
            NB_wheels = mywheels;
            speed = myspeed;
            mileage = mymile;
        }
void Car::ride(){
            cout << "Car " << Id << " is on its way with a speed of " << speed << endl;
        }
void Car::accelerate(double v){
    speed += v;
}

Pointer this

In C++, every object has access to its own address via a crucial pointer called the "this" pointer. The "this" pointer is an implicit parameter for every member function, so it can be used to refer to the object in the calling function.

In the following situations, the "this" pointer is used:

  •   When the local variable's name is the same as the member's name:
    class Point{
        private:
            int x,y;
        public:
            Point(int x, int y){
                this->x = x;
                this->y = y;
            }
    };
  •   It is used to return a reference to the object that was called.
    class Point{
        private:
            int x,y;
        public:
            Point(int x, int y){
                this->x = x;
                this->y = y;
            }
            void show(){
                cout<< x << endl;
            }
     
            // return a reference to the calling object
            Point& itsme(){
                return *this;
            }
    };
Partager ce cours avec tes amis :
Rédigé par ESSADDOUKI Mostafa
ESSADDOUKI
The education of the 21st century opens up opportunities to not merely teach, but to coach, mentor, nurture and inspire.