How to declare a class in the private access specifier of another clas

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 956 Views - Last Post: 15 March 2010 - 09:46 PM Rate Topic: -----

#1 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 07:55 AM

Hello. I had written a code :
#include <iostream>
#include <cstdlib>

using namespace std;

class Intersecton
{
	private :
		struct Array
		{
			int *Set;
			int size;
		} Set_1, Set_2, Set_3;
			
	public :
		Intersecton() 
		{ 
			Set_1.Set = NULL;
			Set_2.Set = NULL;
			Set_3.Set = NULL;  
		}
		
		void takeInput(Array & );
		
		void findIntersection(Array &, Array &, Array &);
		
		void display(Array & );
		
		void Run()
		{
			cout << "  First set : " << endl << endl;
			takeInput(Set_1);
			
			cout << endl;
			cout << "  Second set : " << endl << endl;
			takeInput(Set_2);
	
			findIntersection(Set_1, Set_2, Set_3);
			cout << endl;

			cout << "  First set : " << endl << "  ";			
			display(Set_1);
			
			cout << "  Second set : " << endl << "  ";			
			display(Set_2);
			
			cout << "  Third set : " << endl << "  ";			
			display(Set_3);
		}
		
};
 
void Intersecton :: takeInput(Array &Arr)
{	
	int element;
	char Choice;
	int n = 0;
	
	do
	{	
		cout << "  Enter an element : ";
		cin >> element;
		
		n++;
		Arr.Set = (int *)realloc(Arr.Set, n * sizeof(int));
		Arr.Set[n - 1] = element;
		
		cout << "  Do you want to enter another element? (y/n) : ";
		cin >> Choice;
	} while (Choice == 'y' || Choice == 'Y');
	
	Arr.size = n;
}

void Intersecton :: findIntersection(Array &Arr_1, Array &Arr_2, Array &Arr_3)
{
	int n = 0;
	
	for (int i = 0; i < Arr_1.size; i++)
	{
		for (int j = 0; j < Arr_2.size; j++)
		{
			if (Arr_1.Set[i] == Arr_2.Set[j])
			{
				n++;
				Arr_3.Set = (int *)realloc(Arr_3.Set, n * sizeof(int));
				Arr_3.Set[n - 1] = Arr_1.Set[i];
			}
		}
	}
	
	Arr_3.size = n;
}

void Intersecton :: display(Array &Arr)
{
	for (int i = 0; i < Arr.size; i++)
	{
		cout << Arr.Set[i] << " ";
	}
	
	cout << endl;
}

int main()
{
	Intersecton Object;
	
	Object.Run();	
	
	return 0;
}


and it is okay. But if I change
private :
		struct Array
		{
			int *Set;
			int size;
		} Set_1, Set_2, Set_3;


into
private :
		class Array
		{
			int *Set;
			int size;
		} Set_1, Set_2, Set_3;


I am getting too much errors. Here is the errors :
tapas@My-Child:~/Programming/Set Intersection$ g++ "Intersection (using array) (copy).cpp" -o "Intersection (using array) (copy).o"
Intersection (using array) (copy).cpp: In constructor ‘Intersecton::Intersecton()’:
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:18: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:19: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:20: error: within this context
Intersection (using array) (copy).cpp: In member function ‘void Intersecton::takeInput(Intersecton::Array&)’:
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:65: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:65: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:66: error: within this context
Intersection (using array) (copy).cpp:12: error: ‘int Intersecton::Array::size’ is private
Intersection (using array) (copy).cpp:72: error: within this context
Intersection (using array) (copy).cpp: In member function ‘void Intersecton::findIntersection(Intersecton::Array&, Intersecton::Array&, Intersecton::Array&)’:
Intersection (using array) (copy).cpp:12: error: ‘int Intersecton::Array::size’ is private
Intersection (using array) (copy).cpp:79: error: within this context
Intersection (using array) (copy).cpp:12: error: ‘int Intersecton::Array::size’ is private
Intersection (using array) (copy).cpp:81: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:83: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:83: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:86: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:86: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:87: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:87: error: within this context
Intersection (using array) (copy).cpp:12: error: ‘int Intersecton::Array::size’ is private
Intersection (using array) (copy).cpp:92: error: within this context
Intersection (using array) (copy).cpp: In member function ‘void Intersecton::display(Intersecton::Array&)’:
Intersection (using array) (copy).cpp:12: error: ‘int Intersecton::Array::size’ is private
Intersection (using array) (copy).cpp:97: error: within this context
Intersection (using array) (copy).cpp:11: error: ‘int* Intersecton::Array::Set’ is private
Intersection (using array) (copy).cpp:99: error: within this context


