Class Description:

unit unit2;

interface Type TCircle = class private ciArea:real; protected ciradius:integer; procedure setciArea; public property cirradius: integer read ciradius; property cirarea : real read ciArea; constructor create(radius:integer); end; TCylinder = class(TCircle) private cyArea:real; protected cyradius,cyheight:integer; procedure setcyArea; public property cylradius: integer read cyradius; property cylheight: integer read cyheight; property cylarea : real read cyArea; constructor create(radius, height:integer); end; implementation constructor Tcircle.Create(radius:integer); begin ciradius:= radius; setciArea; end; constructor TCylinder.create(radius,height:integer); begin cyradius:= radius; cyheight:= height; setcyArea; end; procedure TCircle.setciArea; begin ciArea:=cirradius*cirradius*3.14; end; procedure TCylinder.setcyArea; begin cyArea:=2*3.14*cylradius*(cylradius+cylheight); end; end. 

Software module:

 procedure TCircles.Button1Click(Sender: TObject); var a,b:integer; begin if RadioGroup1.ItemIndex=-1 then begin ShowMessage('Выберите фигуру'); end; if (Shape is TCircle) then begin a:=strtoint(edit1.Text); shape:=Tcircle.create(a); label3.Caption:='Площадь окружности = ' + floattostr(shape.cirarea); end; if (Shape is TCylinder) then begin a:=strtoint(edit1.Text); b:=strtoint(edit2.Text); shape:=Tcylinder.create(a,b); //label3.Caption:='Площадь цилиндра = ' + floattostr(shape.cylarea); end; end; 

The problem occurs on the "commented out" line:

[DCC Error] Unit1.pas (60): E2003 Undeclared identifier: 'cylarea'

The error occurs due to the fact that we work with the base class and its descendant, so when we call the descendant class, do we get an error, or is it related to something else? And how to avoid this error, if I need to maintain the class hierarchy for this program, without abstract classes?

  • four
    WTF? Why does the cylinder inherit from the circle ? - karmadro4
  • four
    @tkoff, make better the implementation of specific shapes through one base - TShape. - AseN
  • Karmadro4, This is the task unfortunately. Asen, that's just the point: you need to make the inheritance of the "cylinder" from the circle, without base classes .. - tkoff
  • Composition would be more appropriate. - Nofate

3 answers 3

Well, since such a task ....

What TCylinder = class(TCircle) is not yet inheritance. The properties and methods of the parent and the heir you do not overlap. The radius should be one. area (area) must be one ...

Pseudocode:

 TCircle.area:=PI*TCircle.radius*TCircle.radius; TCylinder.area:=2*(inherited area)+2*PI*(inherited radius)*TCylinder.height; 

    I understand how Shape you have announced as TCircle

    change

     floattostr(shape.cylarea); 

    on

     floattostr(TCylinder(shape).cylarea); 

    or

     floattostr((shape as TCylinder).cylarea); 

    Although you can generally remove cyradius from the TCylinder class and use the ancestor radius (ciradius), at least there will be a meaning in the inheritance.

    • Thanks, it helped, there is no error, but now when choosing the class "cylinder", the procedure for "circle" is performed. This happens because we have a base class - a "circle" and when working with "cylinders" does our program still refer to a "circle"? - tkoff

    Make a virtual f-tion area that the circle will honestly return the area of ​​the circle, and the cylinder - the cylinder. Then everything will fall into place.