GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Feb-2009, 01:01
bobsta bobsta is offline
New Member
 
Join Date: Feb 2009
Posts: 5
bobsta is on a distinguished road

Initialising arrays with default values in derived class


Hi,

I am new to C++ having recently migrated from FORTRAN. I am trying to create multiple derived classes each containing various integer or float parameters and 2 arrays, let's call them X and Y, of different sizes (hence different derived classes). X and Y are private data members of the derived class. My problem is that I want to initialise each instance of these derived classes to have default values for XArray and Y, of which X will be overwritten later (using a member function) on the basis of values returned by an external function.
I hope this question is not too banal.

Thanks in anticipation

CPP / C++ / C Code:
//Class defintion for simple derived class FieldA
//Derived from Field
//FieldA has additional private member arrays
//to be initialised
#include <iostream>
using namespace std;

class Field
{
protected:
	int A;
	int B;
	int C;
	int D;
	double E;

public:
	// constructor
	Field();
	~Field();

	//accessors
	void SetC (int c) { C=c;}
	void SetD (int d) { D=d;}
	void SetE (double e) { E=e;}
	int GetA () const {return A;}
	int GetB () const {return B;}
	int GetC () const {return C;}
	int GetD () const {return D;}
	double GetE () const {return E;}
};

class FieldA: public Field
{
private:
	int Y[41]; //arrays
	int X[80]; 

public:
	//constructor and destructor
	// constructor initialises values and arrays
    FieldA():A(40),B(4),
		Y(-80,-76,-72,-68,-64,-60,-56,-52,-48,
		  -44,-40,-36,-32,-28,-24,-20,-16,-12,
		  -8,-4,0,4,8,12,16,20,24,28,32,36,34,
		   40,44,48,52,56,60,64,68,72,76,80),
		X(110,110,110,110,110,110,110,110,
		  110,110,110,110,110,110,110,110,
		  110,110,110,110,110,110,110,110,
		  50,50,50,50,50,50,50,50,
		  80,80,80,80,80,80,80,80,
		  90,90,90,90,90,90,90,90,
		  30,30,30,30,30,30,30,30,
		  110,110,110,110,110,110,110,110,
		  110,110,110,110,110,110,110,110,
		  110,110,110,110,110,110,110,110){};
	//destructor
	~FieldA(){};


};



int main()
{
	FieldA Test;
	cout << "A : " << Test.GetA() << endl;
    cout << "B : " << Test.GetB() << endl;
	
	Test.SetC(0);
	cout << "C : " << Test.GetC() << endl;

	Test.SetD(57);
    cout << "D : " << Test.GetD() << endl;

	Test.SetE(17.32);
	cout << "E : " << Test.GetE() << endl;

	return 0;
}
Last edited by LuciWiz : 11-Feb-2009 at 02:17. Reason: Please insert your C++ between [cpp] & ]/cpp] tags
  #2  
Old 11-Feb-2009, 05:59
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Initialising arrays with default values in derived class


Quote:
Originally Posted by bobsta
My problem is that I want to initialise each instance of these derived classes to have default values for XArray and Y, of which X will be overwritten later (using a member function) on the basis of values returned by an external function.

In your Field class, you should utilize its ctor to initialize it to a known good value or state. That is, your members should be initialized to at least zero, if applicable. In your case, they are all POD types.

CPP / C++ / C Code:
//Class defintion for simple derived class FieldA
//Derived from Field
//FieldA has additional private member arrays
//to be initialised

#include <iostream>
#include <cstdlib>

using namespace std;

class Field
{
protected:
    int A;
    int B;
    int C;
    int D;
    double E;
public:
    // constructor
    Field() : A(0), B(0), C(0), D(0), E(0){};
    virtual ~Field(){};
    //accessors
    void SetC (int c) { C=c;}
    void SetD (int d) { D=d;}
    void SetE (double e) { E=e;}
    int GetA () const {return A;}
    int GetB () const {return B;}
    int GetC () const {return C;}
    int GetD () const {return D;}
    double GetE () const {return E;}
};

class FieldA: public Field
{
private:
    int Y[41]; //arrays
    int X[80]; 
public:
    //constructor and destructor
    // constructor initialises values and arrays
    FieldA(){
	int aY[]={-80,-76,-72,-68,-64,-60,-56,-52,-48,
	  -44,-40,-36,-32,-28,-24,-20,-16,-12,
	  -8,-4,0,4,8,12,16,20,24,28,32,36,34,
	   40,44,48,52,56,60,64,68,72,76,80};
	memcpy(Y, aY, sizeof(Y)/sizeof(int));

	int aX[]={110,110,110,110,110,110,110,110,
	  110,110,110,110,110,110,110,110,
	  110,110,110,110,110,110,110,110,
	  50,50,50,50,50,50,50,50,
	  80,80,80,80,80,80,80,80,
	  90,90,90,90,90,90,90,90,
	  30,30,30,30,30,30,30,30,
	  110,110,110,110,110,110,110,110,
	  110,110,110,110,110,110,110,110,
	  110,110,110,110,110,110,110,110};
	memcpy(X, aX, sizeof(X)/sizeof(int));
	A = 40;
	B = 4;
    }
    //destructor
    virtual ~FieldA(){};
};

int main()
{
    FieldA Test;
    cout << "A : " << Test.GetA() << endl;
    cout << "B : " << Test.GetB() << endl;

    Test.SetC(0);
    cout << "C : " << Test.GetC() << endl;

    Test.SetD(57);
    cout << "D : " << Test.GetD() << endl;

    Test.SetE(17.32);
    cout << "E : " << Test.GetE() << endl;

    return 0;

}

...not exactly what you were attempting to do, but at least a working example of what I think it is that you are wanting to accomplish.

If this is some form of classroom assignment, I think that the goal here may be to demonstrate that you can't initialize inherited members or arrays in an "initialization list," though I may be wrong. A review of the standard may be a quick way to find out.


MxB
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get a handle to a derived class? Futterama MS Visual C++ / MFC Forum 1 29-Jan-2007 09:47
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Message Class TransformedBG C++ Forum 5 29-Nov-2006 21:28
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 15:11
a tester class and then some. postage Java Forum 1 06-May-2006 15:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:56.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.