View Full Version: class help

C++ Learning Community > C++ Help > class help


Title: class help


dimaz_2kx - January 31, 2007 01:33 AM (GMT)
how do you declare this in a class

characters [] = {a,b,c,d,e,f,........z};

ive tried putting 8 n d constructor but I still get an error!

i hve a hrd tym finding class with this kind of example n d net!
tnx n advance!

Shonoby - January 31, 2007 03:12 AM (GMT)
Umm, well in a class definition u are not allowed to initialize a value of any type. So what u are doing is this:
CODE

class blah {
public:
blah();  // constructor

private:
char blah[] = { a, b, c, d... };

};

this is not correct, y i included the constructor is because. That is the function that initialize the values. So it should look like this:
CODE

class blah {
public:
blah();  // constructor

private:
char blah[] = { a, b, c, d... };

};

blah::blah()
{
char blah[] = { a, b, c, d... };
}

This is how a value should be initialized in the classes constructor.
For a better explanation go here

draco - January 31, 2007 04:59 AM (GMT)
QUOTE (Shonoby @ Jan 30 2007, 10:12 PM)
Umm, well in a class definition u are not allowed to initialize a value of any type. So what u are doing is this:
CODE

class blah {
public:
blah();  // constructor

private:
char blah[] = { a, b, c, d... };

};

this is not correct, y i included the constructor is because. That is the function that initialize the values. So it should look like this:
CODE

class blah {
public:
blah();  // constructor

private:
char blah[] = { a, b, c, d... };

};

blah::blah()
{
char blah[] = { a, b, c, d... };
}

This is how a value should be initialized in the classes constructor.
For a better explanation go here

That compiles, but it doesn't do what you think. It declares a new local variable blah inside the constructor and doesn't change the class variable.

I think you need something like this:

EDIT: Nevermind... i dunno what i was on.. I've been using Java too much
EDIT: Alrighty then, I think you have to use dynamic memory
CODE
class blah
{
int *blab;

public:
blah()
{
 blab = new int[4];
 
 for ( int i = 0; i < 4; ++i )
  blab[i] = i + 1;
}

~blah()
{
 delete blab;
}
};

Shonoby - January 31, 2007 05:05 AM (GMT)
Ok, well your most likely right cause i have not been programming for a while, so i am pretty rusty, but yea, i have not studied that type of class stuff...

Shonoby - January 31, 2007 05:25 AM (GMT)
QUOTE (draco @ Jan 30 2007, 11:59 PM)
QUOTE

That compiles, but it doesn't do what you think. It declares a new local variable blah inside the constructor and doesn't change the class variable.

I think you need something like this:

EDIT: Nevermind... i dunno what i was on.. I've been using Java too much
EDIT: Alrighty then, I think you have to use dynamic memory
CODE
class blah
{
int *blab;

public:
blah()
{
 blab = new int[4];
 
 for ( int i = 0; i < 4; ++i )
  blab[i] = i + 1;
}

~blah()
{
 delete blab;
}
};



Umm, what the hell??, i know u are doing it dynamically, but i doubt that u have to dynamically initialize it. I think you just have to initialize in the constructor and umm, how u said that it did change the value. Well that is incorrect because public member functions use the private member values, and change them, so actually it initializes that value, not another value.

KTC - January 31, 2007 05:32 AM (GMT)
QUOTE
Well that is incorrect because public member functions use the private member values, and change them, so actually it initializes that value, not another value.

Not if you declare a variable inside a block with the same variable name and hide the one outside.........

This work....
CODE
class blah {
public:
   blah();  // constructor

private:
   char characters[26];
};

blah::blah()
{
   for(int i=0; i<26; i++)
   {
       characters[i] = i + 'a';
   }
}

myork - January 31, 2007 03:08 PM (GMT)
CODE

class X
{
   static const char c1[];
   static const char c2[];

   char c3[26];
   public: X();
};


