Assessment CPP
Assessment CPP
Submitted by P Lohith
FRIEND FUNCTION
1.Create class square Use friend function to find area and perimeter.
Solution:
#include <iostream>
using namespace std;
class square{
int side;
float area=0,perimeter=0;
public:
void input();
friend void output(square);
};
void square::input()
{
cout<<"Enter the side: ";
cin>>side;
}
void output(square t)
{
t.area=t.side*t.side;
t.perimeter=4*t.side;
cout<<"Area of Square is "<<t.area;
cout<<"\nPerimeter of Square is "<<t.perimeter;
}
int main() {
square s;
s.input();
output(s);
return 0;
}
Output:
2.Create class length and class breadth use a friend function to find the area and perimeter
of rectangle.
Solution:
#include <iostream>
using namespace std;
class rectangle{
int length,breadth;
int area,perimeter;
public:
void input();
friend void output(rectangle);
};
void rectangle::input()
{
cout<<"Enter the length: ";
cin>>length;
cout<<"Enter the breadth: ";
cin>>breadth;
}
void output(rectangle t)
{
t.area=t.length*t.breadth;
t.perimeter=2*(t.length+t.breadth);
cout<<"Area of Square is "<<t.area;
cout<<"\nPerimeter of Square is "<<t.perimeter;
}
int main() {
rectangle s;
s.input();
output(s);
return 0;
}
Output:
3.Create class simple interest. Use friend function to find the simple interest.
Solution:
#include <iostream>
using namespace std;
class simpleinterest{
int p,t,r;
int si;
public:
void input();
friend void output(simpleinterest);
};
void simpleinterest::input()
{
cout<<"Enter the Principle: ";
cin>>p;
cout<<"Enter the Time: ";
cin>>t;
cout<<"Enter the rate: ";
cin>>r;
}
void output(simpleinterest t)
{
t.si=(t.p*t.t*t.r)/100;
cout<<"Simple interest is: "<<t.si;
}
int main() {
simpleinterest s;
s.input();
output(s);
return 0;
}
Output:
FRIEND CLASS
1..Create class square .Use friend classes to find area and perimeter.
Solution:
#include<iostream>
using namespace std;
class square
{
private:
int side;
public:
square()
{
side=9;
}
friend class operation;
};
class operation
{
public:
void display(square &a)
{
int area,perimeter;
area=a.side*a.side;
perimeter=4*a.side;
cout<<"Area of the square->"<<area<<endl;
cout<<"Perimeter of the square->"<<perimeter<<endl;
}
};
int main()
{
square s;
operation o;
o.display(s);
return 0;
}
Output:
2..Create class length and class breadth use a friend function to find the area and perimeter
of rectangle.
Solution:
#include<iostream>
using namespace std;
class length
{
private:
int l;
public:
length()
{
cout<<"Enter the length: ";
cin>>l;
}
friend class operation;
};
class breath
{
private:
int b;
public:
breath()
{
cout<<"Enter the breadth: ";
cin>>b;
}
friend class operation;
};
class operation
{
public:
void display(length &len,breath &bre)
{
int area,perimeter;
area=len.l*bre.b;
perimeter=2*(len.l+bre.b);
cout<<"Area of the rectangle->"<<area<<endl;
cout<<"Perimeter of the rectangle->"<<perimeter<<endl;
}
};
int main()
{
length l;
breath b;
operation o;
o.display(l,b);
return 0;
}
Output:
3.Create class simple interest. Use friend function to find the simple interest.
Solution:
#include<iostream>
using namespace std;
class simple
{
private:
float principle;
float rate;
int time;
public:
simple()
{
cout<<"Enter the principle amount: ";
cin>>principle;
cout<<"Enter the Rate: ";
cin>>rate;
cout<<"Enter the Time: ";
cin>>time;
}
friend class interest;
};
class interest
{
public:
void display(simple &s)
{
float i;
i=(s.principle*s.rate*s.time)/100;
cout<<"Simple interest="<<i<<endl;
}
};
int main()
{
simple r;
interest u;
u.display(r);
return 0;
}
Output:
Dynamic object creation:
}
int main()
{
int n;
cout<<"enter the number of Employees\n";
cin>>n;
Employee *s=new Employee[n];
for(int i=0;i<n;i++)
{
s[i].getInfo();
}
for(int i=0;i<n;i++)
{
s[i].display();
}
Output:
}
int main()
{
int n;
cout<<"enter the number of students\n";
cin>>n;
Student *s=new Student[n];
for(int i=0;i<n;i++)
{
s[i].getInfo();
}
for(int i=0;i<n;i++)
{
s[i].display();
}
}
Output:
3.Create a class Rectangle use dynamic object creation.
Solution:
#include <iostream>
using namespace std;
class rectangle{
public:
int lenght;
int breadth;
void getinfo();
int area();
int perimeter();
};
void rectangle::getinfo(){
cout<<"Enter the lenght ";
cin>>lenght;
cout<<"Enter the breadth ";
cin>>breadth;
}