Simple class in Matlab ====================== %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Put the following in a file called Square.m % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% classdef Square properties (SetAccess=private, GetAccess=private) x1 = [0,0]; % x1 is the bottom left corner, initialised to (0,0) x2 = [1,1]; % x2 is the top right corner, initialised to (1,1) end methods function obj = Square() % constructor, does nothing end function Plot(obj) plot([obj.x1(1) obj.x2(1) obj.x2(1) obj.x1(1) obj.x1(1)],[obj.x1(2) obj.x1(2) obj.x2(2) obj.x2(2) obj.x1(2)]); end function area = GetArea(obj) area = (obj.x2(2)-obj.x1(2))*(obj.x2(1)-obj.x1(1)); end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % end of Square.m % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Usage: a = Square(); a.GetArea() a.Plot(); Equivalent class (excl. Plot function) in C++ ============================================= // class definition class Square { private: double x1[2]; double x2[2]; public: Square() // Constructor, initialises member variables { x1[0] = 0.0; x1[1] = 0.0; x2[0] = 1.0; x2[1] = 1.0; } double GetArea() { return (x2[0]-x1[0])*(x2[1]-x1[1]); } }; // Usage Square my_square; double area = my_square.GetArea();