const char X::c1[] = "abcdefghijklmnopqrstuvwxyz";
const char X::c2[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' /* ETC */};

#include <algorithm>

X::X()
{
   std::copy(c1,c1+26,c3);
   char c4[]   = "abcdefghijklmnopqrstuvwxyz";
   char c5[]   = {'a', 'b', 'c', 'd', 'e', 'f', 'g' /* ETC */};
}

Zaqufant - January 31, 2007 11:23 PM (GMT)
I may just be stupid, and I havn't much done anything with dynamic memory, but you can do this...
CODE
blab = new int[4];

And this declares an array with 4 elements!? If so, this will help me alot with my class that I have developed!

charlie - February 1, 2007 12:27 AM (GMT)
QUOTE (Zaqufant @ Jan 31 2007, 04:23 PM)
I may just be stupid, and I havn't much done anything with dynamic memory, but you can do this...
CODE
blab = new int[4];

And this declares an array with 4 elements!? If so, this will help me alot with my class that I have developed!

Yes you can, but this is usually better:
CODE
std::vector<int> blab(4);
In a class it would look like this:
CODE
class one
{
 std::vector<int> blab;
public:
 one() : blab(4) { }
};

class two
{
 int* blab;
public:
 two() : blab(new int[4]) { }
 ~two() { delete [] blab; }
private:
 // Disable copying since the automatic versions won't work properly.
 // You could also implement these yourself, or just use class one instead.
 two(const two&);
 void operator=(const two&);
};


Finally, working off of myork's code, there is this way to initialize an "array" in a class:
CODE
class X
{
 static const char c1[];
 static const int c1_size;

 vector<char> c;
public:
 X();
};

const char X::c1[] = "abcdefghijklmnopqrstuvwxyz";
const int X::c1_size = sizeof(c1)/sizeof(c1[0]);

X::X() : c(c1, c1 + c1_size) { }

Zaqufant - February 1, 2007 01:51 AM (GMT)
How can you use dynamic memory in standard C, or can you?

and what does the sizeof(); function do?

Shonoby - February 1, 2007 03:02 AM (GMT)
sizeof() calculates the amount of bytes used to store each of the standard data types, run this code:
CODE

// Using "sizeof"
// By: Darwin Aguirre
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int sizeget ( double * );

int main()
{
   double array[ 72 ];

   cout << "Bytes in array "
        << sizeof( array ) << endl;

   cout << "Bytes return by function sizeget "
        << sizeget( array ) << endl;

   return 0;
}

int sizeget ( double *aPtr )
{
   return sizeof( *aPtr );
}

The first 'cout' displays the amount of bytes in the value, which is determined by the size of the data type's bytes times the number of elements in array, but only applies to arrays. But the second 'cout' displays the number of bytes in the data type.

adeyblue - February 1, 2007 04:46 AM (GMT)
In C you can use malloc or calloc to allocate the memory (there are others) and free to dispose of it.

Zaqufant - February 1, 2007 12:37 PM (GMT)
Thank you very much.

myork - February 1, 2007 02:18 PM (GMT)
QUOTE (Shonoby @ Jan 31 2007, 10:02 PM)
sizeof() calculates the amount of bytes used to store each of the standard data types


sizeof() returns the size in bytes of any type (or object [for objects it returns the size of the type of the object. We know this because sizeof() runs at compile time not runtime]).

It even works on arrays (but be carefull pointers are always the same size).

Zaqufant - February 1, 2007 09:21 PM (GMT)
What parameter should you pass for malloc();? What is a size_t? What should I pass?

myork - February 2, 2007 04:42 AM (GMT)
QUOTE (Zaqufant @ Feb 1 2007, 04:21 PM)
What parameter should you pass for malloc();? What is a size_t? What should I pass?



size_t is a size (usually an unsigned int ie 0 -> <Big Number>)

The value you pass to malloc() is the number of bytes you require.

CODE

/*
* Allocate space for 1 int
*/
int*  x1  = (int*)malloc(sizeof(int));
/*
* If the malloc fails then x1 is NULL.
* You must check for this.
*/



/*
* Allocate space for an array of 4 int
*/
int*  x2  = (int*)malloc(sizeof(int) * 4);
/* Check for NULL pointer. */

/*
* Do Stuff
*/

free(x1);
free(x2);

adeyblue - February 2, 2007 07:16 AM (GMT)
There's no need to check the pointer for NULL before calling free() unless you're using an ancient compiler, the C standard (89/90/99) says that free(NULL) does nothing. Much the same as delete-ing a NULL pointer does nothing in C++.

myork - February 2, 2007 12:23 PM (GMT)
QUOTE (adeyblue @ Feb 2 2007, 02:16 AM)
There's no need to check the pointer for NULL before calling free() unless you're using an ancient compiler, the C standard (89/90/99) says that free(NULL) does nothing. Much the same as delete-ing a NULL pointer does nothing in C++.

OK.
Conceed that point.
I from the old days before that was a garantee. That's why the C++ delete was a god send. No need to check for NULL anymore.

dimaz_2kx - February 3, 2007 04:48 AM (GMT)
ei...tnx for all d answer, though am a bit confuse... i'll ty out a combination of what myork wrote...tnx!




Hosted for free by InvisionFree