OK sumant here is your chance

I look forward to seeing what you have.

Ken
klopterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sumant032199Commented:
Yahoo, fantastic .
It is really a very prompt reply.
Have this code. I must use two different classes for this.

#include <iostream.h>
#include <conio.h>

const MAX_LENGTH=20;
class iB
{
     friend ForArray_B; // ForArray_B is made a friend of iB so as
     // allow it to access the variable i.
     protected:
        int i;        // i stores index of array in ForArray_B.
     public:
     iB()
      {
       i=0;
      }
     void operator = (int n)   // Restricts index (Overloaded =).
      {
       if(n <= (MAX_LENGTH-1) && (n >= 0))   i=n;
      }
};
class ForArray_B
{
     protected: // This is the array which can be used to store info.
         int array[MAX_LENGTH];
     public:
     int& operator [] (iB b) // The [] is overloaded to make sence of
     // array notation.
      {
        return array[b.i];
      }
};

class iA
{
     friend ForArray_A; // ForArray_A is made a friend of iA so as
     // allow it to access the variable i.
     protected:
        int i;        // i stores index of array in ForArray_A.
     public:
     iA()
      {
       i=0;
      }
     void operator = (int n)   // Restricts index (Overloaded =).
      {
       if(n <= (MAX_LENGTH-1) && (n >= 0))   i=n;
      }
};
class ForArray_A
{
     protected: // This is the array which can be used to store info.
         int array[MAX_LENGTH];
     public:
     int& operator [] (iA a) // The [] is overloaded to make sence of
                             // array notation. Returning by reference is the key feature
                             // of this   program.
      {
        return array[a.i];
      }
};
void main(void)
{
     clrscr();
     iA indexA;      // Actually it is an object but due to overloaded
                 // operators it can be used like an integer.
                 // IF YOU WANT ANY ADDITIONAL OPERATION THEN
                 // YOU MUST OVERLOAD IT.
     ForArray_A A;   // An object to store array. Because of overloaded
                 // [] operator a feeling of array notation comes.

     iB indexB;      // Another index
     ForArray_B B;   // and another array.


     indexA=0;
     A[indexA]=15;
     cout << A[indexA] << "\n";

     indexB=0;
     B[indexB]=30;
     cout << B[indexB] << "\n";

//     indexA=indexB; // This will not work until you add a conversion function
                  // or a constructor in any of the two objects.
//     A[indexB];        // This will not work because there is no overloaded
                  // [] to handle indexB in A.
//     if(indexA==indexB) // This is also not possible. == not overloaded.

     getch();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
klopterAuthor Commented:
sumant, I am very impressed with your
ability to put this together.  

It is nice to know that one can
do something like this but I am afraid
that this is too complicated to use
on a regular basis.  What I was hoping
for was something like:

typedef int indexA ;
int A[indexA of size MAXSIZEA];
indexA iA;

typedef int indexB;
int B[indexB of size MAXSIZEB];
indexB iB;

// Such that the following would give compile-time errors:

iA = iB;
B[iA] = 5;


I was also vaguely hoping that something
in the vector package would help me.

What you, nietod and KangaRoo have convinced me is that there is no
simple in-line solution that meets
my needs.


Ken
P.S.  I did take your code and compile
it on gcc.  First I had to get rid of
<conio.h>, clrscr() and getch() because
my gcc does not offer those.  
I then got the following error messgages:

KenArrays.cc:7: `ForArray_B' is neither function nor method; cannot be declared friend
KenArrays.cc: In method `int & ForArray_B::operator [](class iB)':
KenArrays.cc:29: member `i' is a protected member of class `iB'
KenArrays.cc: At top level:
KenArrays.cc:35: `ForArray_A' is neither function nor method; cannot be declared friend
KenArrays.cc: In method `int & ForArray_A::operator [](class iA)':
KenArrays.cc:58: member `i' is a protected member of class `iA'

I also tried compiling it on cxx
(DEC/Compac's C++ compiler and got the
following messages:

cxx: Warning: KenArrays.cc, line 4: omission of explicit type is nonstandard
          ("int" assumed)
const MAX_LENGTH=20;
------^
cxx: Error: KenArrays.cc, line 7: invalid friend declaration
  friend ForArray_B; // ForArray_B is made a friend of iB so as
---------^
cxx: Error: KenArrays.cc, line 29: member "iB::i" is inaccessible
      return array[b.i];
---------------------^
cxx: Error: KenArrays.cc, line 35: invalid friend declaration
  friend ForArray_A; // ForArray_A is made a friend of iA so as
---------^
cxx: Error: KenArrays.cc, line 58: member "iA::i" is inaccessible
      return array[a.i];
---------------------^
cxx: Info: 4 errors detected in the compilation of "KenArrays.cc".
Exit 1



As I said, I am not particularly
interested in pursuing this, but
I thought that I should pass this
along to you since you put so much
time into this.

Thanks again,
  Ken
0
sumant032199Commented:
Thanks a lot.
I am sorry that although beeing a C++ program, it is not portable. This works just fine in Turbo C++ v3.0 under DOS.
If you want to make it sure then you can tell anybody having this compiler to check.

Something about your question.
>>>How should I declare A[], B[], C[],
indexA, indexB, and indexC so that indexA
cannot be used as an index into B[] or C[] and
indexA cannot be compared or set to indexB
or indexC?<<<

1> You can achieve this only if indexA,B,C are objects of different classes.
2>Then using [] in objects leads to operator overloading only.
Hence without these 2 and returning by reference no prgram will be able to perform what you are looking for.

I was thinking about typedef at first but i realised that it won't offer you flexibility and reliability. You must have observed that if you neglect the class definations then main() function is too easy to understand. Some improvements in classes will make it THE PERFECT.

Anyway, You trusted me and gave A grade
Thank you very very much.

I shall be very happy if you check the program in Turbo C++ compiler.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

KangaRooCommented:
Let me see....
0
nietodCommented:
>> cxx: Warning: KenArrays.cc, line 4: omission of
>> explicit type is nonstandard
>> ("int" assumed)
>> const MAX_LENGTH=20;
should be

const int MAX_LENGTH = 20;


>> cxx: Error: KenArrays.cc, line 7: invalid
>> friend declaration
ForAarray_B needs to be declared before it is made a friend.  Add

class ForArray_B;

before class iB's declaration.

>> cxx: Error: KenArrays.cc, line 29: member
>> "iB::i" is inaccessible
>> return array[b.i];
Fixed with the above fix.
'
>> cxx: Error: KenArrays.cc, line 35: invalid
>> friend declaration
>> friend ForArray_A;

declare ForArray_A forward too.

>> beeing a C++ program, it is not portable
Beeing a C++ program it should be portable.  The problem is that your compiler is very (VERY) old and out of date and is letting problems slip by that other compilers catch.
0
KangaRooCommented:
For a more up to date C++ compiler under dos, check http://www.delorie.com for DJGPP
With some fiddling you can also get the command line version of BC5 to run under Dos...
0
sumant032199Commented:
Hi nietod, Kangaroo

Why there are so many errors coming?
0
KangaRooCommented:
Basically because the code was written for an old (late 80's I believe) compiler (Turbo C++). In those days, C++ was a bit different then it is now.
0
sumant032199Commented:
I have Turbo C++ v3.0 1990-92.
0
nietodCommented:
When that compiler was first released, the C++ standards committee hadn't even been formed.  Any compiler from before 1997 is LONG out of date.  (Three years is a long time in computer years.)  

You probalby have no support for exceptions, RTTI, STL, namespaces, overloading of operator new[]  and delete[], or templates..  It is time to upgrade!
0
sumant032199Commented:
I don't have support for exceptions but
RTTI and STL I don't know. Remaining work. And I am having DJGPP will that e ok?
0
sumant032199Commented:
I don't have support for exceptions but
RTTI and STL I don't know. Remaining work. And I am having DJGPP will that be ok?
0
nietodCommented:
I'm surprised you have any of those things.  I'm not familiar with DJGPP.  But any C++ compiler written in the last few years will be a big improvement for you.  Especially if it was written in the last year or two.
0
KangaRooCommented:
DJGPP would do fine, though you might check on the version. DJGPP is currently at version 2.02 (1998)
0
sumant032199Commented:
Yes I am having DJGPP v2.02 with RHIDE.
I will try to implement the above technique in DJGPP
0
sumant032199Commented:
What is RTTI STL.
0
KangaRooCommented:
RTTI stands for RunTime Type Information. It is a language feature which allow you to retrieve runtime type information on objects.
STL stands for Standard Template Library. This is a library of containers and algorithms, all as templates.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.