//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:
0 comments:
Post a Comment