Friend function in class.
Example of friend function in class.
simple code:
#include<iostream>
using namespace std;
class integer;//formal diclaration
class complex{
int a,b;
char sign='+';
int sign1=1;
public:
complex(){a=0,b=0;}
complex(int a,int b){
this->a=a;
this->b=b;
}
void show(){
if(b<0) sign1=-1,sign='-';
else sign='+';
cout<<"Complex number : "<<a<<sign<<b*sign1<<'i'<<endl;
}
friend complex add(complex ,integer );
friend complex add(complex ,complex );
};
class integer{
int c;
friend complex add(complex ,integer );
friend complex add(complex ,complex );
public:
integer(int c){this->c=c;}
};
complex add(complex c,integer i){
complex temp;
temp.a=c.a+i.c;
temp.b=c.b;
return temp;
}
complex add(complex c1,complex c2){
complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return temp;
}
int main()
{
complex com(2,4);
integer in(5);
com.show();
cout<<"After addition with integer : "<<endl;
com=add(com,in);
com.show();
complex com1(2,-7);
com1.show();
cout<<"After addition with complex : "<<endl;
com=add(com,com1);
com.show();
return 0;
}
Operator overloading using friend function
code:
#include<iostream>
using namespace std;
class integer;//formal diclaration
class complex{
int a,b;
char sign='+';
int sign1=1;
public:
complex(){a=0,b=0;}
complex(int a,int b){
this->a=a;
this->b=b;
}
void show(){
if(b<0) sign1=-1,sign='-';
else sign='+';
cout<<"Complex number : "<<a<<sign<<b*sign1<<'i'<<endl;
}
friend complex operator + (complex ,integer );
friend complex operator + (complex ,complex );
};
class integer{
int c;
friend complex operator + (complex ,integer );
friend complex operator + (complex ,complex );
public:
integer(int c){this->c=c;}
};
complex operator + (complex c,integer i){
complex temp;
temp.a=c.a+i.c;
temp.b=c.b;
return temp;
}
complex operator + (complex c1,complex c2){
complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return temp;
}
int main()
{
complex com(2,4);
integer in(5);
com.show();
cout<<"After addition with integer : "<<endl;
com=com+in;
com.show();
complex com1(2,-7);
com1.show();
cout<<"After addition with complex : "<<endl;
com=com+com1;
com.show();
return 0;
}
Another example
simple code:
#include<iostream>
using namespace std;
class integer;//formal diclaration
class complex{
int a,b;
char sign='+';
int sign1=1;
public:
complex(){a=0,b=0;}
complex(int a,int b){
this->a=a;
this->b=b;
}
void show(){
if(b<0) sign1=-1,sign='-';
else sign='+';
cout<<"Complex number : "<<a<<sign<<b*sign1<<'i'<<endl;
}
friend complex add(complex ,integer );
friend complex add(complex ,complex );
};
class integer{
int c;
friend complex add(complex ,integer );
friend complex add(complex ,complex );
public:
integer(int c){this->c=c;}
};
complex add(complex c,integer i){
complex temp;
temp.a=c.a+i.c;
temp.b=c.b;
return temp;
}
complex add(complex c1,complex c2){
complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return temp;
}
int main()
{
complex com(2,4);
integer in(5);
com.show();
cout<<"After addition with integer : "<<endl;
com=add(com,in);
com.show();
complex com1(2,-7);
com1.show();
cout<<"After addition with complex : "<<endl;
com=add(com,com1);
com.show();
return 0;
}
Operator overloading using friend function
code:
#include<iostream>
using namespace std;
class integer;//formal diclaration
class complex{
int a,b;
char sign='+';
int sign1=1;
public:
complex(){a=0,b=0;}
complex(int a,int b){
this->a=a;
this->b=b;
}
void show(){
if(b<0) sign1=-1,sign='-';
else sign='+';
cout<<"Complex number : "<<a<<sign<<b*sign1<<'i'<<endl;
}
friend complex operator + (complex ,integer );
friend complex operator + (complex ,complex );
};
class integer{
int c;
friend complex operator + (complex ,integer );
friend complex operator + (complex ,complex );
public:
integer(int c){this->c=c;}
};
complex operator + (complex c,integer i){
complex temp;
temp.a=c.a+i.c;
temp.b=c.b;
return temp;
}
complex operator + (complex c1,complex c2){
complex temp;
temp.a=c1.a+c2.a;
temp.b=c1.b+c2.b;
return temp;
}
int main()
{
complex com(2,4);
integer in(5);
com.show();
cout<<"After addition with integer : "<<endl;
com=com+in;
com.show();
complex com1(2,-7);
com1.show();
cout<<"After addition with complex : "<<endl;
com=com+com1;
com.show();
return 0;
}
code:
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
#include<stdlib.h>
#include<conio.h>
using namespace std;
class student;
class information{
string name,address,grade;
public:
information(){
name="Are not initialised.";
address="Not found.";
}
information(string name,string address){
this->name=name;
this->address=address;
}
void grade_cal(student A);
void dis(student B);
};
class student{
int roll;
double cgpa;
public:
student(int roll,double cgpa){
this->roll=roll;
this->cgpa=cgpa;
}
student(){
roll=0;
cgpa=0.0;
}
friend void information::grade_cal(student A);
friend void information::dis(student B);
};
void information::grade_cal(student A){
if(A.cgpa>=3.0){
grade="First class.";
}
else if(A.cgpa>=2.5){
grade="Second class.";
}
else if(A.cgpa>=2.0){
grade="Third class.";
}
else{
grade="Fail.";
}
}
void information::dis(student B){
cout<<"Name: "<<name<<endl;
cout<<"Roll: "<<B.roll<<endl;
cout<<"Address: "<<address<<endl;
cout<<"Grade: "<<grade<<endl;
cout<<"CGPA: "<<B.cgpa<<endl;
}
int main() {
system("cls");
int roll=0.0;
double cgpa=0.0;
cout<<"Input Name : ";
string name="Are not initialised.",address="not found.";
getline(cin,name);
cout<<"\nInput Address: ";
getline(cin,address);
cout<<"\nInput roll and CGPA: ";
cin>>roll>>cgpa;
student s(roll,cgpa);
information p(name,address);
return 0;
}
class information{
string name,address,grade;
public:
information(){
name="Are not initialised.";
address="Not found.";
}
information(string name,string address){
this->name=name;
this->address=address;
}
void grade_cal(student A);
void dis(student B);
};
class student{
int roll;
double cgpa;
public:
student(int roll,double cgpa){
this->roll=roll;
this->cgpa=cgpa;
}
student(){
roll=0;
cgpa=0.0;
}
friend void information::grade_cal(student A);
friend void information::dis(student B);
};
void information::grade_cal(student A){
if(A.cgpa>=3.0){
grade="First class.";
}
else if(A.cgpa>=2.5){
grade="Second class.";
}
else if(A.cgpa>=2.0){
grade="Third class.";
}
else{
grade="Fail.";
}
}
void information::dis(student B){
cout<<"Name: "<<name<<endl;
cout<<"Roll: "<<B.roll<<endl;
cout<<"Address: "<<address<<endl;
cout<<"Grade: "<<grade<<endl;
cout<<"CGPA: "<<B.cgpa<<endl;
}
int main() {
system("cls");
int roll=0.0;
double cgpa=0.0;
cout<<"Input Name : ";
string name="Are not initialised.",address="not found.";
getline(cin,name);
cout<<"\nInput Address: ";
getline(cin,address);
cout<<"\nInput roll and CGPA: ";
cin>>roll>>cgpa;
student s(roll,cgpa);
information p(name,address);
p.grade_cal(s);
cout<<"\n\n\t\t\tStudent Information: \n"<<endl;
p.dis(s);return 0;
}
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন