HamroCSIT Logo
HAMRO CSIT
  • Course New
  • Entrance
    • Entrance Preparation
    • MCQ Questions
    • Colleges
    • Entrance Class
    • Entrance Books
    • Free Entrance Video Course
  • Semester
    • First Semester
    • Second Semester
    • Third Semester
    • Fourth Semester
    • Fifth Semester
    • Sixth Semester
    • Seventh Semester
    • Eight Semester
  • Questions
  • Subscription Automated
  • Notices
  • Articles
  • More
    • Ask Question
    • College Ambassadors
    • Financial Support Program
    • Contribute
    • Contact Us
Login Register
Hamro CSIT User Account
  • Sign In
  • Create Account


Shape | Hamro CSIT Shape | Hamro CSIT Shape | Hamro CSIT Shape | Hamro CSIT

Question

  • Home
  • Question
  • Answer
Resolved Guest User Profile Image
Guest User

3 years ago

um_student

How friend function violates the data hiding?

  • Answered by Nemi
  • 0 Comments
  • 3 years ago

In object-oriented programming, data hiding is a principle that allows data to be hidden or encapsulated within a class, so that it cannot be directly accessed or modified from outside the class. This is done to ensure the integrity of the class’s data and prevent unwanted changes or errors.

A friend function is a function that is declared within a class, but is not a member function of the class. It has the special ability to access the private and protected members of the class, which would normally not be accessible from outside the class.

The use of friend functions can violate the principle of data hiding, because it allows external functions to directly access and modify the private and protected members of the class, bypassing the normal rules of encapsulation. This can lead to unintended changes or errors in the class’s data.

For example, let’s say we have a class called “BankAccount” that has private data members for the account holder’s name, account number, and balance. We also have a friend function called “withdrawMoney” that allows the account holder to withdraw money from their account.

If the “withdrawMoney” function is not carefully implemented, it could potentially allow unauthorized access to the account holder’s private data. For example, if the function does not properly validate the user’s identity or restrict the amount that can be withdrawn, it could allow someone to withdraw more money than they should or access someone else’s account information.

In general, it’s important to use friend functions with caution and only when necessary, to ensure that the principle of data hiding is not violated and the class’s data remains secure and consistent.

If you found any error in the answer, please leave a comment and report the answer.
Subscribe
Login
Notify of
Please login to comment
0 Comments
Oldest
Newest Most Voted
Share

Share this link via

Or copy link

logoHAMROCSIT

Hamro CSIT is a comprehensive web and mobile platform that provides B.Sc. CSIT students with resources like notes, syllabi, question banks, solved past papers, practical files, and free entrance preparation materials — all in one place.

  • [email protected]
Semester
  • First Semester
  • Second Semester
  • Third Semester
  • Fourth Semester
  • Fifth Semester
  • Sixth Semester
  • Seventh Semester
  • Eighth Semester
Links
  • About Us
  • FAQs
  • Sitemap
  • Privacy Policy
  • Terms and Conditions
  • College Ambassadors
  • Financial Support Program
Hits Counter
20153638
Google Play App Store
Follow Us

Copyright 2026 | HAMROCSIT | All Right Reserved

Official Payment Partner Esewa Logo
Seventh Sem Discount - Limited Spots

HAMROCSIT.COM

Copyright 2024 | HAMROCSIT.COM | All Right Reserved - Nymna Technology