Answer to Question #225288 in C++ for Sharky

Question #225288
Write a program to define two classes Alpha and Beta containing an integer each as data members. Define a function Sum() that will be a friend to both Alpha and Beta, That will take one object from each class as argument and return the sum of the data members of the argument objects.
1
Expert's answer
2021-08-11T14:12:32-0400
#include <iostream>
using namespace std;

class Beta;

class Alpha {
public:
    Alpha(int x) : data(x) {}
private:
    int data;
friend int Sum(Alpha a, Beta b);    
};

class Beta {
public:
    Beta(int x) : data(x) {}
private:
    int data;
friend int Sum(Alpha a, Beta b);    
};

int Sum(Alpha a, Beta b) {
    return a.data + b.data;
}    


int main() {
    Alpha a(1);
    Beta b(2);

    cout << "Alpha + Beta is " << Sum(a, b) << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS