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
// 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, constchar * 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>
usingnamespace 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):
#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') returntrue;
if ( X >= 'a' && X <= 'z') returntrue;
if ( X >= '0' && X <= '9') returntrue;
returnfalse;
}
/* 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)
returntrue;
}
returnfalse;
}
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;
}
*/
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;
}
};
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.