Friend Function

#include<iostream.h>
#include<conio.h>
class matrix
{
int i,j,m,n;
int mat[2][2];
public:
void read();void display();
friend matrix add(matrix m1,matrix m2);
}m1,m2,m3;
matrix add(matrix m1,matrix m2)
{
matrix z;
z.m=m1.m;
z.n=m1.n;
for(int k=0;k<m1.m;k++)
{
for(int l=0;l<m1.n;l++)
{
z.mat[k][l]=m1.mat[k][l]+m2.mat[k][l];
}
}
return z;
}
void matrix::read()
{
cout<<” enter size”;
cin>>m>>n;
cout<<” nw enter”;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>mat[i][j];
}
}
}
void matrix::display()
{
cout<<” answer”;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<mat[i][j];
}
}

}
void main()
{
clrscr();
m1.read();
m2.read();
m3=add(m1,m2);
m3.display();
getch();
}

Class- Student

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
private:
int roll;char name[30];int clas[7];int marks[5];float percentage;
int calculate();
public:
void readmarks();
void displaymarks();
};
int student::calculate()
{
int s=0;
for(int a=0;a<5;a++)
{
s=s+marks[a];
}
percentage=s/5;
return (percentage);
}
void student::readmarks()
{
cout<<“enter”;
for(int b=0;b<5;b++)
{
cin>>marks[b];
}
percentage=calculate();
}
void student::displaymarks()
{
cout<<“marks r”;
for(int c=0;c<5; c++)
{
cout<<marks[c];
cout<<“\n”;
}
cout<<“\n”<<percentage;
}
void main()
{
clrscr();
student s1;
s1. readmarks();
s1. displaymarks();
getch();
}

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives