subclass properties to superclass

3 views (last 30 days)
Hi guys,
I would like to make subclass properties as properties of superclass.
For example i have a tree structure, in class clcore i have a subclass clmaterialcore and then subsubclasses of clmaterialcore is clsteel, clferrite. I need to decide which type of material to be used.
This is given as a property "matcore". matcore is one of the properties of clcore. i did in a way, but i dont know whether iam doing it correct or wrong. i will explain you with an example since it is a small part of my project.
classdef clcore
properties
Matcore
mc
end
end
This is my superclass
classdef clmaterialcore
properties
mat
m
end
end
This is a subclass clmaterialcore
classdef clsteel
properties
sp
S
end
end
This is a subsubclass steel
classdef clferrite
properties
fp
F
end
end
This is subsubclass ferrite.
Generally the properties sp, fp are the functions which forms a struct . sp forms astruct using the function steel_catalog, fp forms a struct using the function ferrite_catal og.Iam not able to understand how to implement this. Should i implement it in main file or should i implement within the class.
%calculates the values of the clSisteel
objIS.sp=steel_catalog(objIS.S);
%calculates the values of the clFerrite
objIF.fp=ferrite_catalog(objIF.F);
%chooses which maaterial to be used
if objImat.mat==1
objImat.m=steel_catalog(objIS.S);
elseif objImat.mat==2
objImat.m=ferrite_catalog(objIF.F);
else
disp('No such material catalog for core');
return
end
%calculates the values of core
objICore.Matcore=objImat.mat;
objICore.mc=objImat.m;
This is my main file.
Thankyou,
Mohana

Answers (1)

Steven Lord
Steven Lord on 22 Jul 2020
As written you don't have a class hierarchy. You have four completely independent classes. If you want one of your classes to be a subclass of a different one of your classes, you need to have the subclass inherit from the superclass.
It's not really clear to me what you're asking. Are you saying that you want your clsteel class to take whatever the user passes in to construct it and call a function steel_catalog with some of that data to initialize the properties of the object? You can do that in the clsteel class constructor.
Let's take a step back. Can you describe in words not code the system you're trying to model? Are you trying to build a car and specify the material from which each part is made, for example?
  1 Comment
kanuri venkata mohana
kanuri venkata mohana on 22 Jul 2020
Hi Steven, Thankyou for your reply. I want to design a transformer in that I need to decide my core material. As I told before this material is one of the properties of the core. Since I have different materials I need to choose one of them. The chosen material will be the property of my core class. In order to choose my material I have different functions for example steel_catalog . It is a function which contains the material properties like permeability similarly ferrite_ catalog. My question is I am unable to understand how to implement this in OOP.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!