Program to add marks of class academic with marks of class extra Elective using friend function - C++

 //Program to add marks of academic class with marks of extraElective class using friend function

#include <iostream>

using namespace std;

class extraElective; //forward declaration

class academic{

    private: 

    int marks;

    public:

    friend int add(academic, extraElective);

    void getData();

};   //end of class

 void academic::getData(){

    cout<<"Enter the academic marks "; 

    cin>>marks;

}

class extraElective{

    private: 

    int marks;

    public:

    friend int add(academic, extraElective);

    void getData();

}; //end of class

void extraElective::getData(){

    cout<<"Enter the elective marks "; 

    cin>>marks;

}

int add(academic obj1, extraElective obj2){      //expecting parameters of class type

 return (obj1.marks+obj2.marks);   

}  // friend function

int main(){

    academic obj1;

    extraElective obj2;

    obj1.getData();

    obj2.getData();

    cout<<"The Sum of both marks: " <<add(obj1,obj2);  //passing objects as arguments

    return 0;

}

Output: 





We have used Dev C++ IDE for this program. If you have any queries regarding this, you can leave a comments as well. 

stay Safe! stay Positive ! 
Thank you.
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment