搜尋此網誌

2025年9月21日星期日

Function members

Keeping data as private as possible in C++ is a cornerstone of good object-oriented design.

Encapsulation means bundling data with the methods that operate on it.

By making data private, you ensure that it can only be accessed or modified through controlled interfaces (public methods).

This protects the internal state of an object from unintended interference.

Private members prevent external code from directly changing critical values.

In C++, setters and getters are public member functions used to access and modify private data members of a class. They’re essential for encapsulation, which protects internal data and provides controlled access

Getter (Accessor): Retrieves the value of a private member.
Setter (Mutator): Updates the value of a private member.

The concept of a vector spans across mathematics, physics, and computer science --- but each field gives it a slightly different flavor.

In Mathematics, a vector is a quantity with both magnitude and direction.

In Physics, vectors describe physical quantities that require direction and magnitude. Velocity, acceleration, force, momentum are common examples.

In computing, a vector often refers to a dynamic array --- a resizable list of elements.
Example in C++: std::vector<int> nums = {1, 2, 3};

The arrow operator (->) in C++ is used to access members of a structure or class through a pointer. It’s a shorthand for dereferencing a pointer and then accessing a member.

std::string&: passes the string by reference, avoiding a copy.

Together, const std::string& is the best practice for passing large objects efficiently and safely.

In C++, the std::find() function is part of the Standard Template Library (STL) and is used to search for a specific value within a range of elements. It’s defined in the <algorithm> header and works with containers like arrays, vectors, lists, and more.

std::find(start_iterator, end_iterator, value);

start_iterator: Beginning of the range.
end_iterator: One past the last element of the range.
value: The value to search for.

The != operator in C++ means "not equal to". It's a relational operator used to compare two values. If the values are not equal, it returns true; otherwise, it returns false.

items->end() returns an iterator that points one past the last element of the container. It does not point to a valid element. It’s a sentinel used to mark the end of a range.

In C++, the const qualifier is a powerful tool that tells the compiler: “This value should not be changed.” It’s all about protecting data and clarifying intent.

Constant correctness in C++ is the discipline of using the const keyword wherever possible to ensure that data which shouldn't be modified—isn't. It’s not just a style preference; it’s a powerful way to catch bugs early, clarify intent, and make your code safer and more maintainable.

It means, “Everything that isn’t supposed to change should be marked as const.”

In C++, && can be used as logical AND operator:

int a = 5, b = 10;
if (a > 0 && b > a) {
    std::cout << "Both conditions are true!" << std::endl;
}

tweak: to make slight changes to a machine, system, etc. to improve it

for (initialization; condition; update) {
    // code to execute
}

Microsoft Copilot

沒有留言:

發佈留言