Need help figuring out Overloading constructors. (Very lost)

In my CS class we just learned about overloading constructors and we're up to writing out first program using them however; I am VERY confused. We're writing a program that is putting characters into an array then is manipulating them in different ways.

Most of the client program has been provided for us but the part I'm stuck on is overloading the constructors and writing the functions in the class. I'm still pretty new to classes but for the most part I know have the syntax figured out but when it comes to overloading constructors I'm extremely lost.

I think if I can get some help with the first few functions that I can figure out the rest myself (that's pretty much the way I've learned everything in C++).

A Bag is representative of an important category of data structures – namely collections (of ‘things’) also referred to as containers. One of the distinguishing features of a Bag is that it can contain duplicate values.

...

For this project you will create a class to implement a variation of the Bag ADT. For this assignment, a Bag will refer to a homogeneous collection of AlphaNumeric char



Here's part of the client program (prog5.cpp).
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
// lines that begin with // ***    are "real" comments
// lines with just //  are for you to uncomment as you implement the functions
 
#include "bag.h"
 
int main (int argc, const char * argv[])
{
    cout << "Welcome to the bag operator driver" << endl;

// *** Create two bags.
    Bag two("abbcccdddd1");

    Bag one;

// *** testing << with cout.
  // cout << "******* cout test: \n";
  // cout << one << two;
    
// *** testing + to add single chars to a bag
  //  one = one + 'A';
  //  one = one + 'B';
  //  one = one + 'C';
    cout << "******* single char addition test: \n";
  //  cout << one ;
    
// *** testing + to add two bags together
//    one = one + two;
    cout << "******* *** testing + to add two bags \n";
//    cout << one ; 



Here's what I've got for the class definitions (bag.h):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
 
using namespace std;
 
class Bag {
 
public:
    Bag(string);
    Bag operator+( char addchar);  //add a char to the array
    // Bag operator+( char addchar);//add two arrays together
private:
    char contents[1024];
    int num;
    void compact();
    bool hasA(char A);	//determines if the char is in the array (somewhere)
};


And finally, here's where I need most of my assistance. The class functions (bag.cpp):
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
82
83
84
85
86
#include "bag.h"
 
/*  Constructor, secondary.
    fill a bag up with the characters from a string.
*/ 
Bag::Bag(string source) {
    for (int i=0; i < (int)source.length(); i++)
        contents[i]=source[i];
    num = (int)source.length();
}

/*
  isAlNum  determines if a character is alphanumeric.
  called from Bag:compact
*/
bool isAlNum( char X) { 
    if ( X >= 'A' && X <= 'Z') return true;
    if ( X >= 'a' && X <= 'z') return true;
    if ( X >= '0' && X <= '9') return true;
    
    return false;
}
 
/*  compact the Bag's internal array by removing all non alphnumeric chars
    up to position num 
*/
void Bag::compact() {
   int last = num-1;
    for (int i=num-1; i>=0; i--) {
        if ( !isAlNum( contents[i] ) )  {
             contents[i] = contents[last];
             last--;
        }
    num = last+1;
    
  }
}

bool Bag::hasA (char X)
{
	int i = 0;
	
	for (i = 0; i < 1024; i++)
		{	
			if (contents[i] == X)
				return true;
		}


	return false;
}


Bag Bag::operator+( char addchar)
{  
	new Bag;
	int i = 0;
	int count = 0;

    for (i = 0; i < 1024; i++)
		i = count;

	[count++] = addchar;

    return Bag;      //Return copy of local variable.
                                //Do not return a reference to a 
						    //local variable – always an error
}



/*
Bag &Bag::operator+( string Bag )
{

int i = 0;
for (i = 0; i < 1024; i++)
	Bag one [i] == tempbag [i];

int j;
for (j = 0; j < 1024; j++)
	Bag one [i] == Bag two [j];
return Bag one;
}
*/
	





Last edited on
"Overloading" is a C++ term to mean that two functions (or methods) that take different argument lists have the same name. For example:

1
2
3
4
5
6
7
8
9
void hello()
  {
  cout << "Hello world!\n";
  }

void hello( const string& name )
  {
  cout << "Hello " << name << "!\n";
  }

Both of these functions share the same name. Keep in mind that they are different functions.

A constructor is just a special method (class function) that initializes a class's data members. For example, if I had a two-dimensional point, I might add a couple of different constructors. (The constructors are the functions that have the same name as the class.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class point
  {
  public:
    int x, y;

    // First (default) constructor: takes no arguments, just set x and y to zero
    point()
      {
      x = 0;
      y = 0;
      }

    // Second (overloaded) constructor: set x and y as given.
    point( int xx, int yy )
      {
      x = xx;
      y = yy;
      }
  };
Now the code can have a valid point either way:
1
2
3
4
5
point pt1;
cout << pt1.x << ", " << pt1.y << "\n";

point pt2( 12, -7 );
cout << pt2.x << ", " << pt2.y << "\n";

0, 0


12, -7

Your professor wants you to do just that.

Hope this helps.
That does help, thank you.

But I'm also having a lot of trouble with things like this Bag Bag::operator+( char addchar)

I don't even know if that's correct but I were writing code that looks similar to that.

Also, I think were supposed to use something called a *this pointer? Which I also need clarification on. I'm going to talk to my instructor tomorrow to hopefully get some guidance.
Topic archived. No new replies allowed.