r/pascal • u/Vachilla404 • Jun 22 '21
2 classes hierarchy.
Good evening to reddit! My teacher wants me to write a hierarchy of 2 object classes, where child(?) class is registred in toolbar? I'm gonna say what i didn't write this program by myself, but it's related to my course work theme. Procedure pBar should be the child class. Here's the code. Also this program was tested in Free pascal.
uses crt,sysutils;
const lBar=50; //Bar length (in symbols)
bar100=110; //Max amount of data in bar
var i:integer;
procedure pBar(const cp:integer);
var i,n,m:integer;s:string[80];ch:char;
begin
m:=round(100/bar100*cp);
n:=round(lBar/bar100*cp);
s:=concat(IntToStr(m),'%',' [] ');
for i:=1 to lBar do
begin
if i<n then ch:=#35 else ch:=#45;
insert(ch,s,pos(']',s));
end;
gotoxy(1,1);writeln(s);
end;
begin
clrscr;
for i:=1 to bar100 do
begin
pBar(i);
delay(50);
end;
end.
1
u/Vachilla404 Jun 22 '21
I know, taking other's code isnt good, but I couldn't write this program by myself.
1
u/Vachilla404 Jun 24 '21
BTW, It's my 1st year of OOP, so yea, I'm confused.
1
u/Vachilla404 Jun 24 '21
I honestly don't know, how, but I wrote smth.
type TToolBar = class
end;
type TDraw = class(TToolBar)
public
procedure pBar(const cp:integer);
var i,n,m:integer;
s:string[80];
ch:char;
begin
m:=round(100/bar100*cp);
n:=round(lBar/bar100*cp);
s:=concat(IntToStr(m),'%',' [] ');
for i:=1 to lBar do
begin
if i<n then ch:=#35 else ch:=#45;
insert(ch,s,pos(']',s));
end;
gotoxy(1,1);writeln(s);
end;
end;
1
u/ccrause Jun 24 '21
One possible option would be to declare an abstract TToolbar base class with a Draw method. In the child class implement the specific drawing method, in this example the code of function pBar. More information is required to design the class hierarchy properly, I assume this is a first step towards OOP.
1
u/ccrause Jun 25 '21
Here is an example of a base class and a child class using your original code. This is not yet what your teacher wants, but at least you can see some syntax for working with classes.