Is it possble to declare a class in the private access specifier of another class? If possible then what are the changes I have to make in this code? Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: How to declare a class in the private access specifier of another clas

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 08:45 AM

members of a class are by default private, and a struct are default public. If you do use a class dont forget to add the access specifier public: to it
private : 
                class Array 
                { 
                   public: //<---
                        int *Set; 
                        int size; 
                } Set_1, Set_2, Set_3;


This post has been edited by ImaSexy: 15 March 2010 - 08:45 AM

Was This Post Helpful? 1
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 08:49 AM

A struct is a class with the default modifier set to public. So replace the keyword "struct {" with "class { public: "

i.e.:
class Array
{
        public:
        int *Set;
        int size;
} Set_1, Set_2, Set_3;


note, you might as well keep the keyword struct though if you want these to be public.

Though perhaps a better solution would be to do something like this:

class Array
{
        int *Set;
        int size;
        public:
        int *getSet() { return Set; }
        size_t getSize() { return size; }
        Array(int* set = NULL, size_t sz  = 0) : Set(set), size(sz) { }
} Set_1, Set_2, Set_3;


then change your constructor to:
Intersecton() { }

Was This Post Helpful? 1
  • +
  • -

#4 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 08:52 AM

Edit:deleted.

This post has been edited by Tapas Bose: 15 March 2010 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 08:52 AM

Som other notes: Using private sub-classes is fine, but remember not to use them in PUBLIC interfacing functions!!! Because the public does not have access to the PRIVATE class!!!

i.e.: void takeInput(Array & ); <--- Doh! Array is a private class!!! your customers don't have access to it!
Was This Post Helpful? 1
  • +
  • -

#6 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 08:58 AM

@ NickDMax, sir is there any more modification needed? Because again I am getting error. How can I use int *getSet() and size_t getSize()?

This post has been edited by Tapas Bose: 15 March 2010 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 09:06 AM

View PostNickDMax, on 15 March 2010 - 07:52 AM, said:

Som other notes: Using private sub-classes is fine, but remember not to use them in PUBLIC interfacing functions!!! Because the public does not have access to the PRIVATE class!!!

i.e.: void takeInput(Array & ); <--- Doh! Array is a private class!!! your customers don't have access to it!

Sir I read that private data member are accessible by public member function!!!
Was This Post Helpful? 0
  • +
  • -

#8 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 09:13 AM

And how can I take input in the array? Do I have to write another function viz void setArray(int ) and after taking input by takeInput() I have to pass it into setArray(int )? Because as you said Set is not accessible by takeInput()
Was This Post Helpful? 0
  • +
  • -

#9 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 09:43 AM

Okay sir I modify my code as you told :
#include <iostream>
#include <cstdlib>

using namespace std;

class Intersecton
{
	private :
		class Array
		{
			int *Set;
			int size;
			
			public:
				void setArray(int element)
				{
					size++;
					Set = (int *)realloc(Set, size * sizeof(int));
					Set[size - 1] = element;
				}
				
				int **getSet() { return &Set; }
				size_t getSize() { return size; }
				
				Array(int *set = NULL, size_t sz  = 0) : Set(set), size(sz) { }
				
		} Set_1, Set_2, Set_3;
			
	public :
		Intersecton() { }
		
		void takeInput(Array & );
		
		void findIntersection(Array &, Array &, Array &);
		
		void display(Array & );
		
		void Run()
		{
			cout << "  First set : " << endl << endl;
			takeInput(Set_1);
			
			cout << endl;
			cout << "  Second set : " << endl << endl;
			takeInput(Set_2);
	
			findIntersection(Set_1, Set_2, Set_3);
			cout << endl;

			cout << "  First set : " << endl << "  ";			
			display(Set_1);
			
			cout << "  Second set : " << endl << "  ";			
			display(Set_2);
			
			cout << "  Third set : " << endl << "  ";			
			display(Set_3);
		}
		
};
 
