Q:

Create a base class named Point that has properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius

0

Create a base class named Point that has properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class the x and y data members represent the center coordinates of a circle. The function members of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points ( sqrt((x2-x1)2 + (y2-y1)2 )). The derived class should have a constructor and an override function named area that returns the area of a circle. Write a script that has 2 objects of each class and calls all of the member functions. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Point.m

classdef Point

 properties

 x = 0;

 y = 0;

 end

 

 methods

 

 function obj = Point(xc, yc)

 if nargin == 2

 obj.x = xc;

 obj.y = yc;

 end

 end

 

 function out = area(obj)

 out = 0;

 end

 

 function out = distance(obja, objb)

 out = sqrt((objb.x - obja.x) ^2 + ...

 (objb.y - obja.y)^2);

 end

 end

end

Circle.m

classdef Circle < Point

 properties

 radius = 0;

 end

 

 methods

 

 function obj = Circle(xc, yc, r)

 if nargin < 3

 xc = 0;

 yc = 0;

 r = 0;

 end

 obj@Point(xc,yc)

 obj.radius = r;

 end

 

 function out = area(obj)

 out = pi * obj.radius ^ 2;

 end

 end

end

Ch11Ex13.m

pta = Point(2.5,5)

ptb = Point(3, 6)

distance(pta,ptb)

pta.distance(ptb)

pta.area

onecirc = Circle(2,5,6);

onecirc.area

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now