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...

  • How do i write a destructor for this class? #ifndef NODE_H #define NODE_H #include <iostream> using...

    How do i write a destructor for this class? #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node{ public: // Constructor // Preconditions: None // Postconditions: None Node(); // Destructor // Preconditions: None // Postconditions: Frees dynamically allocated memory ~Node(); // Overloaded Constructor // Preconditions: None // Postconditions: Initializes member variable with given argument Node(bool value); // ReplaceValue - Replaces m_value with opposite (if true then false or if false then true) // Preconditions: None // Postconditions: m_value...

  • 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++ 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...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • 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...

  • 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 =...

  • // 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...

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

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