Skip to content

Instantly share code, notes, and snippets.

@rsmahmud
Created January 27, 2017 13:24
Show Gist options
  • Save rsmahmud/48d0fd9d35f3083dd2691577e91401b8 to your computer and use it in GitHub Desktop.
Save rsmahmud/48d0fd9d35f3083dd2691577e91401b8 to your computer and use it in GitHub Desktop.
Friend Function Concept in C++
/*
* Create 3 classes which takes the data for 3 semesters containing info of subject and marks
* Calculate the % for all semester
* finally calculate CGPA with a function program
*/
#include<iostream>
#include<conio.h>
using namespace std;
class Semester1;
class Semester2;
class Semester3{
int marks[5];
float perc;
public:
void get(){
perc=0;
cout<<"\nEnter marks of Semester 3\n\n";
cout<<"Marks of 5 Subjects : ";
for(int i=0; i<5; i++){
do{
cin>>marks[i];
if(marks[i]<0 || marks[i]>100)
cout<<"Invalid Marks\n";
}while(marks[i]<0 || marks[i]>100);
perc+=marks[i];
}
perc/=5;
cout<<"\nPercentage of Semester 3 : "<<perc<<"%"<<endl;
}
friend void CGPA(Semester1, Semester2, Semester3);
};
class Semester1{
int marks[5];
float perc;
public:
void get(){
perc=0;
cout<<"\nEnter marks of Semester 1\n\n";
cout<<"Marks of 5 Subjects : ";
for(int i=0; i<5; i++){
do{
cin>>marks[i];
if(marks[i]<0 || marks[i]>100)
cout<<"Invalid Marks\n";
}while(marks[i]<0 || marks[i]>100);
perc+=marks[i];
}
perc/=5;
cout<<"\nPercentage of Semester 1 : "<<perc<<"%"<<endl;
}
friend void CGPA(Semester1, Semester2, Semester3);
};
class Semester2{
int marks[5];
float perc;
public:
void get(){
perc=0;
cout<<"\nEnter marks of Semester 2\n\n";
cout<<"Marks of 5 Subjects : ";
for(int i=0; i<5; i++){
do{
cin>>marks[i];
if(marks[i]<0 || marks[i]>100)
cout<<"Invalid Marks\n";
}while(marks[i]<0 || marks[i]>100);
perc+=marks[i];
}
perc/=5;
cout<<"\nPercentage of Semester 2 : "<<perc<<"%"<<endl;
}
friend void CGPA(Semester1, Semester2, Semester3);
};
void CGPA(Semester1 s1, Semester2 s2, Semester3 s3){
cout<<"\n\nCGPA : "<<(( s1.perc/10.0)+(s2.perc/10.0)+(s3.perc/10.0))/3.0<<endl;
}
int main(){
Semester1 s1;
Semester2 s2;
Semester3 s3;
s1.get();
s2.get();
s3.get();
CGPA(s1,s2,s3);
return getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment