Hey guys. I need to add a constructor that takes an int argument, sz, and an array of char of size sz. I then need the constructor to set the first sz members of the private dara array (myArray) to the sz members of the argument array of char.
Here's what I've got so far; I'm not sure how to call the object (i.e., CharPair ( 10 , x [ ] ) )
#pragma once
#include <cstdlib>
#include <iostream>
using namespace std ;
class CharPair
{
public:
CharPair ( ) ;
CharPair ( int ) ;
CharPair ( int , char [ ] ) ;
void outSize ( ) ;
char& operator[] ( int index ) ;
private:
char myArray [ 100 ] ;
int size ;
} ;[/code][/code][code][code=c++]#include "prob4.h"
CharPair::CharPair ( ) : size ( 10 )
{
for ( int i = 0 ; i < size ; i++ )
myArray [ i ] = '#' ;
}
CharPair::CharPair ( int sz )
{
for ( int i = 0 ; i < sz ; i++ )
myArray [ i ] = '#' ;
}
CharPair::CharPair ( int sz , char test [ sz ] )
{
for ( int i = 0 ; i < sz ; i++)
test [ i ] = myArray [ i ] ;
}
void CharPair::outSize ( )
{
cout << size << endl ;
}
char & CharPair::operator []( int index )
{
if ( index < size && index >= 0 )
return myArray [ index ] ;
else
{
cout << "Illegal index value.\n" ;
exit ( 1 ) ;
}
}[/code][/code][code][code=c++]#include "prob4.h"
int main ( )
{
CharPair a ;
CharPair b ( 12 ) ;
CharPair c ( 10 , ??? ) ;
//test data
a.outSize ( ) ;
a [ 1 ] = 'A' ;
a [ 2 ] = 'B' ;
cout << a [ 1 ] << endl ;
cout << a [ 2 ] << endl ;
for ( int i = 0 ; i < 12 ; i++ )
cout << b [ i ] << endl ;
return 0 ;
}
any help?