Passing of class of same object and with a value

I am given an assignment to implement folllowing code...What i dont understand is that in the add,sub and constructor how can large number itself be passed and with a value??

class LargeNumber {
private:
string BIG_NUMBER;
public:
LargeNumber(); // should initialize the number to 0;
LargeNumber(const string val); // initializes the bignumber with given value
LargeNumber(const LargeNumber val); // Copy Constructor
void Add(const LargeNumber right); // Adds the given number to
BIG_NUMBER.
void Sub(const LargeNumber right); // Subtract the given number from
BIG_NUMBER.
void Mul(const LargeNumber right); // Multiply the given number with
BIG_NUMBER
void setNumber(const string val); // Changes the BIG_NUMBER to given
value.
string getLargeN(); // returns the BIG_NUMBER
void print(); // prints the BIG_NUMBER
}:
in the add,sub and constructor how can large number itself be passed and with a value?

LargeNumber is treated as any other type such as int or double or std::string.

Here for example, void Add(const LargeNumber right); the parameter right is a particular variable whose type is LargeNumber.


code tags added for legibility
http://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class LargeNumber {

private:
    string BIG_NUMBER;

public:
    LargeNumber();                      // should initialize the number to 0;
    LargeNumber(const string val);      // initializes the bignumber with given value
    LargeNumber(const LargeNumber val); // Copy Constructor
    void Add(const LargeNumber right);  // Adds the given number to BIG_NUMBER.
    void Sub(const LargeNumber right);  // Subtract the given number from BIG_NUMBER.
    void Mul(const LargeNumber right);  // Multiply the given number with BIG_NUMBER
    void setNumber(const string val);   // Changes the BIG_NUMBER to given value.
    string getLargeN();                 // returns the BIG_NUMBER
    void print();                       // prints the BIG_NUMBER
};
How LargeNumber is a type i have defined it as a class?
Yes. A class can be considered a user-defined type.

Maybe you could read up on the topic, perhaps in a textbook, or in the tutorial pages here:

http://www.cplusplus.com/doc/tutorial/classes/
Why this code is showing error on add function?



#include<iostream>
using namespace std;
class LargeNumber {
private:
string BIG_NUMBER;
public:
LargeNumber()
{
BIG_NUMBER="0";
} // should initialize the number to 0;
LargeNumber(const string val) // initializes the bignumber with given value
{
BIG_NUMBER=val;
}
/*LargeNumber(const LargeNumber val) // Copy Constructor
{

}*/
/*void Add(const LargeNumber right) // Adds the given number to
{

BIG_NUMBER=BIG_NUMBER+right;
}*/
void Sub(const LargeNumber right) // Subtract the given number from
{
BIG_NUMBER=BIG_NUMBER-right;
}
void Mul(const LargeNumber right) // Multiply the given number with
{
BIG_NUMBER=BIG_NUMBER*right;
}
void setNumber(const string val) // Changes the BIG_NUMBER to given
{
BIG_NUMBER=val;
}
string getLargeN()
{
return BIG_NUMBER;
} // returns the BIG_NUMBER
void print() // prints the BIG_NUMBER
{
cout<<BIG_NUMBER;
}
}
main()
{
LargeNumber l(300000000);
LargeNumber v;
LargeNumber v.Add(l.BIG_NUMBER);
cout<<v.getLargeN();
}
Edit: I'd suggest you do what @Chervil said and read more about classes -
http://www.cplusplus.com/doc/tutorial/classes/


Please use code tags so people can read your code better - http://www.cplusplus.com/articles/jEywvCM9/

BIG_NUMBER=BIG_NUMBER*right; BIG_NUMBER=BIG_NUMBER+right;

right is of type LargeNumber. BIG_NUMBER is of type std::string. So I dont know what you're expecting when you do try to add, subtract and multiply these things?

Edit:

A few comments.

Classes ends with semicolon.

1
2
3
4
class hello
{

}; // you're missing the semicolon 


main() // you're missing the int part. should be int main()

You need to #include <string>

LargeNumber l(300000000); Your constructor takes a string. You are not passing it a string.

Last edited on
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/


Why this code is showing error on add function?
1
2
3
4
    void Add(const LargeNumber right) // Adds the given number to
    {
        BIG_NUMBER = BIG_NUMBER+right;
    }

BIG_NUMBER is of type std::string

right is of type LargeNumber

The compiler doesn't know what to do with these completely different types.

You could make it compile by doing this... but ...
1
2
3
4
    void Add(const LargeNumber right) // Adds the given number to
    {
        BIG_NUMBER = BIG_NUMBER + right.BIG_NUMBER;
    }

... but that would merely concatenate the two strings. For example 12 + 34 would give 1234 instead of 46


I think you need to consider the intention of the LargeNumber class.
In order to make it do calculations on numbers, you will have to go through the two strings one digit at a time, and add each pair of digits. Remember that if the result is greater than 9, there will be a digit carried into the next column.
Last edited on
Topic archived. No new replies allowed.