/* Author: Internshala Module: Fundamentals of Object Oriented Programming Using C++ Topic: Friend Class and Friend Function */ #include <iostream> #include <string> using namespace std; class Employee { private: string phNo; public: string name; void setPhoneNumber(string phoneNumber) { this->phNo = phoneNumber; } friend void display(Employee); // Function declaration }; void display(Employee emp) { // Function definition cout << "Employee name: " << emp.name << ", Phone: " << emp.phNo << endl; } int main() { Employee employee; employee.setPhoneNumber("+91-8093"); employee.name = "Rishi Raj"; display(employee); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter