Lecture 8
Lecture 8
*/
struct point
{
;double x, y
;}
class line
{
:protected
;struct point p1 , p2
;double length
:public
//constructor line(void)
{
;p1.x = p1.y = p2.x = p2.y = 0
}
double get_length(void)
{
;double dx, dy, length
;dx = pow ((p2.x - p1.x), 2.0)
;dy = pow((double) (p2.y - p1.y), 2.0)
;length = pow(dx + dy, 0.5)
;return length
}
;}
/*
*/
int main(void)
{
;line line1 , line2(1,1,2,2)
;line1.set_data(10,10,40,50)
;)(line2.read_data
;"cout << "Length of first line = " << line1.get_length() << "\n
;"cout << "Length of second line = " << line2.get_length() << "\n
}
/*
Inherit the above class in another class Line2 //
which add two function members //
is_horizontal and is_vertical //
*/
class Line2 : public line
{
:public
)(Line2
{
;)(line
}
Line2(double a , double b , double c , double d)
{
;line(a,b,c,d)
}
bool isHorizontal(void)
{
;return true if(p1.y == p2.y)
;return false else
}
bool isVertical(void)
{
;return true if(p1.x == p2.x)
;return false else
}
;}
void main(void)
{
;Line2 L1, L2
;L1.set_data(10,10,40,50)
;)(L2.read_data
;double len1, len2
;)(len1 = L1.get_length
;)(len2 = L2.get_length
if(L1.isHorizontal() == true)
;"cout << "Line 1 is horizontal \n
if(L1.isVertical() == true)
;"cout << "Line 1 is vertical \n
if(L2.isHorizontal() == true)
;"cout << "Line 2 is horizontal \n
if(L2.isVertical() == true)
;"cout << "Line 2 is vertical \n
}
/*
,Write a class human which has data members: id, gender, birthday //
and function members to read, set, get, print data and //
.calculate_age//
Then inherit this class into two classes students and doctor //
the student class has one more data member: level (int), and two //
more function members setlevel and getlevel //
the doctor class has one more data member salary (float) //
and 2 more function members //