Monday 8 July 2013

C++ program to find the area of triangle using Class

#include<iostream.h>
#include<conio.h>

class triangle
{
private:
    float b ;
    float h ;
public:
    void set();
    void print();
    float area();
} ;
void triangle :: set()
    {
    cout<<"\nEnter the value of Base (then press ENTER key)  : ";
    cin>>b;
    cout<<"Enter the value of Height (then press ENTER key): ";
    cin>>h;
    }
void triangle :: print()
    {
    cout<<"\n\nFormula:";
    cout<<"\nArea of Triangle = 1/2(Base * Height)";
    }
float triangle :: area()
    {
    return 0.5*b*h ;
    }

void main()
{
    clrscr();
    cout<<"\nFinding Area of Triangle";
    cout<<"\n---------------------------";
    triangle r ;
    r.set() ;
    r.print() ;
    cout<<"\n\nResult:";
    cout<<"\nArea of Triangle = "<<r.area();
    cout<<"\n---------------------------";
    cout<<"\n\n\nPress any key to Exit";
    getche() ;
}

No comments:

Post a Comment