Home >Backend Development >C++ >What does a.x mean in c++

What does a.x mean in c++

下次还敢
下次还敢Original
2024-05-07 23:18:18531browse

In C, "a.x" accesses member variables or member functions of class or structure a through the dot operator ".". Member variables return their values ​​and member functions perform calls. Access qualifiers control member access rights.

What does a.x mean in c++

a.x in c

In C, "a.x" means the name x in class or structure a member variables or member functions. The "." (dot) operator is used to access members of an object.

Member variables

If x is a member variable, a.x returns the value of the variable. For example:

<code class="cpp">class Point {
public:
    int x;
    int y;
};

Point p;
p.x = 10;
cout << p.x; // 输出 10</code>

Member function

If x is a member function, a.x() calls the function. For example:

<code class="cpp">class Shape {
public:
    int area() { return 0; }
};

Shape s;
cout << s.area(); // 输出 0</code>

Access Qualifier

Access qualifiers (such as public, private, protected) determine where members can be accessed. If x is a private member, it can only be accessed within the class.

Example

The following is an example of using a.x to access member variables and member functions:

<code class="cpp">class Person {
public:
    string name;
    int age;

    void greet() { cout << "Hello, my name is " << name << endl; }
};

Person p;
p.name = "John";
p.age = 25;
p.greet(); // 输出 "Hello, my name is John"</code>

The above is the detailed content of What does a.x mean in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does a=b mean in c++Next article:What does a=b mean in c++