Jump to content

add two nos


srinivasa

Recommended Posts

Stefan it's a class that adds two numbers together.

 

Anyway I would suggest you try this in your code;

 

$test = new inputs();

$test->$no2 = -9;
$test->addNos();

 

your class is not protected to outside manipulation, now that PHP has pretty good OOP functionality I suggest you start using the proper OOP declarations for variables, classes and functions like: public, protected, private and final.

Edited by krillz
Link to comment
Share on other sites

Am glad to say i got the answer........code is here

 

class Calculator

{

var $sum;

var $dif;

 

function add($number1, $number2)

{

$this->sum=$number1 + $number2;

}

 

function substract($number1, $number2)

{

$this->dif=$number1 - $number2;

}

}

 

$num1=$_POST['no1'];

$num2=$_POST['no2'];

 

//Create an object

$calc = new Calculator();

 

//Call class methods

$calc->add($num1,$num2);

$calc->substract($num1,$num2);

 

//Display output

echo "Sum = ".$calc->sum." <br>";

echo "Difference = ".$calc->dif." <br>";

?>

 

 

now i would like to set initial value using constructor...help me out....

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...