Assign a class variable to an instantiation of a class

Want assign the randomly chosen variable to the instantiation

Page 1 of 1

0 Replies - 693 Views - Last Post: 14 February 2009 - 06:19 PM Rate Topic: -----

#1 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Assign a class variable to an instantiation of a class

Post icon  Posted 14 February 2009 - 06:19 PM

Thanks for the all the help on the last post! I got it working!

Next step: I want to assign the randomly chosen array variable to the class instantiation so that I can manipulate it.
Here is the class.

#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <iostream>
#include <cstdlib>
#define NUM_READ_LINES 50

using namespace std;

class Dictionary
{
	friend ostream& operator << (ostream&, const Dictionary&);
	friend istream& operator >> (istream&, Dictionary&);
	friend ifstream& operator >> (ifstream&, Dictionary&);
	friend ofstream& operator << (ofstream&, const Dictionary&);

public:
	char Capital[NUM_READ_LINES][50];
	void openFile1();
	int assignment(int x);

	const Dictionary& operator=(const Dictionary&);
	Dictionary(const char *);
	Dictionary();
	Dictionary(const Dictionary&);
	~Dictionary();
	char &operator[] (int);
	const char &operator[](int) const;

//Overload the relational operators for class Dictionary

	bool operator==(const Dictionary&) const;
	bool operator!=(const Dictionary&) const;
	bool operator<=(const Dictionary&) const;
	bool operator<(const Dictionary&) const;
	bool operator>=(const Dictionary&) const;
	bool operator>(const Dictionary&) const;

	char *strPtr; //Pointer to the char array that holds the string
	int strLength; //Variable to hold the length of the string
};
#endif



Here is the class .cpp file.
#include "Dictionary.h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include <fstream>
#include <time.h>
#define NUM_READ_LINES 50


using namespace std;

Dictionary::Dictionary(const char *str)
{
	strLength = strlen(str);
	strPtr = new char[strLength +1];

	strcpy(strPtr, str);
}

Dictionary::Dictionary()
{
	strLength = 0;
	strPtr = new char[1];
	strcpy(strPtr, "");
}

Dictionary::Dictionary(const Dictionary& capStr)
{
	strLength = capStr.strLength;
	strPtr = new char[strLength +1];
	strcpy(strPtr, capStr.strPtr);
}

Dictionary::~Dictionary()
{
	delete [] strPtr;
}

const Dictionary& Dictionary::operator =(const Dictionary& capStr)
{
	if (this != &capStr)
	{
		delete [] strPtr;
		strLength = capStr.strLength;
		strPtr = new char[strLength + 1];
		strcpy(strPtr, capStr.strPtr);
	}
	return *this;
}

char& Dictionary::operator [](int index)
{
	assert (0 <= index && index < strLength);
	return strPtr[index];
}

const char& Dictionary::operator [](int index) const
{
	assert (0 <= index && index < strLength);
	return strPtr[index];
}

bool Dictionary::operator==(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator <(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator <=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator >(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator >=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator !=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

ostream& operator << (ostream& osObject, const Dictionary& str)
{

	osObject << str.strPtr;

	return osObject;
}

istream& operator >> (istream& isObject, Dictionary& str)
{
	char temp[81];

	isObject >> setw(81) >> temp;
	str = temp;
	return isObject;
}

ifstream& operator >> (ifstream& isObject, Dictionary& str)
{
	char temp[81];

	isObject >> setw(81) >> temp;
	str = temp;
	return isObject;
}
//ofstream& operator << (ofstream& osObject, Dictionary& str)
//{
	//osObject << str.strPtr;

	//return osObject;
//}

void Dictionary::openFile1()
{

	int i = 0;
	int counter = 0;
	ifstream inFile;
	inFile.open("caps.txt");
	while (!inFile.eof() && (i < NUM_READ_LINES))
		{
			inFile.getline(Capital[counter],50);
			counter++;
		}

		for (int i = 0; i < counter; i++) 
		{
			cout << Capital[i] << endl;

		}
}

int Dictionary::assignment(int random_num)
{
	srand((unsigned)time(0)); 
	//cout << (rand() % 50 + 1) << endl;
	random_num = (rand() % 50 +1);
	cout << "The randomly chosen word is: " << Capital[random_num] << endl;



	return random_num;
}



And here is main, where I would like to declare Dict2 = Capital[random_num] but can't figure out exactly how to go about it. I can't just use the assignment operator via const or the default.

#include <iostream>
#include "Dictionary.h"
#include <fstream>

using namespace std;

int main()
{
	int random_num;
	Dictionary Dict;
	Dictionary Dict2;
	Dict = "Kansas City";
	cout << "Dict[2] = " << Dict[2] << endl;
	cout << "The length of Dict is: " << Dict.strLength << endl;

	Dict.openFile1();
	Dict.assignment(random_num);


	cin.get();
	cin.get();

	return 0;

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1