April 19, 2024, 07:01:08 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


1st OOP example

Started by Parker, March 02, 2007, 10:10:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Parker

This is a translation of a class I use in C++. I know there are others (std::string, CString, wxString, etc) but I like to do things myself a lot of the time, and this provides what I need.

Plus there are no other string classes for EB (in fact, there are almost no classes period :D) and if I were to translate std::string or something similar, it would be too complicated for a simple example.

class DynString
private
pointer pstr
uint allocsize
uint s_length
public
declare DynString( )
declare _DynString( )

declare set( DynString s )
declare setz( string s )

declare append( DynString s )
declare appendz( string s )

declare c_str( ), string
declare length( ), int
end class

sub DynString::DynString( )
pstr = new(char, 1)
allocsize = 1
s_length = 0
end sub

sub DynString::_DynString( )
delete pstr
end sub

sub DynString::set( DynString s )
setz( s.c_str( ) )
end sub

sub DynString::setz( string s )
uint slen : slen = len( s )

if slen = 0 then
*<char>pstr[0] = 0
s_length = 0
end if

if slen >= allocsize then
allocsize = slen + 1
delete pstr
pstr = new( char, allocsize )
end if

*<string>pstr = s
s_length = slen
end sub

sub DynString::append( DynString s )
appendz( s.c_str( ) )
end sub

sub DynString::appendz( string s )
uint slen : slen = len( s )

if slen + s_length >= allocsize then
if 2 * allocsize > slen then _
allocsize *= 2 _
else _
allocsize += slen * 2

pointer temp : temp = new( char, s_length + 1 )
*<string>temp = *<string>pstr
delete pstr
pstr = new( char, allocsize )
*<string>pstr = *<string>temp
delete temp
end if

*<string>pstr += s
s_length += slen
end sub

sub DynString::c_str( )
return *<string>pstr
end sub

sub DynString::length( )
return s_length
end sub

'' test code
DynString dstr
dstr.setz( "Hello" )
print dstr.length( ), dstr.c_str( )
dstr.appendz( " World" )
print dstr.length( ), dstr.c_str( )
do:until inkey$()<>""

REDEBOLT

Thanks for the code, Parker.

I ran it in debug, and observed that the stepper does not step into the methods.
I placed the stop in a method, but the debugger didn't display the variables.
Regards,
Bob

Parker

The only local variables are 'slen' in both setz() and appendz(). The others are "instance variables", those that exist within an instance of a class. Make sure you're trying one of those methods.

Other than that, I haven't used the debugger much so I can't really help you there. The methods should be the same as normal subroutines, just with a 'this' parameter.

pistol350

Quote from: Parker on March 02, 2007, 10:10:09 PM
(in fact, there are almost no classes period :D)
;D
LOL ,as far as i am concerned, i would be glad to be able to Say that.
Anyway,thank you Parker for this example, it is useful for guys like me who are newbies to OOP . ;)
Cheers!
Regards,

Peter B.

Techno

Hi all,

I try to build my class project but it fails. What wrong here?. How can I put it in an project?

I have attached to code with separated files

- define and declares the methods, members in the class cDynString.inc
- The implementation of the class cDynString.iwb
- The main program for test the class

Thanks if you give guide lines to developed classes in a project

Kind regards

LarryMc

1) what do you mean "it fails"? Be specific!
2) do you mean an application or do you really mean a "project"? read the help manual section on projects and the tutorial on projects and try to set one up first and then ask your questions?
3)At least one of your class methods doesn't match your declaration.

Is this some important part of a something important you are working on or is this just something you are playing around with.

I really don't have time to spend time on this right now unless it is really, really important; and you will have to explain to me why it is that important right now.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

well
it looks to me that technoMan cannot figure what is what in many things or
he just try to punch us in the head  ::)

Techno

Quote from: LarryMc on June 03, 2015, 01:59:52 PM
1) what do you mean "it fails"? Be specific!
2) do you mean an application or do you really mean a "project"? read the help manual section on projects and the tutorial on projects and try to set one up first and then ask your questions?
3)At least one of your class methods doesn't match your declaration.

Is this some important part of a something important you are working on or is this just something you are playing around with.

I really don't have time to spend time on this right now unless it is really, really important; and you will have to explain to me why it is that important right now.


Larry,

When I try to compiled the source files (inc and iwb) files of the class fails. Is that not correct when I included the implementation of the class file in my main program when test the class?

LarryMc

As usual, you didn't answer my questions.

I'll look at it some time nect week when I get time.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library