Professional Documents
Culture Documents
Q1.
Write the definition of a class FIGURE in C++ with following description:
Private Members
Sides integer
Shape char array of size 20
AssignShape() Member function to assign value of shape based upon Sides as follows:
Sides Shape
<3 Open
==3 Triangle
==4 Quadrilateral
>5 Polygon
Public Members
GetFigure() A function to allow user to enter value of Sides. Also, this function
should call Assign() to assign value of Shape
ShowFigure() A function to display Sides and Shape
Ans:
#include<iostream.h>
#include<string.h>
class FIGURE{
private:
int Sides;
char Shape[20];
public:
void AssignShape(){
if(Sides<3)
strcpy(Shape,”Open”);
else if(Sides==3)
strcpy(Shape,”Triangle”);
else if(Sides==4)
strcpy(Shape,”Quadrilateral”);
else if(Sides>5)
strcpy(Shape,”Polygon”);
}
void GetFigure(){
{
cout<<”\n enter value of Sides:”;
cin>>Sides;
AssignShape();
}
void ShowFigure(){
cout<<”Sides:”<<Sides<<”Shapes:”<<Shape;
}
};
void main(){
FIGURE F;
F. GetFigure();
F. ShowFigure();
}
Q2.
Write the definition of a function grace_score (int score [], int size) in C++, which should
check all the elements of the array and give an increase of 5 to those scores which are
less than 40.
Example: if an array of seven integers is as follows:
45 35 85 80 33 27 90
After executing the function, the array content should be changed as follows:
45 40 85 80 38 32 90
#include<iostream.h>
for(int x=0;x<size;x++)
{
if(score[x]<40)
{
score[x]=score[x]+5;
}
}
}
void main()
{
int A[7]={45,35,85,80,33,27,90};
grace_score(A,7);
for(int x=0;x<7;x++)
{
cout<<A[x]<<" ";
}
}