Practical-12
Write a cpp program for a friend function.
Introduction
Code
Practical-12.cpp
#include <iostream>
class Animal
{
public:
Animal(std::string name)
{
this->name = name;
}
void showName()
{
std::cout << "My name is: " << this->name << std::endl;
}
private:
std::string name;
};
// Friend Function
void showNameFriend( Animal& a )
{
a.showName();
}
int main ()
{
Animal dog("Spot");
showNameFriend(dog);
return 0;
}
Output
My name is: Spot