0% found this document useful (0 votes)
15 views14 pages

Assessment CPP

The document contains 3 C++ programming assignments that use classes and friend functions. The first creates classes for a square and rectangle and uses friend functions to calculate area and perimeter. The second creates a simple interest class and uses a friend function to calculate interest. The third uses dynamic memory allocation to create arrays of object for employee, student, and rectangle classes.

Uploaded by

lohith8572
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Assessment CPP

The document contains 3 C++ programming assignments that use classes and friend functions. The first creates classes for a square and rectangle and uses friend functions to calculate area and perimeter. The second creates a simple interest class and uses a friend function to calculate interest. The third uses dynamic memory allocation to create arrays of object for employee, student, and rectangle classes.

Uploaded by

lohith8572
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ASSIGNMENT – 17

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:

1.Create a class Employee use dynamic object creation.


Solution:
#include<iostream>
using namespace std;
class Employee
{
public:
string name;
int age;
int id;
void getInfo();
void display();
};
void Employee::getInfo()
{
cout<<"Enter the name\n";
cin>>name;
cout<<"enter the age\n";
cin>>age;
cout<<"enter the id no\n";
cin>>id;
}
void Employee::display()
{
cout<<"************\n";
cout<<"NAME:"<<name<<endl;
cout<<"AGE :"<<age<<endl;
cout<<"ID NO:"<<id<<endl;
cout<<"************\n";

}
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:

2.Create a class Student use dynamic object creation.


Solution:
#include<iostream>
using namespace std;
class Student
{
public:
string name;
int age;
int rollno;
void getInfo();
void display();
};
void Student::getInfo()
{
cout<<"Enter the name\n";
cin>>name;
cout<<"enter the age\n";
cin>>age;
cout<<"enter the roll no\n";
cin>>rollno;
}
void Student::display()
{
cout<<"************\n";
cout<<"NAME:"<<name<<endl;
cout<<"AGE :"<<age<<endl;
cout<<"Rollno"<<rollno<<endl;
cout<<"************\n";

}
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;
}

int area(rectangle r){


int area=r.lenght*r.breadth;
return area;
}

int perimeter(rectangle re){


int peri=2*(re.lenght+re.breadth);
return peri;
}
int main() {

rectangle &s1=*(new rectangle);


s1.getinfo();
cout<<"\nAREA "<<area(s1);
cout<<"\nPERIMETER "<<perimeter(s1);
return 0;
}
Output:

You might also like