void Intersecton :: takeInput(Array &Arr)
{	
	int element;
	char Choice;
	
	do
	{	
		cout << "  Enter an element : ";
		cin >> element;
		
		Arr.setArray(element);
		
		cout << "  Do you want to enter another element? (y/n) : ";
		cin >> Choice;
	} while (Choice == 'y' || Choice == 'Y');
}

void Intersecton :: findIntersection(Array &Arr_1, Array &Arr_2, Array &Arr_3)
{
	int **set_1 = Arr_1.getSet();
	int **set_2 = Arr_2.getSet();
	
	for (int i = 0; i < Arr_1.getSize(); i++)
	{
		for (int j = 0; j < Arr_1.getSize(); j++)
		{
			if ((*set_1)[i] == (*set_2)[j])
			{
				Arr_3.setArray((*set_1)[i]);
			}
		}
	}
}

void Intersecton :: display(Array &Arr)
{
	int **set = Arr.getSet();
	
	for (int i = 0; i < Arr.getSize(); i++)
	{
		cout << (*set)[i] << " ";
	}
	
	cout << endl;
}

int main()
{
	Intersecton Object;
	
	Object.Run();	
	
	return 0;
}



And its working. Is it okay according to C++'s OOP nature? Please tell me.
Another point if I change size_t getSize() { return size; } to size_t *getSize() { return &size; } and change for (int i = 0; i < Arr.getSize(); i++) into for (int i = 0; i < *Arr.getSize(); i++) I am getting error :
Intersection (using array) (copy).cpp: In member function ‘size_t* Intersecton::Array::getSize()’:
Intersection (using array) (copy).cpp:23: error: invalid conversion from ‘int*’ to ‘size_t*’
Was This Post Helpful? 0
  • +
  • -

#10 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 09:50 AM

My last modification :
#include <iostream>
#include <cstdlib>

using namespace std;

class Intersecton
{
	private :
		class Array
		{
			int *Set;
			size_t size;
			
			public:
				void setArray(int element)
				{
					size++;
					Set = (int *)realloc(Set, size * sizeof(int));
					Set[size - 1] = element;
				}
				
				int **getSet() { return &Set; }
				size_t *getSize() { return &size; }
				
				Array(int *set = NULL, size_t sz  = 0) : Set(set), size(sz) { }
				
		} Set_1, Set_2, Set_3;
			
	public :
		Intersecton() { }
		
		void takeInput(Array & );
		
		void findIntersection(Array &, Array &, Array &);
		
		void display(Array & );
		
		void Run()
		{
			cout << "  First set : " << endl << endl;
			takeInput(Set_1);
			
			cout << endl;
			cout << "  Second set : " << endl << endl;
			takeInput(Set_2);
	
			findIntersection(Set_1, Set_2, Set_3);
			cout << endl;

			cout << "  First set : " << endl << "  ";			
			display(Set_1);
			
			cout << "  Second set : " << endl << "  ";			
			display(Set_2);
			
			cout << "  Third set : " << endl << "  ";			
			display(Set_3);
		}
		
};
 
void Intersecton :: takeInput(Array &Arr)
{	
	int element;
	char Choice;
	
	do
	{	
		cout << "  Enter an element : ";
		cin >> element;
		
		Arr.setArray(element);
		
		cout << "  Do you want to enter another element? (y/n) : ";
		cin >> Choice;
	} while (Choice == 'y' || Choice == 'Y');
}

void Intersecton :: findIntersection(Array &Arr_1, Array &Arr_2, Array &Arr_3)
{
	int **set_1 = Arr_1.getSet();
	int **set_2 = Arr_2.getSet();
		
	for (int i = 0; i < (int)(*Arr_1.getSize()); i++)
	{
		for (int j = 0; j < (int)(*Arr_2.getSize()); j++)
		{
			if ((*set_1)[i] == (*set_2)[j])
			{
				Arr_3.setArray((*set_1)[i]);
			}
		}
	}
}

void Intersecton :: display(Array &Arr)
{
	int **set = Arr.getSet();
	
	for (int i = 0; i < (int)(*Arr.getSize()); i++)
	{
		cout << (*set)[i] << " ";
	}
	
	cout << endl;
}

