C++ Examples
- Write a C++ Program to print a welcome text in a separate line.
Write a program to add data objects of two different classes using friend function.
#include<iostream>
using namespace std;
//class declaration
class a2;
class a1
{
private:
int num1 = 45;
friend int add(a1 obj1 , a2 obj2);
};
class a2
{
private:
int num2 = 70;
friend int add(a1 obj1 , a2 obj2);
};
//function to add two class members
int add(a1 obj1 , a2 obj2)
{
return obj1.num1 + obj2.num2;
}
int main()
{
a1 obj1;
a2 obj2;
cout<<"Addition of numbers is "<<add(obj1,obj2);
}
Output
Addition of numbers is 115