C PROGRAMMING PART A PART B CODE NEEDED lab12Q1cpp lab12Q2cp
C++ PROGRAMMING
PART A:
PART B:
CODE NEEDED:
lab12_Q1.cpp:
lab12_Q2.cpp:
Download lab12 Q1 cpp. Triangles, and Circles are Shapes, geometric objects which can have their perimeter and area calculated. Implement a Shape abstract class, which can be used in the following mann void describeShape (Shape double area s.getArea0 string type s get Type0; double perim s.get Perimeter 0 cout \"This type has a perimeter of perim and an area o area endl. The Shape class is provided for you. Implement Triangle and Circle classes and use the given driver program to produce the following output This Triangle has a perimeter of 12 and an area of: 6 This Circle has a perimeter of: 25.1328 and an area of: 50.2656 Press any key to continue Please do not modify the driver program. No state pertaining to the Triangle or Circle may be stored in the Shape objects. Use Heron\'s formula for calculating the area of a Triangle: A Nos (a b c)/2Solution
Part A:-
source code:-
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
class Shape
{
public:
virtual string getType() = 0;
virtual double getPerimeter() = 0;
virtual double getArea() = 0;
};
class Triangle
{
public:
int x,y,z;
double setDims(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
string getType()
{
cout<<\"It\'s a triangle!!\";
}
double getPerimeter()
{
int add;
add=x+y+z;
return(add);
}
double getArea()
{
int s,temp,area;
s=(x+y+z)/2;
temp=s*(s-x)*(s-y)*(s-z);
area=sqrt(temp);
return(area);
}
};
class Circle
{
public:
int x;
double setDims(int a)
{
x=a;
}
string getType()
{
cout<<\"It\'s a circle!!\";
}
double getPerimeter()
{
double per;
per=2*3.14*x;
return(per);
}
double getArea()
{
double area;
area=3.14*x*x;
return(area);
}
};
void describeShape(Shape &s) {
double a = s.getArea();
double p = s.getPerimeter();
string t = s.getType();
cout << \"This \" << t << \" has a perimeter of: \" << p
<< \" and an area of: \" << a << endl;
}
int main() {
Triangle t;
Circle c;
c.setDims(4);
t.setDims(3, 4, 5);
describeShape(t);
describeShape(c);
return 0;
}
Part B:-
source code:
#include<math.h>
using namespace std;
class Shape {
public:
virtual string getType() = 0;
virtual double getPerimeter() = 0;
virtual double getArea() = 0;
};
class Triangle
{
public:
int x,y,z;
double setDims(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
string getType()
{
cout<<\"It\'s a triangle!!\";
}
double getPerimeter()
{
int add;
add=x+y+z;
return(add);
}
double getArea()
{
int s,temp,area;
s=(x+y+z)/2;
temp=s*(s-x)*(s-y)*(s-z);
area=sqrt(temp);
return(area);
}
};
class Circle
{
public:
int x;
double setDims(int a)
{
x=a;
}
string getType()
{
cout<<\"It\'s a circle!!\";
}
double getPerimeter()
{
double per;
per=2*3.14*x;
return(per);
}
double getArea()
{
double area;
area=3.14*x*x;
return(area);
}
};
void describeShape(Shape &s) {
double a = s.getArea();
double p = s.getPerimeter();
string t = s.getType();
cout << \"This \" << t << \" has a perimeter of: \" << p
<< \" and an area of: \" << a << endl;
}
void largerArea(Shape &a, Shape &b, double *result)
{
double area1,area2;
area1=a.getArea();
area2=b.getArea();
if(area1>area2)
{
result=area1;
cout<<\"triangle has greater area\"<<result;
}
else
{
result=area2;
cout<<\"circle has greater area\"<<result;
}
}
int main() {
Triangle t;
Circle c;
c.setDims(2);
t.setDims(3, 4, 5);
describeShape(t);
describeShape(c);
double result;
largerArea(t, c, &result);
cout << \"The larger area is: \" << result << endl << endl;
t.setDims(6, 8, 10);
describeShape(t);
describeShape(c);
largerArea(t, c, &result);
cout << \"The larger area is: \" << result << endl;
return 0;
}



