linked list

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* 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?
1
2
3
4
5
6
7
8
9
10
11
private:
	string firstName;
	string lastName;
 
        struct node{
		node *link;
		*head

		{//end of struct node
public:


Or would struct be used sort of like a prototype in the since it goes before the header as such
1
2
3
4
5
6
7
8
9
10
11
12
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)
Last edited on
The only differences between class and struct are:
1) Default inheritance for class is private, for struct is public;
2) Default access for class is private, for struct is public;
3) struct is 6 keystrokes, class is 5.

One nests classes/structs when there is a logical relationship between them. Almost always when I do this it is because the nested type should be private within the class (ie, the user doesn't need to use it directly). If you are building a linked list class and you need a node class/struct, unless the user must directly use the node class (which they shouldn't), node should be declared in the protected or private section of the linked list class.
Topic archived. No new replies allowed.