Question:
Pleas help in constructors in OOP?
?
2012-07-25 12:05:55 UTC
I'm learning OOP (Object oriented Programming ) nowadays,, this problem i'm trying to solve,, but it seems like i don't understand it fully or don't know how to use constructors!!

Implement Class Integer that have private data and public methods add , subtract , multiply which take 1 parameter and return Integer . Test class in main

Note : i wanna use constructors + each function takes only one parameter -its data type is Integer not int ,, and it returns (for example the Sum with datatype Integer ,too)


Any help please !! ,, I need any hints or sth. to understand this :S
Three answers:
Jared
2012-07-25 13:24:54 UTC
in c++ the class would look something like this: Look for the comments



class Integer{

private:

int number;

public:

Integer(int); // Constructor declaration

Integer add(int);

Integer subtract(int);

Integer multiply(int);

int value();

};



Integer::Integer(int a){

// This is the constructor definition

number = a;

}



Integer Integer::add(int a){

return number+a;

}



Integer Integer::subtract(int a){

return number-a;

}



Integer Integer::multiply(int a){

return number*a;

}



int Integer::value(){

// Printing purposes only here

return number;

}





//Then a main function for testing



int main(int argc, char *argv[]){

using namespace std;

Integer q (10); // The constructor defined object



cout << "10+3 = " << q.add(3).value() << endl;





return 0;

}
?
2017-01-14 20:38:20 UTC
No it would not, it calls the constructor function, it would not make one. To make an occasion of an merchandise the object would desire to have a Constructor as one in each and every of it fairly is techniques, this methodology is talked approximately as once you create a occasion of your merchandise and could initialize your merchandise. numerous the time you're in simple terms putting variables to their beginning or given values if the Constructor has parameters. in case you attempt to make an occasion that would not have a constructor the two your software will crash or won't function properly.
Josh Parsons
2012-07-25 12:07:44 UTC
I do not see a question anywhere in there and what language is this?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...