int main()
{
	Intersecton Object;
	
	Object.Run();	
	
	return 0;
}


Is it okay sir?
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 10:08 AM

Note there are lots of changes that you have to make to use the class. Here is my version:
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

class Array {
        int *Set;
        size_t size;
        int dummy;
    public:
        //Function for changing the size of the array
        Array &ReSize(size_t new_size);
        
        //function in increase the size by some number of elements
        Array &IncSize(size_t num);
        
        //get a pointer to the set
        int *getSet() const { return Set; }
        
        //get the current size
        size_t getSize() const { return size; }
        
        //default constructor
        Array() : Set(NULL), size(0), dummy(0) { }
        
        //deconstructor -- note that if you need a deconstructor you also need a copy constructor and an assignment operator!
        ~Array() {
            ReSize(0); // will delete the current array
        }
        
        //Copy constructor
        Array(const Array &other) {
            ReSize(other.size);
            if (Set) { // if size was 0 then Set will be NULL
                memcpy(Set, other.Set, size*sizeof(int));
            }
        }
        
        Array& operator=(const Array& other) {
            //don't do anything for self assignment
            if (&other != this) {
                ReSize(other.size);
                if (Set) { // if size was 0 then Set will be NULL
                    memcpy(Set, other.Set, size*sizeof(int));
                }
            }
            return *this;
        }
        
        //Operator[] for access to elements, Note, if index does not exist, one will be created (thus it is not a const member function).
        int &operator[](size_t index) { 
            if (index < size) {
                return *(Set + index);
            } else {
                ReSize(index + 1);
                return (*this)[index];
            }
        }


        //Operator[] for access to elements, Note will not add elements since the array is const. returns a dummy value rather than throw exception
        const int &operator[](size_t index) const { 
            if (index < size) {
                return *(Set + index);
            } 
            return dummy;
        }
        
};


//Resize the array and copy over the contents...
Array&  Array::ReSize(size_t new_size) {
    //if the new_size is bigger then the current size...
    if (new_size > size) {
        int* oldSet = Set; //save the old pointer
        Set = new int[new_size]; // create a new array
        if (oldSet) { // if the old pointer was not null
            memcpy(Set, oldSet, new_size*sizeof(int)); //copy the old data
            size = new_size; //keep track of our current size...
        }
        delete[] oldSet; //delete the old memory
    } else if (new_size < size) { //if new_size < size no need to allocate new memory!
        if (new_size == 0) { //If new_size == 0 then destry the current array...
            size = new_size; // set the size to 0
            if (Set) { delete[] Set; } // delete the current array
            Set = NULL; //clean up our array ptr
        } else { 
            size = new_size;
        }
    } // if new_size == size -- do nothing.
    return *this;
}

//easy function for adding some elements... 
Array &Array::IncSize(size_t num) {
    return ReSize(size + num);
}

//make printing the array easy...
ostream& operator<<(ostream& out, const Array& arr) {
    out << "[";
    for(size_t i = 0; i < arr.getSize(); ++i) {
        out << arr[i] << ((i != arr.getSize()-1) ? ", ": "");
    }
    out << "]";
    return out;
}

class Intersecton {
    public:
        Array Set_1, Set_2, Set_3;

        Intersecton() {   }

        void takeInput(Array & );

        Array findIntersection(const Array &Arr_1, const Array &Arr_2) ;

        void Run() {
            cout << "  First set : " << endl << endl;
            takeInput(Set_1);
            cout << endl;
            cout << "  Second set : " << endl << endl;
            takeInput(Set_2);
            Set_3 = findIntersection(Set_1, Set_2);
            cout << endl;
            cout << "  First set : " << endl << "  ";
            cout << Set_1 << endl;
            cout << "  Second set : " << endl << "  ";
            cout << Set_2 << endl;
            cout << "  Third set : " << endl << "  ";
            cout << Set_3 << endl;
        }

};

//
void Intersecton::takeInput(Array &Arr) {
    int element;
    char Choice;
    int n = 0;
    do {
        cout << "  Enter an element : ";
        cin >> element;
        Arr[n++] = element;
        cout << "  Do you want to enter another element? (y/n) : ";
        cin >> Choice;
    } while (Choice == 'y' || Choice == 'Y');
}

