0% found this document useful (0 votes)
27 views4 pages

Lecture 8

The document defines a Line class with Point struct data members and methods to calculate length, read/set data, get points, and print data. It then inherits from this class to create a Line2 class with additional isHorizontal and isVertical methods. Finally, it defines a base Human class with id, gender, birthday and age calculation, which two derived classes Student and Doctor inherit from, adding their own data members and methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Lecture 8

The document defines a Line class with Point struct data members and methods to calculate length, read/set data, get points, and print data. It then inherits from this class to create a Line2 class with additional isHorizontal and isVertical methods. Finally, it defines a base Human class with id, gender, birthday and age calculation, which two derived classes Student and Doctor inherit from, adding their own data members and methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Inheritance //

Define a structure point, which has two float numbers x,y //


Define a class called line which has 2 data members //
of type points //
The class has function members: calculate_length //
,read_data , set_data, get_p1, get_p2, print_data //
.The initial values for x1,y1,x2,y2 = 0 ,//
Then write a program to make two lines, set first line //
data (line1) to be 10,10, 40, 50 //
.Read second line (line2) data from the user //
.Print which line is longer //

*/
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
}

//constructor line(int x1 , int y1 , int x2 , int y2)


{
;p1.x = x1
; p1.y = y1
; p2.x = x2
; p2.y = y2
}
void read_data(void)
{
;" :cout << "Enter values of x1,y1, x2, y2
; cin >> p1.x >> p1.y >> p2.x >> p2.y
}

void set_data(double a, double b, double c, double d)


{
;p1.x = a
;p1.y = b
;p2.x = c
;p2.y = d
}
void set_x1(double a)
{
;p1.x = a
}
int get_x1(void)
{
;return p1.x
}
point get_p1(void)
{
;return p1
}
point get_p2(void)
{
;return p2
}
void print_data(void)
{
;"cout << "x1 = " << p1.x << "\t
;"cout << "y1 = " << p1.y << "\t
;"cout << "x2 = " << p2.x << "\t
;"cout << "y2 = " << p2.y << "\n
}

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)

;" :cout << "Data of first line are


;)(line1.print_data
;" :cout << "Data of second line are
;)(line2.print_data

;line1.set_data(10,10,40,50)
;)(line2.read_data

;" :cout << "Data of first line are


;)(line1.print_data
;" :cout << "Data of second line are
;)(line2.print_data

;struct point temp1 , temp2


;)(temp1 = line2.get_p1
;)(temp2 = line2.get_p2
; cout << "\t x1= " <<temp1.x << "\t y1 = " << temp1.y
; ;cout << "\t x2= " <<temp2.x << "\t y2 = " << temp2.y << endl

;"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

;"cout << "Firrst Line is longer\n if (len1 > len2)


;"else if (len1 < len2) cout << "Second line is longer\n
;"else cout << "same 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 //

You might also like