Multiple Classes

I need to implement the functions from the header file (2nd piece of code), using a string class (1st piece of code). I'm a little lost on how to start. The 3rd piece of code is my attempt so far. Any direction would be appreciated.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//TESTING PROGRAM

#include<iostream>
#include "String1030.h"

using std::cout;
using std::cin;

int main()
{
  // check the constructors 
  String1030 s("My string");
  String1030 t(s);
  String1030 x;

  char in_buf[256];


  cout << "S size(): " << s.getSize() << endl;
  cout << "T size(): " << t.getSize() << endl;
  cout << "X size(): " << x.getSize() << endl;

  for(int i=0;i<t.getSize();i++)
    cout << t[i];
  cout << endl;

  s[2]='5';

  for(int i=0;i<s.getSize();i++)
    cout << s[i];
  cout << endl;

  // check the assignment operator
  x=s;
  cout << "X: " << x.getString() << endl;

  // check the size reset.
  x.setSize(30);

  cin >> in_buf;
  x.setString(in_buf);
  cout <<  "\nx: " << x.getString() << endl;


  //more checks on resize

  //set to a negative value, nothing should change
  s.setSize(-8);
  cout << "S size(): " << s.getSize() << endl;

  //set to 0, should be 0
  s.setSize(0);
  cout << "S size(): " << s.getSize() << endl;

  //read into the 0 length array should NOT have an error
  //and should NOT transfer any characters. Output should not
  //have any errors either.
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //reset to something larger than 0
  s.setSize(10);
  cout << "S size(): " << s.getSize() << endl;

  //read should work now
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //now the assignment return value

  x=t=s;

  cout << "T: " << t.getString() << endl;
  cout << "X: " << x.getString() << endl;


  return 0;

}


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//HEADER FILE

#ifndef STRING1030_H
#define STRING1030_H

#include<iostream>

using std::ostream;
using std::istream;
using std::endl;
using std::cerr;

class String1030
{

  public:
      // The constructor. The "0" is the digit 0 NOT a 
      // character. It is used to let us know that 
      // nothing is being passed to the constructor.
    String1030(const char *buf=0);
      //This next is a "copy" constructor. Remember that
      //we have to create new storage and then copy
      //the array content. We must not just copy the pointer.
    String1030(const String1030& oldstring);
      // The destructor because we are allocating memory.
      // We must deallocate it when the object goes out of
      // scope (is destroyed).
    ~String1030();

    String1030& operator=(const String1030& right);

      // Allows to change the element at a certain index.
    char& operator[](int index);

    int getSize(void);
    void setSize(int newsize);
    const char *getString();
      // Replace the existing string with a new one. 
    void setString(const char *carray);


  private:
    char *buffer;
    int mysize;

};


#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//ACTUAL IMPLEMENTATION

#include<iostream>
#include "String1030.h"

int getSize()
{
  String1030 myString;
  int i = 0;

  while (myString[i])
  {
    i++;
  }
  return i;
}

void setSize(int newSize)
{
  String1030 myString;

  myString[newSize];
}
You may want to take a look through the code snippets in this:
http://www.cplusplus.com/doc/tutorial/classes/

In particular, notice how implementing a declared member function is done. Those implementations are what needs to go in your implementation file, one for each member function/constructor/destructor.

If you need any more help, let us know. :)

-Albatross

P.S. - You're underthinking your setSize function and overthinking your getSize function.
Last edited on
So my functions in the implementation file will start like this: int String1030::getSize(){} ? As for the overthinking...is there a way I can get the length without a loop? I'm not allowed to use the STL string class.
Yes, and yes. After all, you're storing the length of your string, aren't you?

int mysize;

-Albatross
Last edited on
Topic archived. No new replies allowed.