/* 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 class Car; }; class Car { // Friend class of class Employee public: string carName; void display(Employee emp) { cout << "Employee name: " << emp.name << ", Phone: " << emp.phNo << ", Car name: " << carName << endl; } }; int main() { Employee employee; employee.setPhoneNumber("+91-8093"); employee.name = "Rishi Raj"; Car car; car.carName = "Ferrari 488"; car.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