Monday, August 15, 2011


In this session we were introduced to “class” which is one of the most important significance of Object Oriented Programming.


#1.Problem statement:
We have to find the area of a rectangle using class of an Object Oriented Program.
Solution:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>

class area{
private:
float x1,y1,x2,y2,x3,y3,x4,y4;
float a,b,c;
public:
float input();
float output();
}p;

float area::input(){
cout<<"enter points: A, B, C & D”;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
a=0.5*((x1*(y2-y3))-(x2*(y3-y1))+(x3*(y1-y2)));
b=0.5*((x1*(y3-y4))-(x3*(y4-y1))+(x4*(y1-y3)));
c=sqrt(a*a)+sqrt(b*b);
return c;
}

float area::output(){
cout<<"result="<<input();
}

void main(){
clrscr();
p.output();
getch();
}

Class Diagram:
Class area
Private:
     x1,y1,x2,y2,x3,y3,x4,y4
     a,b,c
Public:
     Input()
     Output()


Remarks:
Here we calculated the area by summing area of two triangles formed by the points.


#2. Problem statement:
We have to calculate the area of a given rectangle by drawing perpendicular on the joining line of two opposite points.

Solution:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>

class area{
private:
float x1,y1,x2,y2,x3,y3,x4,y4;
float a,b,c,d,x,h;
public:
float input();
void output();
}p;

float area::input(){
cout<<"enter points: A, B, C & D”;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
b=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
a=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
x=(a*a+c*c-b*b)/(2*c);
h=sqrt((a*a)-(x*x));
d=c*h;
return d;
}

void area::output(){
cout<<"result="<<input();
}

void main()
{
clrscr();
p.output();
getch();
}

Class Diagram:
Class area
Private:
     x1,y1,x2,y2,x3,y3,x4,y4
     a,b,c,d,x,h
Public:
     Input()
     Output()

Remark:
We used Pythagoras’s law here & doubled area of a triangle to get the required area. 

I had problem with the function input(). If I use it in main() function it get executed twice.but if I use it through output() then it gets the required result. Other than that this session was quite good.

No comments:

Post a Comment

Vision