#ifndef H_NodeType #define H_NodeType #include <iostream> using namespace std; t
ID: 3531198 • Letter: #
Question
#ifndef H_NodeType
#define H_NodeType
#include <iostream>
using namespace std;
template <class Type>
class nodeType
{
public:
const nodeType<Type>& operator=(const nodeType<Type>&);
//Overload the assignment operator.
void setInfo(const Type& elem);
//Function to set the info of the node.
//Postcondition: info = elem;
Type getInfo() const;
//Function to return the info of the node.
//Postcondition: The value of info is returned.
void setLink(nodeType<Type> *ptr);
//Function to set the link of the node.
//Postcondition: link = ptr;
nodeType<Type>* getLink() const;
//Function to return the link of the node.
//Postcondition: The value of link is returned.
nodeType();
//Default constructor
//Postcondition: link = NULL;
nodeType(const Type& elem, nodeType<Type> *ptr);
//Constructor with parameters
//Sets info point to the object elem points to and
//link is set to point to the object ptr points to.
//Postcondition: info = elem; link = ptr
nodeType(const nodeType<Type> &otherNode);
//Copy constructor
~nodeType();
//Destructor
private:
Type info;
nodeType<Type> *link;
};
// You continue from here
Write the definitions of the member functions of the class nodeType. Also, write a program to test your class
Explanation / Answer
#ifndef H_NodeType
#define H_NodeType
#include <iostream>
using namespace std;
template <class Type> class nodeType
{
public:
//Overload the assignment operator.
const nodeType<Type>& operator=(const nodeType<Type>& m)
{
if(this==&m)
return *this;
else
{
this->~nodeType();
info=m.info;
link=m.link;
}
}
//Function to set the info of the node.
//Postcondition: info = elem;
void setInfo(const Type& elem)
{
info=elem;
}
//Function to return the info of the node.
//Postcondition: The value of info is returned.
Type getInfo() const
{
return info;
}
//Function to set the link of the node.
//Postcondition: link = ptr;
void setLink(nodeType<Type> *ptr)
{
link=ptr;
}
//Function to return the link of the node.
//Postcondition: The value of link is returned.
nodeType<Type>* getLink() const
{
return link;
}
//Default constructor
//Postcondition: link = NULL;
nodeType(Type value=0):link(NULL),info(value)
{
}
//Constructor with parameters
//Sets info point to the object elem points to and
//link is set to point to the object ptr points to.
//Postcondition: info = elem; link = ptr
nodeType(const Type& elem,nodeType<Type>*ptr)
{
info=elem;
link=ptr;
}
//Copy constructor
nodeType(const nodeType<Type> &otherNode)
{
info=otherNode.info;
link=otherNode.link;
}
void display()
{
cout<<info<<" ";
if(link==NULL)
std::cout<<std::endl;
else
{
link->display();
}
}
~nodeType()
{
}
//Destructor
private:
Type info;
nodeType<Type> *link;
};
#endif
int main(){
nodeType<int> a(3),b(4),c(5);
a.display();
b.display();
c.display();
a.setLink(&b);
a.display();
a.setInfo(9);
a.display();
int k=a.getInfo();
cout<<endl<<k<<endl;
nodeType<int> d(6,&a);
d.display();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.