Skip to main content

Posts

Friend Function in C++

  Friend Function in C++ Definition A friend function in C++ is a special function that can access private and protected members of a class, even though it is not part of that class. Normally, private and protected members of a class can only be accessed by the class's own methods or its friends. To make a function a friend of a class, we declare it inside the class definition using the keyword friend .   Why Do We Use Friend Functions? We use a friend function to allow an external function access to private or protected data of a class without making it a member of that class.   Need for Friend Functions 1.     Access Private Data : Helps an external function work with private or protected members of a class. 2.     Convenience : Simplifies code by avoiding the use of getters or setters. 3.     Specialized Use : Useful when an external function needs a special relationship with the class.   Advanta...

String practice sample question in C++ for Code Clash Vol. 2

Write a C++ program for find the   Length of the string without using strlen() function. #include <iostream> #include <string> using namespace std; int main() {     string str;     int length = 0;     cout << "Enter a string: " << endl;     getline(cin, str);     for (int i = 0; str[i] != '\0'; i++)     {         length++;     }      // or we can write but que asking without length() or size()                                 // length = str.length();     cout << "Length of string: " << length << endl;     return 0; }     Write a program to check the strength of a password as Weak. #include <iostream> using namespace std; int main() {     string pass...