You are here:

C++/definition of a Class

Advertisement


Question
QUESTION: Hello,

coould you help me out with this problem by partially giving functions to be included in the class Dictionary. please, dont define the functions, just declare them all plus what values should be in the public and private parts of the class.

question:

Write a class Dictionary to define a very simple dictionary. A dictionary should consist of a number of entries (array). Each entry consists of two parts, a reference word and an explanatory text. Use a extra help class Entry to define this. For class Dictionary there should be a member function with which new entries can be added. (The array does not have to be sorted.) There should be an indexing operator [] that gets a text as parameter. This text should be a reference word. The operator should look up the reference word in the dictionary and as result give a pointer to a corresponding explanatory text(or 0 if the reference word is not present).


ANSWER: something like this, perhaps.

struct entry
{
  entry( const char* wd, const char* expl ) ; // constructor

  enum { WORD_SZ = 20, EXPL_SZ = 200 } ;

  char word[ WORD_SZ ] ;
  char explanation[ EXPL_SZ ] ;
};

class dictionary
{
   public :
     dictionary() ;

     void add_entry( const char* wd, const char* expl ) ; // add an entry
     const char* operator[] ( const char* wd ) const ; // look up the word

   private :

     enum { MAX_ENTRIES = 1024 } ;

     entry entries[ MAX_ENTRIES ] ;
     int num_entries ;
};





---------- FOLLOW-UP ----------

QUESTION: Hello,

I thought it's C++, why then do you have a structure?

struct entry
{
 entry( const char* wd, const char* expl ) ; // constructor

 enum { WORD_SZ = 20, EXPL_SZ = 200 } ;

 char word[ WORD_SZ ] ;
 char explanation[ EXPL_SZ ] ;
};

dont understand now why you have the struct entry, cuz it's suppose to be a class only in C++

ANSWER: A struct and class are synonyms in C++, they differ only in the default access specifier.

from the ISO standard for C++:
"A structure is a class defined with the class-key struct; its members and base classes are public by default" - ISO/IEC 14882 : 1998 (E) clause 9/4



---------- FOLLOW-UP ----------

QUESTION: Hi, so, i could use constructors in structures as well but there is no access specifier as all functions and members are public right?

ok , there is one other object   int num_entries, i dont understand how this fit into the whole picture. could i then ask you to set up a small main programme on how to use this. leave the member function definition to me. i will try to write them first but if i am stuck, might get back to you. thanks

Answer
You can have constructors as well as access specifiers (you can have anything that you could have in a class) in a struct. The following are equivalent:

class A
{
  public: // in a class, the default access specifier is private
     A( int i ) : value(i) {}

  private:
     int value ;
};

struct A
{
  //public: // in a struct, the default access specifier is public
     A( int i ) : value(i) {}

  private:
     int value ;
};

An object of type dictionary can hold a maximum of MAX_ENTRIES entries.
num_entries is there to keep track of the actual number ( <= MAX_ENTRIES ) of entries that have been made so far.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.