Saturday, April 19, 2025

C++ OO Getters and Setters

In object-oriented programming, getters and setters are methods used to access and modify the private members of a class.

A getter is a method that returns the value of a private member variable, allowing outside code to read it, while a setter is a method that sets the value of a private member variable, allowing outside code to modify it.

Using getters and setters provides a controlled way for outside code to access and modify the internal state of a class. This is useful because it allows the class to enforce certain constraints or perform certain actions when its state is modified. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string someString;

    public:
        void getStr() {
            cout << someString << endl;
        }
        void setStr(string x) {
            someString = x;
        }
};

int main() {

    Cls Object;
    Object.setStr("Slavoj Zizek");

    Object.getStr();
    return 0;
}

This is an example of a class with a setter and getter method. The Cls class has a private member variable called someString, and two public methods to access it. The getStr method returns the value of someString and setStr sets the value of someString to a new value passed as a parameter.

In this specific code, an instance of the Cls class is created, and the setStr method is used to set the value of someString to "Slavoj Zizek". Then, the getStr method is called to print the value of someString to the console, which is "Slavoj Zizek".

Getters and setters are useful in object-oriented programming because they provide a way to access and manipulate the private member variables of a class while maintaining control over how the variables are accessed and changed. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string name;
        string lastname;

    public:
        void getStr() {
            cout << name << endl;
            cout << lastname << endl;
        }
        void setStr(string x, string y) {
            name = x;
            lastname = y;
        }
};

int main() {

    Cls Object;
    Object.setStr("Jordan", "Peterson");

    Object.getStr();
    return 0;
}

This rogram that defines a class Cls with two private member variables name and lastname. The class also has two public member functions getStr() and setStr(string x, string y).

The getStr() function is used to display the values of name and lastname on the console.

The setStr(string x, string y) function is used to set the values of name and lastname to the values passed as arguments to the function.

In the main() function, an object of class Cls is created, and the setStr() function is called to set the values of name and lastname. Then, the getStr() function is called to display the values of name and lastname.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...