### can anyone here help me to solve this problem

hi experts

can anyone here help me to solve this problem

Problem Statement:

You are required to write a program for calculating area of Trapezoid. Formula for calculating area of trapezoid is

((a+b)/2 )*h

Where a and b are two bases of trapezoid and h corresponds to height.

Detailed Description:

 Create a class named Trapezoid which contains two bases and height as data members.
 Implement a default constructor to initialize all data members with zero and a parameterized constructor which takes three arguments to initialize data members of class.
 Take input from user for base1, base2 and height of 2 objects.
 Overload + operator for the class Trapezoid in such a way that corresponding elements of both objects of the same class can be added.
 Also implement a friend function named calculateArea() which takes two objects of the class Trapezoid as arguments, adds both objects using overloaded + operator and calculates the area of resultant Trapezoid object.
This sort of problem can be built up one step at a time, starting with something simple and adding code for each additional requirement. Please let us know how far you have got so far, and where you are stuck.
We're not going to write your program for you.
Post what you have so far and we will try and help.

Please use code tags (the <> formatting button) when posting your code.
That's easy. Just show your code and where you got stuck so that we can help you. Okay ?
sir, i just want to know that is there any mistake in my code or any modificatin is required or its perfectly alright

Here's the code :

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
class trapezoid{
public:
trapezoid();
trapezoid(float,float,float);
trapezoid operator +(trapezoid&);
void getinput();
void set_base1(float);
void set_base2(float);
void set_hight(float);
void display();
friend void calculateArea(trapezoid,trapezoid);

private:
float base1,base2,hight;
};
trapezoid::trapezoid()
{
base1 = 0.0;
base2 = 0.0;
hight = 0;

}
trapezoid::trapezoid(float bs1,float bs2,float ht)
{
base1 = bs1;
base2 = bs2;
hight = ht;

}
void trapezoid::display()
{
cout<<"base 1: "<<base1<<endl;
cout<<"base 2: "<<base2<<endl;
cout<<"hight : "<<hight<<endl;
cout<<"The Eare of the Trapezoid is: "<<(base1+base2)/2*hight<<endl;
}

void trapezoid::set_base1(float bs1)
{
base1 = bs1;

}
void trapezoid::set_base2(float bs2)
{
base2 = bs2;
}
void trapezoid::set_hight(float ht)
{
hight = ht;
}

trapezoid trapezoid::operator+(trapezoid& trap)
{
trapezoid temp;
temp.base1 = base1 + trap.base1;
temp.base2 = base2 + trap.base2;
temp.hight = hight + trap.hight;
return temp;

}
void trapezoid::getinput()
{
float bs1 = 0.0;
float bs2 = 0.0;
float ht = 0;
cin>>bs1;
cin>>bs2;
cin>>ht;
set_base1(bs1);
set_base2(bs2);
set_hight(ht);
}
void calculateArea(trapezoid bs1,trapezoid bs2)
{
trapezoid temp;
temp = bs1 + bs2;
temp.display();

}
int main()
{
trapezoid bs1;
trapezoid bs2;
cout<<"Object {1}"<<endl;
bs1.getinput();
cout<<endl;
cout<<"Object {2}"<<endl;
bs2.getinput();
calculateArea(bs1,bs2);
_getch();

}
This seems working fine mahi shah, i just checked it out.. i didn't understand your purpose of get and set methods because you can directly pass those parameter during object initialization and i think there is no mention of get and set method in detailed description..
Last edited on
ok thnx sir

Can you help me to make it a good code
Last edited on
you can use proper naming and programing convention so that it becomes more readable and easily understandable..
like, proper spacing, how to name calss and object, proper commenting, etc..
You don't need to have "calculateArea(..)" as a friend, can consider removing it.
thnx Hitesh sir

Santosh sir, what do u mean by "You don't need to have "calculateArea(..)" as a friend, can consider removing it " ?
comment this line
`//friend void calculateArea(trapezoid,trapezoid); `
sorry sir i didn't got ur point

if i place a comment line where should i place that
Last edited on
Why do you need that line? I say remove it from the code.
Last edited on
Topic archived. No new replies allowed.