There are a few errors in this code (C++). I tried to debug as best I could. Can
ID: 3850036 • Letter: T
Question
There are a few errors in this code (C++). I tried to debug as best I could.
Can someone help me figure this out?
#include<iostream>
using namespace std;
template<class T>
class Pair
{
T F;
T S;
public:
Pair();
Pair(T x,T y);
T getFirst();
T getSecond();
void setFirst(T x);
void setSecond(T y);
void disp();
};
template<class T>
Pair<T>::Pair()
{
}
template<class T>
Pair<T>::Pair(T x,T y)
{
F=x;
S=y;
}
template<class T>
T Pair<T>::getFirst()
{
return F;
}
template<class T>
T Pair<T>::getSecond()
{
return S;
}
template<class T>
void Pair<T>::setFirst(T x)
{
F=x;
}
template<class T>
void Pair<T>::setSecond(T y)
{
S=y;
}
template<class T>
void Pair<T>::disp()
{
cout<<F<<" "<<S<<endl;
}
template <class T>
struct linklstNode
{
T value;
linklstNode<T> *next;
};
template <class T>
class LinkedList
{
linklstNode<T> *start;
public:
LinkedList()
{
start=null;
}
linklstNode<T>* getFront()
{
return start;
}
void addFront(T val)
{
linklstNode<T>* newNod;
newNod->value=val;
newNod->next=NULL:
if(start==null)
start=newNod;
else
{
newNod->next=start;
start=newNod;
}
}
void addBack(T val)
{
linklstNode<T>* newNod;
newNod->value=val;
newNod->next=NULL:
if(start==null)
start=newNod;
else
{
linklstNode<T>* temp=front;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newNod;
}
}
};
template <class T>
class pairList: public Pair<T>,public LinkedList<T>
{
public:
pairList()
{
}
void insertPair(Pair p)
{
LinkedList::addFront(p);
}
void display()
{
linklstNode<T>* temp=LinkedList::getFront();
while(temp)
{
cout<<Pair::disp();
}
}
};
int main()
{
pairList<int> P;
Pair<int> k(12,15);
Pair<int> l(24,25);
P.insertPair(k);
P.insertPair(l);
P.display();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
template<class T>
class Pair
{
T F;
T S;
public:
Pair();
Pair(T x,T y);
T getFirst();
T getSecond();
void setFirst(T x);
void setSecond(T y);
void disp();
};
template<class T>
Pair<T>::Pair()
{
}
template<class T>
Pair<T>::Pair(T x,T y)
{
F=x;
S=y;
}
template<class T>
T Pair<T>::getFirst()
{
return F;
}
template<class T>
T Pair<T>::getSecond()
{
return S;
}
template<class T>
void Pair<T>::setFirst(T x)
{
F=x;
}
template<class T>
void Pair<T>::setSecond(T y)
{
S=y;
}
template<class T>
void Pair<T>::disp()
{
cout<<F<<" "<<S<<endl;
}
template <class T>
struct linklstNode
{
T value;
linklstNode<T> *next;
};
template <class T>
class LinkedList
{
linklstNode<T> *start;
public:
LinkedList()
{
start=null;
}
linklstNode<T>* getFront()
{
return start;
}
void addFront(T val)
{
linklstNode<T>* newNod;
newNod->value=val;
newNod->next=NULL:
if(start==null)
start=newNod;
else
{
newNod->next=start;
start=newNod;
}
}
void addBack(T val)
{
linklstNode<T>* newNod;
newNod->value=val;
newNod->next=NULL:
if(start==null)
start=newNod;
else
{
linklstNode<T>* temp=front;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newNod;
}
}
};
template <class T>
class pairList: public Pair<T>,public LinkedList<T>
{
public:
pairList()
{
}
void insertPair(Pair p)
{
LinkedList::addFront(p);
}
void display()
{
linklstNode<T>* temp=LinkedList::getFront();
while(temp)
{
cout<<Pair::disp();
}
}
};
int main()
{
pairList<int> P;
Pair<int> k(12,15);
Pair<int> l(24,25);
P.insertPair(k);
P.insertPair(l);
P.display();
system("pause");
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
template<class T>
class Pair
{
T F;
T S;
public:
Pair();
Pair(T x,T y);
T getFirst();
T getSecond();
void setFirst(T x);
void setSecond(T y);
void disp();
};
template<class T>
Pair<T>::Pair()
{
}
template<class T>
Pair<T>::Pair(T x,T y)
{
F=x;
S=y;
}
template<class T>
T Pair<T>::getFirst()
{
return F;
}
template<class T>
T Pair<T>::getSecond()
{
return S;
}
template<class T>
void Pair<T>::setFirst(T x)
{
F=x;
}
template<class T>
void Pair<T>::setSecond(T y)
{
S=y;
}
template<class T>
void Pair<T>::disp()
{
cout<<F<<" "<<S<<endl;
}
template <class T>
struct linklstNode
{
T value;
linklstNode<T> *next;
};
template <class T>
class LinkedList
{
linklstNode<T> *start;
public:
LinkedList()
{
start=NULL; //NULL in caps
}
linklstNode<T>* getFront()
{
return start;
}
void addFront(T val)
{
linklstNode<T>* newNod = new linklstNode<T>(); //alocate memory
newNod->value=val;
newNod->next=NULL; // Use semicolon not colon
if(start==NULL) //NULL in caps
start=newNod;
else
{
newNod->next=start;
start=newNod;
}
}
void addBack(T val)
{
linklstNode<T>* newNod;
newNod->value=val;
newNod->next=NULL; // Use semicolon not colon
if(start==NULL) //use NULL in caps
start=newNod;
else
{
linklstNode<T>* temp=start; //we must use start not front
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newNod;
}
}
};
template <class T>
class pairList //no super class
{
private:
LinkedList<Pair<T> > *ll; //linkedlist object pointer is required. We dont need super class
public:
pairList()
{
ll = new LinkedList<Pair<T> > ();
}
void insertPair(Pair<T> p) //add params <T>
{
ll->addFront(p); //add params <T>
}
void display()
{
linklstNode<Pair<T> >* temp = ll->getFront();
while(temp)
{
(temp->value).disp(); //cout not required here and use param<T> with Pair
temp = temp->next; //temp must shift to next node
}
}
};
int main()
{
pairList<int> P;
Pair<int> k(12,15);
Pair<int> l(24,25);
P.insertPair(k);
P.insertPair(l);
P.display();
// system("pause"); //not of much use
return 0;
}
Here you go champ....I have worked for more than an hour to find errors and integrate the code for you. I have commented where ever I made changes. Please go through the code, you wont face any trouble. I hope you will like the code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.