C++ linked list

struct or class?

Page 1 of 1

7 Replies - 4691 Views - Last Post: 19 November 2010 - 05:13 PM Rate Topic: -----

#1 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

C++ linked list

Posted 19 November 2010 - 02:52 PM

I'll try and make this somewhat short. I had an assignment to make a c++ inheritance program that uses 5 headers and 5 cpp's with a main. One header/cpp inherits the other 4 and main calls the base..basic. So I had trouble understanding inheritance and it took me a bit, but got it going. Now the assignment is to revise the program using linked list to load the user input for the address book program. - My text book for school sucks, the professor even hates it. I got another c++ book to help and the two contradict one another. The question I have, is which is better, class or struct? I have all the classes built, or can you just use a struct inside the class? (we skipped templates due to time left in semester, so haven' grasp/dove into that yet). I will show one of the smallest classes. It is used to set name when when inherited base class is called from main() to user into set name.( now how to set it up in linked list input)

/* personType header */

using namespace std;
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
class personType{

private:
	string firstName;
	string lastName;
public:
    void print()const;
	void setName(string,string);
	string getFirstName()const;
	string getLastName()const;
	personType(string first = "", string last = "");//default constructor
};
#endif
________________________________________________________________________________

/* person class implement cpp */

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

void personType::print()const{
	
	cout << firstName << " " << lastName << endl;
}
void personType::setName(string first, string last){

	firstName = first;
	lastName = last;
}
string personType::getFirstName()const{

	return firstName;
}
string personType::getLastName()const{

	return lastName;
}
personType::personType(string first,string last){
	p = Null;
	firstName = first;
	lastName = last;
}




Now should I do something like a struct inside the class? then just use the pointers in the personType cpp to cout the results?

private:
	string firstName;
	string lastName;
 
        struct node{
		node *link;
		*head

		{//end of struct node
public:
//just generic code really for basic idea.



Or would struct be used sort of like a prototype in the since it goes before the header as such

struct Node{
   string firstName, lastName;
   node *link
   *head
};//end struct

class personType{
  public: 
    personType();
  private:
   //whatever here
}; //end personType class 




Am I sort of on the right track? Just looking for a push in the right direction please...tired and confused a bit. Thanks to all, regards.
(singly linked list btw)

Is This A Good Question/Topic? 0
  • +

Replies To: C++ linked list

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C++ linked list

Posted 19 November 2010 - 02:57 PM

Quote

The question I have, is which is better, class or struct? I have all the classes built, or can you just use a struct inside the class?

The only fundamental difference between a class and a struct in C++ is that with a struct all members default to public visibility.

And yes, you can include a struct in a class.
Was This Post Helpful? 1
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ linked list

Posted 19 November 2010 - 03:01 PM

struct and class are almost exactly the same thing. The only difference is that in a class, all members are private by default but any can be explicitly declared public (or private for that matter). In contrast, in a struct, all members are public by default but can be explicitly declared private (or public).

If the nodes are only going to be used inside a single class, I don't think there is anything wrong with declaring the node type (struct or class) inside that class. If you want them to be visible elsewhere in your program, declare the node type in its own header (either as a struct or a class -- whichever you prefer).
Was This Post Helpful? 0
  • +
  • -

#4 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: C++ linked list

Posted 19 November 2010 - 03:15 PM

View Postr.stiltskin, on 19 November 2010 - 02:01 PM, said:

struct and class are almost exactly the same thing. The only difference is that in a class, all members are private by default but any can be explicitly declared public (or private for that matter). In contrast, in a struct, all members are public by default but can be explicitly declared private (or public).

If the nodes are only going to be used inside a single class, I don't think there is anything wrong with declaring the node type (struct or class) inside that class. If you want them to be visible elsewhere in your program, declare the node type in its own header (either as a struct or a class -- whichever you prefer).


Cool. like if I do


	struct Node{//header
	   string
	   node *link
	   *head
	};//end struct
	 
	class personType{//header
	  public:
	    personType();
	  private:
	   //whatever here
	}; //end personType class 



making a sctuct header over the class header..in the same header.h file?
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ linked list

Posted 19 November 2010 - 03:34 PM

You can do that (although you have some syntax errors in your node declaration) -- then the node and its members will be visible in any file that #includes that header.

Or,if the nodes will only be used by member functions of the addressBook class, you could do:

class addressBook {
    public:
        addressBook();
        // other public members of addressBook ...

    private:
        struct Node {
            string somethingorother;
            Node* next;
            // other members of Node ...
        };
        // other private members of addressBook ...
};


This post has been edited by r.stiltskin: 19 November 2010 - 03:37 PM

Was This Post Helpful? 1
  • +
  • -

#6 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: C++ linked list

Posted 19 November 2010 - 04:28 PM

r.stiltskin thanks mate. (and Jack.)I think I have a basic understanding. The personType,extPersonType,addressType,dateType will be inherited into the addressBooktype that main() calls to load all the user input. i.e. Enter you first name. Enter your last name.(from personType). Then the address info user input from addressType and so on. So if it's inherited (personType) from addressBookType, then I private struct should be o.k. , and that is in the form of an indirect question..
All the personType class does is waits for the addressBookType to be called from main() when user is prompted to enter stuff..like name. Then the personType class/struct is involved (used to be setter/getter but now it will be loaded via linked list) Am I correct about the basic understanding/"structure" of it in that since?
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ linked list

Posted 19 November 2010 - 04:42 PM

Maybe you're just using the term "inheritance" inappropriately, because I don't see how inheritance has any application here. Your addressBook may contain objects of personType, but an addressBook should not be an object of personType (or an object derived from personType).

Inheritance applies when you create a base class, say personType, with certain member variables (data) and member functions, and then you create a derived class, say studentType, which inherits all of those members from personType and adds some additional "special" members that studentType objects need but which are not needed by all personType objects.

So getting back to your original question, and thinking some more about your addressBook application ...
I don't know the specific requirements of your assignment, but I think I would make an addressBook class which contains a (private) linked list of personType objects and some (public) functions to manipulate the list (add entries, delete entries, print, etc). My personType object would contain strings for firstname and lastname, functions to construct and print them, etc., and variables to hold any other relevant attributes of a personType -- including address, so if you must have a separate addressType class, I would then have an addressType object as a data member of the personType class.

This post has been edited by r.stiltskin: 19 November 2010 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: C++ linked list

Posted 19 November 2010 - 05:13 PM

View Postr.stiltskin, on 19 November 2010 - 03:42 PM, said:

Maybe you're just using the term "inheritance" inappropriately, because I don't see how inheritance has any application here. Your addressBook may contain objects of personType, but an addressBook should not be an object of personType (or an object derived from personType).

Inheritance applies when you create a base class, say personType, with certain member variables (data) and member functions, and then you create a derived class, say studentType, which inherits all of those members from personType and adds some additional "special" members that studentType objects need but which are not needed by all personType objects.

So getting back to your original question, and thinking some more about your addressBook application ...
I don't know the specific requirements of your assignment, but I think I would make an addressBook class which contains a (private) linked list of personType objects and some (public) functions to manipulate the list (add entries, delete entries, print, etc). My personType object would contain strings for firstname and lastname, functions to construct and print them, etc., and variables to hold any other relevant attributes of a personType -- including address, so if you must have a separate addressType class, I would then have an addressType object as a data member of the personType class.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "addressBookType.h"


void addressBookType::print()const{ // inherited print to print all information
    char choice;
	cout << endl;
	cout << "Print by relation - family, friend, or business? y/n "; //check user if print only by relation
	cin >> choice;
	if(choice == 'y' || choice == 'Y'){
		cout << extPersonType::getRelation() << endl;
	personType::print();
	addressType::print();
	dateType::print();
	}

	if(choice == 'n' || choice == 'N'){
	personType::print();
	addressType::print();
	extPersonType::print();
	dateType::print();
	}
	
}

addressBookType::addressBookType(string first,string last,string relation, string phone,
								 string streetAdd,string cityAdd,string stateAdd,string zipAdd,
								 int month, int day,int year,int passTotalEntry){
									 totalEntry = passTotalEntry;
									
}

void addressBookType::searchBday(int month){//ask for user input month of birthday if already entered. prints birthdays in those months
	int sMonth;
	cout << "Enter month to search birthdays : \n";
	cin >> sMonth;
	if(sMonth == dateType::getdMonth()){
		cout << "Bithdays found : " << sMonth << endl;
	personType::print();
	dateType::print();
	}
	else
		cout << "That birthday date not found..." << endl;
}

void addressBookType::searchName(string first,string last){ // ask for user input last name to see if it has already been entered,
	  string sName;                                     // and prints all information on that last name
	  cout << "Enter last name to search for: ";
	  getline(cin,sName);
	  if(sName == personType::getLastName()){
		cout << "\nFound : " <<  sName << "\n" << endl;
		addressType::print();
		dateType::print();
		extPersonType::print();
		}
	  else
		  cout << "Name not found..." << endl;
}
void addressBookType::addEntry(string first,string last,string relation, string phone, string streetAdd, //function being passed all inherited
		string cityAdd,string stateAdd,string zipAdd,int month, int day,int year){// information to be added to a new entry

			entry[totalEntry] = new addressBookType(first,last,relation,phone,streetAdd, // addressBookType object being passed all 
		 cityAdd,stateAdd,zipAdd,month,day,year);                                // inherited information
			totalEntry++; // counter for <= 500 value set in header file
}

			
_____________________________________________________________________________________________________
main

/*This program uses inheritance to build an address book. The program uses base class addressBookType, to inherit derived classes personType,
dateType, addressType, extPersonType.  It handles user input for  address book, and a "MyTxt.txt" text file located in the program
folder that prints out every entry entered.
Eric Graham
11-18-2010*/

#include <fstream>
#include <iostream>
#include <string>
#include "personType.h"
#include "extPersonType.h"
#include "addressType.h"
#include "dateType.h"
#include "addressBookType.h"

int main(){

		ofstream file;
		file.open("MyTxT.txt");
	
		cout << "******************    Welcome to your address book.    ***********************\n" << endl;
		int month = 0,
			day = 0,
			year = 0,
			input = 0;
	    char choice=0;	             
	    string first,last,street,city,state,zip,relation,phone;
		addressBookType adbt;
		
			cout << "1. Make address book entry." << endl;
			cout << "2. Search by last name." << endl;
			cout << "3. Search birthday by month." << endl;
			cout << "4. Print." << endl;
			cout << "5. Exit." << endl;

		while (choice != 4){
			cout << "\nEnter your choice: ";
			cin >> choice;
			cin.ignore();
			cin.clear();
			
		switch(choice){
		case '1':
			cout << "Enter the first name: ";
			getline(cin,first);
            cout << "Enter the last name: ";
			getline(cin,last);
			file << first << " " << last << endl;
			adbt.setName(first,last);

			cout << "Enter street address: ";
			getline(cin,street);
			cout << "Enter city: ";
			getline(cin, city);
			cout << "Enter state: ";
			getline(cin, state);
			cout << "Enter zip: ";
			getline(cin, zip);
			file << street << "\n" << city << " , " << state << " " << zip << endl;
			adbt.setAddress(street,city,state,zip);

			cout << "Enter relation (Family,Friend,Business): ";
			getline(cin,relation);
			cout << "Enter phone number: ";
			getline(cin,phone);
			file << relation << " - " << phone << endl;
			adbt.setRelation(relation);
			adbt.setPhone(phone);

			cout << "Enter birthday month(MM):";
			cin >> month;
			cout << "Enter birthday day(DD):";
			cin >> day;
			cout << "Enter birthday year (YYYY):";
			cin >> year;
			file << month << "-" << day << "-" << year << endl;
			cout << "(saved to file.)" << endl;
			adbt.setDate(month,day,year);
			file << "\n";
			break;
		case '2':
			adbt.searchName(first,last);
			  break;
		case '3':
			adbt.searchBday(month);
			  break;
		case '4':
			  adbt.print();
			  break;
		case '5':
			  exit(0);
			  break;
		default:
			cout << "Non-valid entry. Please enter valid entry." << endl;
		}
		cin.clear();
	}
			file.close();
cin.sync();  
cin.peek();  
return 0;
};



the inheritance in the original project before the linked list. the program should be written to use linked list to load all the user input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1