class with private array

so im going to convert a struct into a class that would do the same thing as before, adding two num together. my current problem is that in struct, since all the members are public, i could access them and input the number i wanted. but in class, i am having the problem of how do i access the private part and transfer to it from a string.
my class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class LargeInt 
{ 
        //some friend fuctions will go here
public: 
	static const int MAXDIGITS = 5;  
	LargeInt();
	bool ReadLargeInt(istream& is = cin);  
private: 
	int Digits[MAXDIGITS]; 
	int numDigits; 
} ; 

bool ReadLargeInt(istream& is = cin)
{
	string nDigits;
	getline(cin,nDigits);
	int len = nDigits.length();
        //not sure how to to transfer the string to the private digit
	return true;
}

my struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct LargeInt	
{
	int Digits[MAXDIGITS];
	int numDigits;
};

bool ReadLargeInt(LargeInt& num)
{
	string s;
	getline(cin,s);
	int len = s.length();		
	if (len<=MAXDIGITS)		
	{
		num.numDigits = len;
		for (int i = len-1; i>=0; i--)
		{
			num.Digits[(len-1)-i]=(s[i]-'0'):
                }	
		return true;
	}
	else 
		return false;

int main()
{
	LargeInt num;
	cout <<"Enter the first number: ";
	while (!ReadLargeInt(num))
        //...
}

does anyone have any example of how i could do it?
In the example you show using a class, the class itself has a function called ReadLargeInt(). This function can access the private data of the class. You need to write the definition as:

1
2
3
4
5
bool LargeInt::ReadLargeInt(istream &is)
{
    //Code goes here.  Code in here can access the class' private data.
    //Note how the default value for is is not added here.  You need that only once, in the header.
}
ok another question that i have is that in struct, i could call num1.numdigit and num2.numdigit and it would tell me the current numdigit of num1 and num2. but in class, i have to call numdigit and i would get the current numdigit. is there a way in which i could know and be able to call numdigit of num1 and num2?
I don't understand the question. If you want to be able to access numdigit from outside the class, all you need to do is make it public. Or you can make a get accessor, which is just a public function that accesses and returns the value of the numdigit variable.

1
2
3
4
5
6
7
8
class LargeInt
{
    int numdigit;

public:
    int GetNumDigit() { return numdigit; }
...
};
i want to check the numdigit so that i could set the loop for adding the num1 and num2 of the array to sum array using the friend operation+
Topic archived. No new replies allowed.