Create a base class Square and then a derived class Cube, similar to the Rectangle/Box example from the chapter. Include a function to calculate the area of a square and volume of a cube
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:11| Question number:12.11
All Answers
total answers (1)
Square.m
classdef Square
properties
side = 1;
end
methods
function obj = Square(s)
if nargin == 1
obj.side = s;
end
end
function outarg = area(obj)
outarg = obj.side ^ 2;
end
function disp(obj)
fprintf('The square has side %.2f\n', obj.side)
end
end
end
Cube.m
classdef Cube < Square
% properties are not needed
methods
function obj = Cube(s)
if nargin == 0
s = 1;
end
obj@Square(s)
end
function out = volume(obj)
out = obj.side ^ 3;
end
function disp(obj)
fprintf('The cube has volume %.1f\n', volume(obj))
end
end
end
>> mys = Square(3)
mys =
The square has side 3.00
>> mys.area
ans =
9
>> myc = Cube(4)
myc =
The cube has volume 64.0
need an explanation for this answer? contact us directly to get an explanation for this answer