LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2005, 12:46 PM   #1
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 170

Rep: Reputation: 15
C++ and class with a pointer to a class


Hello,

I'm trying to create a simple phonebook that uses two classes, one that holds the entires, the name and phone number and then a phonebook class which is a collection of the entires. I want to use a dynamic array instead of a vector to hold the entires. I would also like the entires to be inserted into the phonebook in an alphabetical order and for the phonebooks to add and subtract each other. So I guess if I want to insert a new entry to a phonebook, I would have to rezise my dynamic array, then move each entry that's already inside back a spot until the next entry is smaller or the letter in this case comes before the one I want to add right now. That is just inserting a new entry, but how would I be able to use that process to add the two entries together; in this case its an overloaded operator+...not insert function...so would I have to call the insert function but also check of that entry is already in the phonebook that I'm adding to?

The samething would be applied for subtracting; create a new dynamic array of size smaller and just copy all of the entires minus the one that is being taken away..that one is simpler I think.

Where would I have to insert the dynamic array inside the class.

This is what I have thus far:

Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <string>

using namespace std;

class PhoneBookEntry{
private:

public:
	string name, number;

	PhoneBookEntry(){};
};

class PhoneBook{
private:
	PhoneBookEntry* phoneEntry;
	int size;
	bool executedInsert;

public:
	PhoneBook(){size = 0;}
	PhoneBook(const PhoneBook&);
	PhoneBook& operator =(const PhoneBook&);
	~PhoneBook() {delete [] phoneEntry;}

	void insert(string, string) const;
	void changeArray(PhoneBook*&, int&, string);
	
	PhoneBook operator +(const PhoneBook&) const;
	PhoneBook operator -(const PhoneBook&) const;

	friend ostream& operator <<(ostream&, const PhoneBook&);
};

/////////////////////////////////////////////////////////////////////////////////////////////////

PhoneBook& PhoneBook::operator =(const PhoneBook& rhs){
	
	if(this != &rhs){
		delete [] phoneEntry;
		phoneEntry = new PhoneBookEntry[size];

		for(int i = 0; i< size; i++){
			phoneEntry[i] = rhs.phoneEntry[i];
		} //for

		phoneEntry = rhs.phoneEntry;
		
		return *this;
	}
}

////////////////////////////////////////////////////////////////////////////////////

PhoneBook::PhoneBook(const PhoneBook& rhs){
	phoneEntry = NULL;
	*this=rhs;

}

////////////////////////////////////////////////////////////////////////////////////


void PhoneBook::changeArray(PhoneBook*& pbArray, int& oldSize, string todo){
	int newSize;

	if(todo == "add"){
		newSize = oldSize++;
	} 
	else if(todo == "subtract"){
		newSize = oldSize--;
	}

		PhoneBook* temp = new PhoneBook[newSize];

		for(int i = 0; i < oldSize; i++)
			temp[i] = pbArray[i];

		delete [] pbArray;	
		
		pbArray = temp;
		temp = NULL;

		oldSize = newSize;

} //expandArray

////////////////////////////////////////////////////////////////////////////////////

void PhoneBook::insert(string n, string numb) const{
/*
I think think the dynamic array should be made here, 
just entry is being inserted to it, so should it be created
in the private section?
*/
}
////////////////////////////////////////////////////////////////////////////////////

PhoneBook PhoneBook::operator +(const PhoneBook& rhs) const{
	PhoneBook temp;

	temp = *this + rhs;
	
	return temp;

} //operator+

////////////////////////////////////////////////////////////////////////////////////

PhoneBook PhoneBook::operator -(const PhoneBook& rhs) const{
	PhoneBook temp;

	temp = *this - rhs;

	return temp;

} //operator-

////////////////////////////////////////////////////////////////////////////////////

ostream& operator <<(ostream& os, const PhoneBook& p){
	return os << "Name: " 
			  << p.phoneEntry->name 
			  << " Phone: " 
			  << p.phoneEntry->number
			  << endl;

} //operator<<

////////////////////////////////////////////////////////////////////////////////////

int main(){

	PhoneBook x,y,z;
	//x.insert(“John Doe”, “800-255-3417”);
	//x.insert(“Thomas”, “448-230-3630”);
	//y.insert(John Doe”, “”);

	z = x - y;
	cout << z; //should display only the Thomas entry object.


}
I'm not sure how successful the code that I have is going to be thus far, the overloaded operators =, +, -, assignment and copy constructor...

so my question is, if someone would be kind enough to demonstrate how to achieve what I need; what I have described ontop..where the dynamic array should be created (PhoneBook *pbArray = new PhoneBook[size], more-or-less what the + / - of the phonebooks would look like...

any help would be appreciated...

Last edited by niteshadw; 07-18-2005 at 12:50 PM.
 
Old 07-18-2005, 01:16 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
As a comment to your overall architecture, I would avoid using a dynamic array for this. Rather, I would use a linked list with a hash table to improve the speed of searches. The only benefit of a dynamic array is searches (on the key) on the order of O(log n). Inserts and deletes would be O(n), as would searches on anything but the key.

A linked list would give O(1) inserts and deletes, with searches of O(n). Using a strong hash table of the key gives nearly O(1) performance on searches as well. Just a thought.
 
Old 07-18-2005, 11:13 PM   #3
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
I agree, a linked list is the way to go with this project. It will make everything easier (your sorting, deletions, and even insertions). Just search the wikipedia for linked lists if you are unsure about how they work.

Last edited by AngryLlama; 07-18-2005 at 11:14 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 09:52 AM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration