PrevUpHomeNext

day-05: cpp class attribute


cpp class attribute

cpp/c++ class attributes

class attributes

A c++ class attribute is a variable that belongs to that class. Attribute can be simply thought as feature . But why is a variable of a class called attribute ? You can compare attribute with method to understand it.

class chat_robot
{
public:
	std::string name = "chat_robot";
public:
	void speak()
	{
		std::cout << "Please talk to me, I am a chat robot!" << std::endl;
	}
};

The class chat_robot has an attribute: name ;

and it has a method: speak .

Now think of it:

I think you understand why it is called attribute now.

How to change the class attribute value, -> setter

You can add another method to change the attribute value of the class:

public:
	void set_name(std::string new_name)
	{
		name = new_name;
	}

You see that a method can have input arguments, and the input is assigned to attribute:

name = new_name;

How to fetch the attribute value from the class, -> getter

You can add another method to get the attribute value of the class:

public:
	std::string get_name()
	{
		return name;
	}

You see that the attribute value is returned by return statement:

return name;

Full example: put all of them together

Save as chat_robot.cpp

#include <iostream>
#include <string>

// cpp/c++ code

class chat_robot
{
public:
	std::string name = "chat_robot";
public:
	void speak()
	{
		std::cout << "Please talk to me, I am a chat robot!" << std::endl;
	}
	void set_name(std::string new_name)
	{
		name = new_name;
	}
	std::string get_name()
	{
		return name;
	}
};

int main()
{
	auto robot_no_1 = chat_robot{};
	robot_no_1.speak();

	std::cout << "name: " << robot_no_1.get_name() << std::endl;

	robot_no_1.set_name("robot");
	std::cout << "name: " << robot_no_1.get_name() << std::endl;
}

Compile and run:

$ g++ chat-robot.cpp -std=c++23 -o chat-robot
$ ./chat-robot
Please talk to me, I am a chat robot!
name: chat_robot
name: robot

Last

This article explained the attributes of cpp class, it has only three points, no stress:

Written on Aug 04, 2024

Back to index

Index


PrevUpHomeNext

E

U