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?

Guess:

char init[100] = {1,2,3,4};
   CharPair cp(5, init);

You can't do this:

CharPair::CharPair ( int sz , char test [ sz ] )

alright cool. so i added this to my code:

char init [ 10 ] ;
    CharPair c ( 10 , init ) ;

    for ( int j = 0 ; j < 10 ; j++ )
        cout << c [ j ] << endl ;

and now I get this output:

Ñ

u

§


B
10
A
B
Illegal index value.
Press any key to continue . . .

Your initializer array should be initialized with valid data.

but i want to initialize it with the data stored in myArray

Chicken and egg.

If you are constructing an object containing myArray and you need to initialize myArray from something before you can copy its contents to another array...

Huh?