Answer to Question #233003 in C++ for SOM

Question #233003

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.


1
Expert's answer
2021-09-04T15:22:51-0400
#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;
}

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