From the course: Object-Oriented Programming with C++
Unlock this course with a free trial
Join today to access over 24,400 courses taught by industry experts.
Friends - C++ Tutorial
From the course: Object-Oriented Programming with C++
Friends
- [Instructor] Let's explore a real world scenario. We are building a review bot that automatically fixes common formatting issues in reviews. The fixed formatting member function retrieves the review title and text, converts their first letter to uppercase, and then updates these variables using the corresponding setters. See the problem? Every time we access a review member variable, we have to first call the gatherers to retrieve their value. After performing the changes, we call the setters to update them. The setters perform input validation, which is unnecessary because our bots changes are safe. Plus, we are making multiple passes over the text when we could fix everything at once. To solve this issue, we can grant direct access to the review classes private members using the friend keyword. Let's switch to the review.h file. In the private section, I will declare the review bot class as friend, friend class ReviewBot. Now it can access the private review members. Finally…
Contents
-
-
-
-
Why should we hide information?1m 44s
-
(Locked)
Doorways to data: Getters and setters7m
-
(Locked)
Applying the DRY principle6m 56s
-
(Locked)
Challenging const correctness3m 3s
-
(Locked)
Friends3m 9s
-
(Locked)
Challenge: Bank account with audit system1m 33s
-
(Locked)
Solution: Bank account with audit system2m 33s
-
-
-
-
-