Home > Backend Development > C++ > body text

What does a.x mean in c++

下次还敢
Release: 2024-05-07 23:18:18
Original
428 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template