Write a program to create a class “student” that stores name and roll number of a student. Create another class “marks” that stores three subject marks. Print the details of two students along with average mark by using
i)A common friend function to both the classes
ii)Making a member function of
ii)Making a member function of “student” class as friendly to “marks” class.
#include <iostream>
#include <string>
using namespace std;
class Marks;
class Student{
string name;
int roll_no;
friend void printUsingCommonFriend(Student student, Marks marks);
public:
Student(string name, int roll_no){
this->name = name;
this->roll_no = roll_no;
}
void printUsingMember(){
cout<<"\n\nName: "<<this->name<<endl;
cout<<"Roll number: "<<this->roll_no<<endl;
}
void printUsingMemberFriend(Marks marks);
};
class Marks{
int s1, s2, s3;
float ave;
friend void printUsingCommonFriend(Student student, Marks marks);
friend class Student;
public:
Marks(int s1, int s2, int s3){
this->s1 = s1;
this->s2 = s2;
this->s3 = s3;
ave = (s1 + s2 + s3) / 3;
}
void printUsingMember(){
cout<<"\nSubject 1: "<<this->s1<<endl;
cout<<"Subject 2: "<<this->s2<<endl;
cout<<"Subject 3: "<<this->s3<<endl;
cout<<"Average marks: "<<this->ave<<endl;
}
};
void Student::printUsingMemberFriend(Marks marks){
this->printUsingMember();
marks.printUsingMember();
}
void printUsingCommonFriend(Student student, Marks marks){
cout<<"\nName: "<<student.name<<endl;
cout<<"Roll number: "<<student.roll_no<<endl;
cout<<"Subject 1: "<<marks.s1<<endl;
cout<<"Subject 2: "<<marks.s2<<endl;
cout<<"Subject 3: "<<marks.s3<<endl;
cout<<"Average marks: "<<marks.ave<<endl;
}
int main(){
Student student1("Lucas", 1), student2("Owen", 2);
Marks marks1(50, 60, 70), marks2(80, 90, 94);
cout<<"Using a common friend function\n";
printUsingCommonFriend(student1, marks1);
printUsingCommonFriend(student2, marks2);
cout<<"\n\nUsing member functions\n";
student1.printUsingMember();
marks1.printUsingMember();
student2.printUsingMember();
marks2.printUsingMember();
cout<<"\n\nUsing a member function of student class as friendly to marks class\n";
student1.printUsingMemberFriend(marks1);
student2.printUsingMemberFriend(marks2);
return 0;
}
Comments
Leave a comment