- Expert C++
- Vardan Grigoryan Shunguang Wu
- 307字
- 2021-06-24 16:34:01
Behavior
In previous examples, we assigned 5 and then 4 to the rating member variable. We can easily make things unexpectedly wrong by assigning invalid values to the object, like so:
cpp_book.rating = -12;
-12 is invalid in terms of the rating of a product and will confuse users, if allowed to. We can control the behavior of the changes made to the object by providing setter functions:
void set_rating(Product* p, int r) {
if (r >= 1 && r <= 5) {
p->rating = r;
}
// otherwise ignore
}
...
set_rating(&cpp_book, -12); // won't change the state
An object acts and reacts to the requests from other objects. The requests are performed via function calls, which otherwise are called messages: an object passes a message to another. In the preceding example, the object that passed the corresponding set_rating message to the cpp_book object represents the object that we call the set_rating() function in. In this case, we suppose that we call the function from main(), which doesn't actually represent any object at all. We could say it's the global object, the one that operates the main() function, though there is not an entity like that in C++.
We distinguish the objects conceptually rather than physically. That's the main point of thinking in objects. The physical implementation of some concepts of object-oriented programming is not standardized, so we can name the Product struct as a class and claim that cpp_book is an instance of the Product ,and that it has a member function called set_rating(). The C++ implementation almost does the same: it provides syntactically convenient structures (classes, visibility modifiers, inheritance, and so on) and translates them into simple structs with global functions such as set_rating() in the preceding example. Now, let's dive into the details of the C++ object model.
- Implementing Modern DevOps
- Boost C++ Application Development Cookbook(Second Edition)
- Instant Typeahead.js
- jQuery從入門到精通 (軟件開發視頻大講堂)
- 匯編語言程序設計(第3版)
- 用Flutter極速構建原生應用
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- JavaScript:Moving to ES2015
- Visual Basic程序設計
- 移動互聯網軟件開發實驗指導
- Orleans:構建高性能分布式Actor服務
- Mastering AWS Security
- ActionScript 3.0從入門到精通(視頻實戰版)
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)
- MongoDB Cookbook