Array Intersecton::findIntersection(const Array &Arr_1, const Array &Arr_2) {
    int n = 0;
    Array retArray;
    for (size_t i = 0; i < Arr_1.getSize(); i++) {
        for (size_t j = 0; j < Arr_2.getSize(); j++) {
            if (Arr_1[i] == Arr_2[j]) {
                retArray[n++] = Arr_1[i];
            }
        }
    }
    return retArray;
}


int main() {
    Intersecton Object;
    Object.Run();
    
    //Array a;
    //a[0] = 1;
    //cout << a << endl;
    //a[1] = 2;
    //cout << a << endl;
    //a[2] = 3;
    //cout << a << endl;
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

#12 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 10:55 AM

Thank you very much sir. But the third set is not printing. Here is the output:
tapas@My-Child:~/Programming/Set Intersection$ ./test.o
First set :

Enter an element : 1
Do you want to enter another element? (y/n) : y
Enter an element : 2
Do you want to enter another element? (y/n) : y
Enter an element : 3
Do you want to enter another element? (y/n) : y
Enter an element : 4
Do you want to enter another element? (y/n) : y
Enter an element : 5
Do you want to enter another element? (y/n) : n

Second set :

Enter an element : 2
Do you want to enter another element? (y/n) : y
Enter an element : 3
Do you want to enter another element? (y/n) : y
Enter an element : 7
Do you want to enter another element? (y/n) : y
Enter an element : 8
Do you want to enter another element? (y/n) : n

First set :
[1, 2, 3, 4, 5]
Second set :
[2, 3, 7, 8]
Third set :
[]
Was This Post Helpful? 0
  • +
  • -

#13 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 11:12 AM

Is there any problem in
Array& operator=(const Array& other) 
        {
            //don't do anything for self assignment
            
                if (&other != this) 
                {
                        ReSize(other.size);
                        
                        if (other.Set)
                        { // if size was 0 then Set will be NULL
                                memcpy(Set, other.Set, size*sizeof(int));
                        }
                }

                return *this;
        }


in this function cout << sizeof(this->Set)/sizeof(int); is giving 1 always. and cout << (*this)[0] is giving 0.

This post has been edited by Tapas Bose: 15 March 2010 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 11:33 AM

well I have a couple of bugs in Array

first of all in Array::Resize:
memcpy(Set, oldSet, size*sizeof(int)); //copy the old data <-- note the use of size and not new_size as new_size is larger than the originally allocated data


next the size needs to be set OUTSIDE of the if(oldSet) -- else it does not get set at all if oldSet == NULL

        if (oldSet) { // if the old pointer was not null
            memcpy(Set, oldSet, size*sizeof(int)); //copy the old data           
        }
        size = new_size; //keep track of our current size...



Then in the copy constructor the Set, size, and dummy need to be initialized:

        //Copy constructor
        Array(const Array &other)  : Set(NULL), size(0), dummy(0) {            
            ReSize(other.size);
            if (Set) { // if size was 0 then Set will be NULL
                memcpy(Set, other.Set, size*sizeof(int));
            }
        }


#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

class Array {
        int *Set;
        size_t size;
        int dummy;
    public:
        //Function for changing the size of the array
        Array &ReSize(size_t new_size);
        
        //function in increase the size by some number of elements
        Array &IncSize(size_t num);
        
        //get a pointer to the set
        int *getSet() const { return Set; }
        
        //get the current size
        size_t getSize() const { return size; }
        
        //default constructor
        Array() : Set(NULL), size(0), dummy(0) { }
        
        //deconstructor -- note that if you need a deconstructor you also need a copy constructor and an assignment operator!
        ~Array() {
            ReSize(0);
        }
        
        //Copy constructor
        Array(const Array &other)  : Set(NULL), size(0), dummy(0) {            
            ReSize(other.size);
            if (Set) { // if size was 0 then Set will be NULL
                memcpy(Set, other.Set, size*sizeof(int));
            }
        }
        
        Array& operator=(const Array& other) {
            //don't do anything for self assignment
            if (&other != this) {
                ReSize(other.size);
                if (Set) { // if size was 0 then Set will be NULL
                    memcpy(Set, other.Set, size*sizeof(int));
                }
            }
            return *this;
        }
        
        //Operator[] for access to elements, Note, if index does not exist, one will be created (thus it is not a const member function).
        int &operator[](size_t index) { 
            if (index < size) {
                return Set[index];
            } else {
                ReSize(index + 1);
                return (*this)[index];
            }
        }


        //Operator[] for access to elements, Note will not add elements since the array is const. returns a dummy value rather than throw exception
        const int &operator[](size_t index) const { 
            if (index < size) {
                return Set[index];
            } 
            return dummy;
        }
        
};


//Resize the array and copy over the contents...
Array&  Array::ReSize(size_t new_size) {
    //if the new_size is bigger then the current size...
    //cout << "Original Size: " << size << " to: " << new_size << endl;
    if (new_size > size) {
        int* oldSet = Set; //save the old pointer
        Set = new int[new_size]; // create a new array
        if (oldSet) { // if the old pointer was not null
            memcpy(Set, oldSet, size*sizeof(int)); //copy the old data           
        }
        size = new_size; //keep track of our current size...
        delete[] oldSet; //delete the old memory
    } else if (new_size < size) { //if new_size < size no need to allocate new memory!
        if (new_size == 0) { //If new_size == 0 then destry the current array...
            size = new_size; // set the size to 0
            if (Set) { delete[] Set; } // delete the current array
            Set = NULL; //clean up our array ptr
        } else { 
            size = new_size;
        }
    } // if new_size == size -- do nothing.
    return *this;
}

//easy function for adding some elements... 
Array &Array::IncSize(size_t num) {
    return ReSize(size + num);
}

//make printing the array easy...
ostream& operator<<(ostream& out, const Array& arr) {
    out << "[";
    for(size_t i = 0; i < arr.getSize(); ++i) {
        out << arr[i] << ((i != arr.getSize()-1) ? ", ": "");
    }
    out << "]";
    return out;
}

class Intersecton {
    public:
        Array Set_1, Set_2, Set_3;

        Intersecton() {   }

        void takeInput(Array & );

        Array findIntersection(const Array &Arr_1, const Array &Arr_2) ;

        void Run() {
            cout << "  First set : " << endl << endl;
            takeInput(Set_1);
            cout << endl;
            cout << "  Second set : " << endl << endl;
            takeInput(Set_2);
            Set_3 = findIntersection(Set_1, Set_2);
            cout << endl;
            cout << "  First set : " << endl << "  ";
            cout << Set_1 << endl;
            cout << "  Second set : " << endl << "  ";
            cout << Set_2 << endl;
            cout << "  Third set : " << endl << "  ";
            cout << Set_3 << endl;
        }

};

//
void Intersecton::takeInput(Array &Arr) {
    int element;
    char Choice;
    int n = 0;
    do {
        cout << "  Enter an element : ";
        cin >> element;
        Arr[n++] = element;
        cout << "  Do you want to enter another element? (y/n) : ";
        cin >> Choice;
    } while (Choice == 'y' || Choice == 'Y');
}

Array Intersecton::findIntersection(const Array &Arr_1, const Array &Arr_2) {
    int n = 0;
    Array retArray;
    for (size_t i = 0; i < Arr_1.getSize(); i++) {
        for (size_t j = 0; j < Arr_2.getSize(); j++) {
            if (Arr_1[i] == Arr_2[j]) {
                retArray[n++] = Arr_1[i];
            }
        }
    }
    return retArray;
}


int main() {
    Intersecton Object;
    Object.Run();
    //Array a;
    //a[0]=1;
    //a[1]=2;
    //a[2]=3;
    //a[3]=4;
    //cout << "Array a: " << a << endl;
    //Array b = a;

    //cout << "Array b: " << b << endl;
    //cout << "done" << endl;
    //
    //Array a;
    //a[0] = 1;
    //cout << a << endl;
    //a[1] = 2;
    //cout << a << endl;
    //a[2] = 3;
    //cout << a << endl;
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

#15 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to declare a class in the private access specifier of another clas

Posted 15 March 2010 - 11:34 AM

Sir I trying to figure the problem but can't get it.

This post has been edited by Tapas Bose: 15 March 2010 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2