5 Replies - 1386 Views - Last Post: 13 March 2009 - 03:52 PM Rate Topic: -----

#1 Bucket  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-March 08

Composition Help!

Post icon  Posted 12 March 2009 - 04:32 PM

EmployeeClass.h
#include <string>
#include <fstream>

using namespace std;

class EmployeeClass	// all classes are private unless specified otherwise 
{																 
	private:
		
		string name;
		string job;
	
	public:
		string getName();
		string getJob();
		void setName(string);
		void setJob(string);
		void addEmployee();
		void print();
};
		
 

EmployeeClass.cpp
#include "EmployeeClass.h"
#include <iostream>

using namespace std;


void EmployeeClass::setName(string enterName) // :: scope resolution operator!
{
	name = enterName;
} 
void EmployeeClass::setJob(string enterJob)
{
	job = enterJob;
}
string EmployeeClass::getName()
{
	return name;
}

string EmployeeClass::getJob()	
{
	return job;
}  

void EmployeeClass::print()
{
	cout << "Name: " << name << endl;
	cout << "Job: " << job << endl;
}

void EmployeeClass::addEmployee()
{
	string employeeName;
	string employeeJob;
	
	cout << "Enter Name: ";
	getline (cin, employeeName);
	
	cout << endl;
	
	cout << "Enter job: ";
	getline (cin, employeeJob);
	
	
	setName(employeeName);
	setJob(employeeJob);
	
}
	

Node.h
#include <iostream>
#include "EmployeeClass.h"

using namespace std;

class NodeClass
{ 
	private:
		struct Node
			{
				EmployeeClass EmployeeNode;
				Node *link;
			};
		
	public:
		
		void addNode();
		void removeNode();
		void printNode();
		
};


Node.cpp
#include <iostream>
#include "Node.h"


using namespace std;

void NodeClass::addNode()
{
	Node *newNode;
	newNode = new Node;
	
	newNode->EmployeeNode.addEmployee();
	
	
	Node = newNode; // <--- compile error
}

void NodeClass::printNode()
{
	Node->EmployeeClass.print();
}



expected primary-expression before '->' token

I baffled on what it's asking me.

I can do this on main using functional programming. But I'm learning Object now, composition, right now.
Later on, I will try to make another Class, list . Thus, making a linked list. I'm probably going to require help later on.

Is This A Good Question/Topic? 0
  • +

Replies To: Composition Help!

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Composition Help!

Posted 12 March 2009 - 04:42 PM

Node is a type. You need to assign to a variable.
Was This Post Helpful? 1
  • +
  • -

#3 Bot-Vector  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 08-January 09

Re: Composition Help!

Posted 12 March 2009 - 04:54 PM

Why are you trying to make a new pointer to an object when you already have done so in that function?

I haven't learned very much about linked lists, so I might be in the wrong here.

What are you trying to do with that statement?
Was This Post Helpful? 1
  • +
  • -

#4 Bucket  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-March 08

Re: Composition Help!

Posted 13 March 2009 - 01:19 PM

Thanks both of you!

Updated Node.h
#include <iostream>
#include "EmployeeClass.h"

using namespace std;

class NodeClass
{ 
	private:
		struct aNode
		{
			EmployeeClass EmployeeNode;
			NodeClass *link;
		};
		
		aNode *newNode;
		
	public:
		NodeClass();
		void addNode();
		void removeNode();
		void printNode();
		
};
 


Node.cpp
#include <iostream>
#include "Node.h"


using namespace std;

NodeClass::NodeClass()
{
	
}

void NodeClass::addNode()
{
	
	newNode = new aNode;
	
	newNode->EmployeeNode.addEmployee();
	
}

void NodeClass::printNode()
{
	newNode->EmployeeNode.print();
}

void NodeClass::removeNode()
{
	delete newNode;   
}



list.h
#include <iostream>
#include "Node.h"

using namespace std;

class List
{
	private:
		aNode head;
		aNode tail;
		aNode current;
	   
	public:
		void addToFrontOfList();


};




list.cpp
#include <iostream>
#include "List.h"

using namespace std;

void  addToFrontOfList()// sticks a Node it in front of the list
{
	if (head == NULL && tail == NULL)
	{
		head = newNode;
		tail = newNode;
	}


}





I'm a bit lost on how to create linked list now. I know node has an employee object, and list has to nodes.
I can do this using functional programming, but I have no clue on how to do it using objects.
I keep getting syntax errors.
Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Composition Help!

Posted 13 March 2009 - 01:25 PM

why not just use the STL List object? #include <list>
Was This Post Helpful? 1
  • +
  • -

#6 Bucket  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-March 08

Re: Composition Help!

Posted 13 March 2009 - 03:52 PM

I'm not that far in the book. but, I'll note that STL.


Besides, this is an exercise in encapsulation and composition.
And I can't seem to figure out how to do a list object yet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1