Question

I have a node.h and node.cpp file. The goal is to combine the code using a...

I have a node.h and node.cpp file. The goal is to combine the code using a template, and put it all into the .h file. So how do I use a template? Here are files. My teacher did a poor job in explaining how to do this properly. Thanks for the help! I should tell you that "Event' is another class that stores events.

//Node.h

#ifndef Node_h
#define Node_h
#include "Event.h"

class Node
{
public:
   //Declare a variable for node data.
   Event nData;

   //Declare a pointer for next node.
   Node* nNext;

   //Default Constructor.
   Node();

   //Custom Constructor 1.
   Node(Event inData);
   //Custom Constructor 2.
   Node(Event inData, Node* inNextNode);

   //Destructor.
   ~Node();
};
#endif

//Node.cpp

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

//Default constructor.
Node::Node()
{
   nNext = NULL; //Assign next pointer as null.
}

//Custom Constructor 1.
Node::Node(Event inData)
{
   //Initialize inData as iData.
   nData = inData;
   //Initialize null as nNext.
   nNext = NULL;
}
//Custom Constructor 2.
Node::Node(Event inData, Node* inNextNode)
{
   //Assign inData as nData.
   nData = inData;
   //Assign inNextNode as nodeNext.
   nNext = inNextNode;
}

//Definition for destructor.
Node::~Node() {}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Node.h : Defining a templated Node class

#ifndef Node_h
#define Node_h

template <class type> // define a template class for the data of Node class
class Node
{
public:
//Declare a variable for node data.
type nData;

//Declare a pointer for next node.
Node<type> *nNext;

//Default Constructor.
Node();

//Custom Constructor 1.
Node(type inData);
//Custom Constructor 2.
Node(type inData, Node<type> *inNextNode);

//Destructor.
~Node();
};


template <class type>
//Default constructor.
Node<type>::Node()
{
nNext = NULL; //Assign next pointer as null.
}


//Custom Constructor 1.
template <class type>
Node<type>::Node(type inData)
{
//Initialize inData as iData.
nData = inData;
//Initialize null as nNext.
nNext = NULL;
}

//Custom Constructor 2.
template <class type>
Node<type>::Node(type inData, Node<type> *inNextNode)
{
//Assign inData as nData.
nData = inData;
//Assign inNextNode as nodeNext.
nNext = inNextNode;
}

//Definition for destructor.
template <class type>
Node<type>::~Node() {}

#endif

Add a comment
Know the answer?
Add Answer to:
I have a node.h and node.cpp file. The goal is to combine the code using a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H...

    ~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H #define DECAYLIST_H #include <iostream> #include "Node.h" using namespace std; const int NUM_CONSECUTIVE = 3; class DecayList{ public:   // Constructor   // Preconditions: None   // Postconditions: New list is created   DecayList(); and here is the node class.   // Destructor   // Preconditions: None   // Postconditions: Dynamically allocated memory freed   ~DecayList(); #include <iostream> #include "Node.h" using namespace std; Node::Node(){     m_next = NULL;     m_value = NULL; } Node::~Node(){     //delete...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • C++ I have failed to call the insert and display. I am recycling code but cannot...

    C++ I have failed to call the insert and display. I am recycling code but cannot figure out how to insert a node at the beginning and display it. Please help #ifndef LINKEDLIST_H_INCLUDED #define LINKEDLIST_H_INCLUDED #include<iostream> #include<stdlib.h> #include<stdexcept> using namespace std; template <class T> struct Node { T data; Node<T> *Next, *Previous; }; template <class T> class Linkedlist { private: Node<T> *First; Node<T> *Last; Node<T> *TempPrevious; Node<T> *TempNext; Node<T> *P; int length; public: Linkedlist() { First = NULL; Last =...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO...

    C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO KNOW HOW I WOULD WRITE IT WITH A TEMPLATE. PLEASE AND THANKS IN ADVANCE. **************LinkedList.h requirements************************ linked list Class EXTRA CREDIT OPPORTUNITY: Create the LinkedList class as a template class for 5 extra credit points!!! private memebers Create a structure called ListNode, which should hold a Dinosaur and a pointer to the next ListNode ListNode pointer called head – will eventually point to the...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT