Passing class objects as parameters

Input data into Class Objects

Page 1 of 1

0 Replies - 5188 Views - Last Post: 22 April 2008 - 02:02 PM Rate Topic: -----

#1 hollis092  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 08

Passing class objects as parameters

Posted 22 April 2008 - 02:02 PM

This program inputs from text files data into two class objects (mathGeeks & computerGeeks).
The input files contain names of math and computer innovators.
I use a loop to read in the respective data, class functions are then called to sort, print, check the list, etc.

First I Use a loop to read in the data from the input files to the respective objects.
Then check if the object is full before calling the insert function.
After initializing the objects from the input files, insert my last name into
each object, making sure it is not full first.
Print both objects to the screen with a blank line in between.
Send both objects as parameters to a void function that will print to the
screen just the names that exist in both objects, as per the example below.
Now remove your name from the object that is storing the mathGeeks data.
Be sure to check and make sure it is not empty first.
Finally, print the object that holds the mathGeeks data to the screen.

Output Example:

Boole, Descartes, Euclid, Fermi, Fibonacci,
Kepler, Newton, Pascal, Pingala, Pythagoras,
MyLastName

Babbage, Boole, Codd, Hollerith, Hopper,
Lovelace, Pascal, Pingala, Spurgeon, Turing,
Von-Neumann

The following names are in both lists: Boole Pascal Pingala MyLastName

Boole, Descartes, Euclid, Fermi, Fibonacci,
Kepler, Newton, Pascal, Pingala, Pythagoras


I am currently trying to Send both objects as parameters to a void function that will print to the
screen just the names that exist in both objects. I will attach the project file. The main file is:

/******************************Header Files************************************


#include<iostream>	  //including needed header files
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
#include "nameType.h"   //must include header file for nameType class




//******************************Start of Main***********************************
int main()
{
	nameType mathGeeks,computerGeeks; // declaring class variables of nameType
	string name, name2;			   //string variables to hold input names 
	string myname = "MyLastName";		// string variable for my last name
	ifstream inFile;
	
	inFile.open("mathGeeks.txt");	// open input file
	
	
	int i = 0;
	
	while(inFile && i < 10)		  //eof while loop read in data from
	{								// mathGeeks.txt
		i++;
		inFile >> name;
	
	 
		mathGeeks.isFullList();	 //check to make sure list isn't full
		mathGeeks.insert(name);	 //insert names into object mathGeeks
} 
	   mathGeeks.isFullList();	  //check to make sure list isn't full
	   mathGeeks.insert(myname);	//then insert my last name
	   inFile.close();			  //close input file
	   
	   
	  inFile.open("computerGeeks.txt");	 // open input file
	
	
	int a = 0;
	
	while(inFile && a < 10)				 //eof while loop to read in data
	{									   // from computerGeeks.txt
		a++;
		inFile >> name2;
	
		
		computerGeeks.isFullList();		// check to make sure list isn't full
		computerGeeks.insert(name2);	   // insert names into computerGeeks object
} 
		computerGeeks.isFullList();		// check to make sure list isn't full
		computerGeeks.insert(myname);	  // then insert my last name

	   inFile.close();					 // close input file
  
	


	mathGeeks.printList();				 //print the object mathGeeks
	cout<<endl<<endl;
	computerGeeks.printList();			 //print the object computerGeeks
	cout<<endl<<endl;
	
	
	
	cout<<"The following names are in both lists: ";
	
	
	
	
	
	
			 

	mathGeeks.isEmptyList();			   //check to make sure list isn't empty
	mathGeeks.remove(myname);			  //remove my last name from mathGeeks
	
	cout<<endl<<endl;
	
	mathGeeks.printList();				//print the mathGeeks object with my 
	cout<<endl<<endl;					//last name removed from the list
	
	
 system("pause");
 return 0;   
}
//*********************************End of Main**********************************



header file
class nameType
{
	  public:
			 bool  isEmptyList( ) const;
			 // Function to test if list is empty.
			 // If the member variable 'length' = zero, then the value True 
			 // is returned.
			 
			 bool  isFullList ( ) const;
			 // Function to test if list is full.
			 // If the member variable 'length' is equal to, or greater than
			 // the size of the list, then the value True is returned.
			 
			 int  search ( string searchItem ) const;
			 // Function to locate a given name (string) in the list.
			 // The function searches the list for the given string parameter
			 // and returns the index position to the first match or the value
			 // -1 if the string can not be found.
			 
			 void  insert ( string newName );
			 // Function to add a name to the current list of sorted names
			 // Post condition:  nameList[ length ] = newName
			 //				  length ++
			 //				  sortNames ( )
			 
			 void  remove ( string removeName );
			 // Function to remove a given name from the sorted list of names.
			 // Post condition:  name removed from list
			 //				  list resorted
			 //				  length - -
			 
			 string  copyNameAtPos ( int pos ) const;
			 // Function to return the name at a given index position.
			 // Function will check if the index position is valid.
			 // If valid, it will return the string value at that position
			 // If not valid, it will return the string 'ERROR'
			 
			 void  printList ( ) const;
			 // Function to print the list of names to the default output device.
			 // Prints all the names in the list in sorted order.
			 
			 nameType ( );
			 // Default constructor
			 // Initializes the array of strings to all blanks and sets the 
			 // member variable 'length' to = zero.			  
			 
	  private:
			  string nameList[ 12 ];
			  int	length;
			  
			 void  sortNames ( );
			  // Function to sort the list of names into ascending alphabetical
			  // order.
};



implementation file
// Filename: nameTypeImp.cpp, Implementation file for the class nameType

#include <iostream>
using namespace std;
#include "nameType.h"

bool nameType::isEmptyList ( ) const
{
	 if (length == 0)
		return true;
	 else
		 return false;
}

bool nameType::isFullList( ) const
{
	 if ( length >= 12 )
		return true;
	 else
		 return false;
}

int nameType::search( string searchItem )const
{
	for ( int i = 0; i < length; i++ )
	{
		if ( nameList[i] == searchItem )		
			 return  i;
	}
	
	return -1;
}

void  nameType::insert( string newName )
{
	  nameList[ length ] = newName;
	  length++;
	  sortNames();
}

void  nameType::remove( string removeName )
{
	  int i;
	  i = search(removeName);
	  nameList[ i ] = "zzzzz";
	  sortNames( );
	  length--;
	  nameList[length] = "";	  
}

string  nameType::copyNameAtPos ( int pos ) const
{
		if ( length > 0 && pos < length )
		   return nameList[pos];
		else
			return "ERROR";
}			

void  nameType::printList( ) const
{
	  for ( int i = 0; i < length; i++ )
	  {
		  if ( i < length - 1 )
		  {
			   cout << nameList[i] << ", ";
			   if ( (i + 1) % 5 == 0 )
				  cout << endl;			   
		  }
		  else
			  cout << nameList[i];
	  }
}

nameType::nameType ( )
{
	for ( int i = 0; i < 12; i++ )
		nameList[i] = "";
		
	length = 0;
}

void  nameType::sortNames( )
{
	  int index, lowest, I;
	  string temp;
	  
	  for ( index = 0; index < length - 1; index++ )
	  {
		  lowest = index;
		  
		  for ( I = index + 1; I < length; I++ )
			  if ( nameList[I] < nameList[lowest] )
				 lowest = I;
				 
		  temp = nameList[lowest];
		  nameList[lowest] = nameList[index];
		  nameList[index] = temp;
	  }
} 




Is This A Good Question/Topic? 0
  • +

Page 1 of 1