Just a quick question regarding the use of class objects

For my homework I'm supposed to make a guessing game. The instructions say to use at least one class to access and build the tree, but then the instructions also say not to use class objects. I'm a little confused here, so say if I had this:
1
2
3
4
5
6
7
8
class Tree{
 //stuff here
};

int main()
{
    Tree myTree;
}


does that break the rules of not using class objects? Because if it does, I don't understand how I'm supposed to access stuff from the Tree class. Or am I overthinking the no class objects rule?
Last edited on
Your example has a class Tree and one object myTree. What exactly do the instructions say ?
"Write a class called CharacterTree that handles building and accessing your decision
tree. The only requirement of this class is that is uses some sort of linear or dynamic
array to store the tree itself. DO NOT USE STRUCTS OR CLASS OBJECTS."

And then suggestions for how to start include:

1
2
void CharacterTree::insert( string s, int index){}
string CharacterTree:retrieve( int index ){}


so if I need to use those in main(), I really need a Tree object, right? Like this:
myTree.retrieve(index);
If you create a object of type CharacterTree you would use a class object.
The only way I can think of is to use static functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class CharacterTree
{
public:
  static void insert(string s, int index);
  static string retrieve(int index);
};

void CharacterTree::insert(string s, int index)
{
  // your code here
}
string CharacterTree::retrieve(int index)
{
  // your code here
  return "";
}

int main()
{
  // now you can use it like 
  CharacterTree::insert("A", 1); // or whatever
  string data = CharacterTree::retrieve(1);
}
"The only requirement of this class is that is uses some sort of linear or dynamic
array to store the tree itself. DO NOT USE STRUCTS OR CLASS OBJECTS."

I read this as meaning that inside your class object, for storing the actual tree data, you are not to use any more classes. Just an array.
Last edited on
I read this as meaning that inside your class object, for storing the actual tree data, you are not to use any more classes. Just an array.

Likewise.

A tree is trivially implemented with "nodes". For example, a binary tree could use:
1
2
3
4
5
struct Node {
  std::string data;
  Node* left;
  Node* right;
};

This assignment says that you cannot implement class CharacterTree with such approach.
Ah! Thank you! That makes sense. I did start by using just an array, but reread the instructions and started to doubt my understanding.
Topic archived. No new replies allowed.