Inheritance??

Im implementing a template class
my class is tem.h my implementation is tem.template
but temp.h has include "node.h" that means that is being inherited right?

My questions:
How can I use functions from node.h into the implementation tem.template?
closed account (zb0S216C)
Based on your terminology, I think you're confusing file inclusion with OOP's inheritance. The difference is quite large. Here's an example of OOP inheritance:

1
2
3
4
5
6
7
8
9
10
11
12
13
// This is not inheritance:
#include "AFileName"

// This is inheritance:
struct base_class
{
    // ...
};

struct derived_class : base_class
{
    // "derived_class" has inherited from "base_class"
};

See here: http://www.cplusplus.com/doc/tutorial/inheritance/

Wazzak
ok I got it. But how am I supposed to use the class node.h just like any other class? Im trying it to use it inside tem.template.
thanks for you reply
closed account (zb0S216C)
You can include the said file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// AFileWith.SomeWackyExtension:
template <typename t>
struct node
{
    // ...
};

// AFile.h
#include "AFileWith.SomeWackyExtension"

template <typename t>
struct something : node<t>
{
    // ..
};

Or am I missing the point?

Wazzak
Last edited on
Not missing the point, I guess I am not being specific.


I have one class
tem.h inside has #include "node.h"

Iam implementing the class tem.h which is called tem.template because
I am using template.

Now on my constructor I think that I have to use one function from the class node.h but I am not sure how to use it.
Iam confused with the #include "node.h" because on the tem.template im implementing tem.h and also im trying to use functions from node.h

thanks
closed account (zb0S216C)
Can you post the code you're struggling with? You don't have to post all of it, just the relevant parts.

Wazzak

#ifndef NODE2_H
#define NODE2_H
#include <cstdlib> // Provides NULL and size_t
#include <iterator> // Provides iterator and forward_iterator_tag

namespace CISP430_A3
{
template <class Item>
class node
{
public:
// TYPEDEF
typedef Item value_type;
// CONSTRUCTOR
node(const Item& init_data=Item( ), node* init_link=NULL)
{ data_field = init_data; link_field = init_link; }
// MODIFICATION MEMBER FUNCTIONS
Item& data( ) { return data_field; }
node* link( ) { return link_field; }
void set_data(const Item& new_data) { data_field = new_data; }
void set_link(node* new_link) { link_field = new_link; }
// CONST MEMBER FUNCTIONS
const Item& data( ) const { return data_field; }
const node* link( ) const { return link_field; }
private:
Item data_field;
node *link_field;
};

// FUNCTIONS to manipulate a linked list:
template <class Item>
void list_clear(node<Item>*& head_ptr);

template <class Item>
void list_copy
(const node<Item>* source_ptr, node<Item>*& head_ptr, node<Item>*& tail_ptr);

template <class Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry);

template <class Item>
void list_head_remove(node<Item>*& head_ptr);

template <class Item>
void list_insert(node<Item>* previous_ptr, const Item& entry);

template <class Item>
size_t list_length(const node<Item>* head_ptr);

template <class NodePtr, class SizeType>
NodePtr list_locate(NodePtr head_ptr, SizeType position);

template <class Item>
void list_remove(node<Item>* previous_ptr);

template <class NodePtr, class Item>
NodePtr list_search(NodePtr head_ptr, const Item& target);

// FORWARD ITERATORS to step through the nodes of a linked list
// A node_iterator of can change the underlying linked list through the
// * operator, so it may not be used with a const node. The
// node_const_iterator cannot change the underlying linked list
// through the * operator, so it may be used with a const node.
// WARNING:
// This classes use std::iterator as its base class;
// Older compilers that do not support the std::iterator class can
// delete everything after the word iterator in the second line:

template <class Item>
class node_iterator
// : public std::iterator<std::forward_iterator_tag, Item>
{
public:
node_iterator(node<Item>* initial = NULL)
{ current = initial; }
Item& operator *( ) const
{ return current->data( ); }
node_iterator& operator ++( ) // Prefix ++
{
current = current->link( );
return *this;
}
node_iterator operator ++(int) // Postfix ++
{
node_iterator original(current);
current = current->link( );
return original;
}
bool operator ==(const node_iterator other) const
{ return current == other.current; }
bool operator !=(const node_iterator other) const
{ return current != other.current; }
private:
node<Item>* current;
};

template <class Item>
class const_node_iterator
// : public std::iterator<std::forward_iterator_tag, const Item>
{
public:
const_node_iterator(const node<Item>* initial = NULL)
{ current = initial; }
const Item& operator *( ) const
{ return current->data( ); }
const_node_iterator& operator ++( ) // Prefix ++
{
current = current->link( );
return *this;
}
const_node_iterator operator ++(int) // Postfix ++
{
const_node_iterator original(current);
current = current->link( );
return original;
}
bool operator ==(const const_node_iterator other) const
{ return current == other.current; }
bool operator !=(const const_node_iterator other) const
{ return current != other.current; }
private:
const node<Item>* current;
};

}

#include "node2.template"
#endif







#ifndef SEQUENCE4_H
#define SEQUENCE4_H
#include <cstdlib> // Provides size_t
#include "node2.h" // Provides node class

namespace CISP430_A3
{
template <class Item>
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef Item value_type;
typedef size_t size_type;

// CONSTRUCTORS and DESTRUCTOR
sequence( );
sequence(const sequence& source);
~sequence( );
// MODIFICATION MEMBER FUNCTIONS
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void operator =(const sequence& source);
void remove_current( );
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return many_nodes; }
bool is_item( ) const { return (cursor != NULL); }
value_type current( ) const;
private:
node<Item> *head_ptr;
node<Item> *tail_ptr;
node<Item> *cursor;
node<Item> *precursor;
size_type many_nodes; // keeps track of how many items are in a sequence
};
}

// The implementation of a template class must be included in its header file:
#include "sequence4.template"
#endif


#include <cstdlib> // Provides size_t
#include "node2.h" // Provides node class

namespace CISP430_A3 // sequence is now sequence<item> except constructor and destructor
{

template <class Item>
sequence<Item>::sequence()
{
*head_ptr = NULL;
*tail_ptr = NULL;
*cursor = NULL;
*precursor = NULL;
many_nodes = 0;
}

template <class Item>
sequence<Item>::sequence(const sequence<Item>& source)
{

}


template <class Item> //destructor
sequence<Item>::~sequence()
{
}

}



Im trying to use function of node.h inside the constructor

sequence<Item>::sequence(const sequence<Item>& source)
{

}
Im trying to use a function set_data from of node.h inside the constructor

sequence<Item>::sequence(const sequence<Item>& source)
{

}
You don't have to post all of it, just the relevant parts.
sigh.


Im trying to use a function set_data from of node.h inside the constructor
¿which node do you want to set?
Last edited on

sequence<Item>::sequence(const sequence<Item>& source)
{
set_data(source); // but I know that is wrong but you know im trying to set
// set_data to the source
}



make sense?
No.
¿What do you want to do?
I am trying to set the second constructor but im not sure how to do it.
I think that i have to use the function set_data from the class node.h
but i am not sure
Yes, I've got that. What I'm asking is what is supposed to happen there.
Im not sure.
I guess I going to skip that and keep going, maybe after implementing all the functions it will make more sense and I ll be able to implement the constructor
thanks
Topic archived. No new replies allowed.