Summation of two array elements by using two different class in OPP(C++)


#include<iostream>
using namespace std;
class C1{
private:
        int sum1 = 0;
public:
        void inp1(int A[], int s1){
                for(int i = 0; i < s1; i++){
                        sum1 = sum1 + A[i];
                }
        }
        friend class C2;
};
class C2{
private:
        int sum2 = 0;
public:
        void inp2(int B[], int s2){
                for(int i = 0; i < s2; i++){
                        sum2 = sum2 + B[i];
                }
        }
        void sum(C1 &obj){
                int S = obj.sum1;
                int res = S + sum2;
                cout<<"Summation of two array elements: "<<res<<endl;
        }
};
int main()
{
        int arr1[] = {2, 3, 4, 5, 6, 7};
        int arr2[] = {20, 30, 40};
        C1 obj1;
        obj1.inp1(arr1, 6);
        C2 obj2;
        obj2.inp2(arr2, 3);
        obj2.sum(obj1);
        return 0;
}

No comments:

Post a Comment