Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Question on OOPS concept

Former Member
0 Kudos

Hi,

I am trying to learn OOPS. I have tried to develop a CLASS just to add & subtract 2 variables (which will be given as input parameters).. Plz find my code below..

When I try to check syntax for the code, it gives me error "The obligatory parameter 'OP1' had no value assigned to it"... Can anyone plz help..

I might have missed some basic step..

REPORT ZVG_OOPS.

class amount definition deferred.

PARAMETERS: p_op1 LIKE bseg-dmbtr OBLIGATORY,

p_op2 LIKE bseg-dmbtr OBLIGATORY.

DATA: addnsub TYPE REF TO amount.

CLASS amount DEFINITION.

PUBLIC SECTION.

METHODS: constructor IMPORTING op1 TYPE dmbtr

op2 TYPE dmbtr,

add IMPORTING op1 TYPE dmbtr

op2 TYPE dmbtr

EXPORTING result TYPE dmbtr,

subtract IMPORTING op1 TYPE dmbtr

op2 TYPE dmbtr

EXPORTING result TYPE dmbtr.

PRIVATE SECTION.

DATA: result TYPE dmbtr.

ENDCLASS.

CLASS amount IMPLEMENTATION.

METHOD constructor.

IF op1 < op2.

MESSAGE e029(ZR) with text-001 p_op1 p_op2.

ENDIF.

ENDMETHOD.

METHOD add.

me->result = op1 + op2.

ENDMETHOD.

METHOD subtract.

me->result = op1 - op2.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA: r_op TYPE dmbtr.

CREATE OBJECT addnsub.

addnsub-> constructor( EXPORTING op1 = p_op1

op2 = p_op2).

addnsub->add( EXPORTING op1 = p_op1

op2 = p_op2

IMPORTING result = r_op ).

Thanks

Geetha

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

CONSTRUCTOR would be called automatically when you instantiate the object using the CREATE OBJECT.

So, you should actually do like this:


*CREATE OBJECT addnsub.
*
*addnsub-> constructor( EXPORTING op1 = p_op1
*op2 = p_op2).

CREATE OBJECT addnsub 
  EXPORTING 
   op1 = p_op1
   op2 = p_op2.

Regards,

Naimesh Patel

8 REPLIES 8

naimesh_patel
Active Contributor
0 Kudos

CONSTRUCTOR would be called automatically when you instantiate the object using the CREATE OBJECT.

So, you should actually do like this:


*CREATE OBJECT addnsub.
*
*addnsub-> constructor( EXPORTING op1 = p_op1
*op2 = p_op2).

CREATE OBJECT addnsub 
  EXPORTING 
   op1 = p_op1
   op2 = p_op2.

Regards,

Naimesh Patel

0 Kudos

Thanks Naimesh.. That answers me!!!! Thanks again for your quick response..

0 Kudos

Hi

Another question..

Now that my code is free of errors, when I execute my code,

REPORT ZVG_OOPS.

class amount definition deferred.

PARAMETERS: p_op1 LIKE bseg-dmbtr OBLIGATORY,

p_op2 LIKE bseg-dmbtr OBLIGATORY.

DATA: addnsub TYPE REF TO amount.

CLASS amount DEFINITION.

PUBLIC SECTION.

METHODS: constructor IMPORTING op1 TYPE dmbtr

op2 TYPE dmbtr,

add IMPORTING op1 TYPE dmbtr

op2 TYPE dmbtr

EXPORTING result TYPE dmbtr,

subtract IMPORTING op1 TYPE dmbtr

op2 TYPE dmbtr

EXPORTING result TYPE dmbtr.

PRIVATE SECTION.

DATA: result TYPE dmbtr.

ENDCLASS.

CLASS amount IMPLEMENTATION.

METHOD constructor.

IF op1 < op2.

MESSAGE e029(ZR) with text-001 p_op1 p_op2.

ENDIF.

ENDMETHOD.

METHOD add.

me->result = op1 + op2.

ENDMETHOD.

METHOD subtract.

me->result = op1 - op2.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA: r_op TYPE dmbtr.

CREATE OBJECT addnsub EXPORTING op1 = p_op1

op2 = p_op2 .

addnsub->add( EXPORTING op1 = p_op1

op2 = p_op2

IMPORTING result = r_op ).

WRITE:/ r_op.

When the method ADD is triggered... me->result gets the added value but its not passed to r_op.. Can you plz help..

Thanks

Geetha

0 Kudos

In the add-method, you are adding OP1 and OP2 and put this value in the private attribute RESULT.

So when you want to retrieve the result you should say something like this:

result (which is the export parameter from the method, and NOT the private attribute) = op1 + op2.

In the current code the result is stored in the private attribute RESULT.

METHOD add.

me->result = op1 + op2.

ENDMETHOD.

me->result => PRIVATE SECTION. DATA: result TYPE dmbtr.

0 Kudos

Not very clear.. can you plz clarify...

How my code should be?

I tried to declare RESULT in PUBLIC SECTION...

Still, I did not get the value in r_op..

0 Kudos

Since your expecting the result back in the RESULT parameter, you need to assign the result in that value.


METHOD add.
me->result = op1 + op2.
result = me->result.
ENDMETHOD.

METHOD subtract.
me->result = op1 - op2.
result = me->result.
ENDMETHOD.

If you don't want to use the attribute RESULT in your any calculation than you don't need it. You can just pass the two parameters and get back the result in the exporting parameter.

Regards,

Naimesh Patel

0 Kudos

Perfect!!!!! it worked.. Thankyou...

Former Member
0 Kudos

I have posted a new question in the same tag..