Initializing an array outside class after declaration - c++

For example:
const int m = 10;
class C{
public:
double A[m];
};
int main(){
C name;
name.A[m] = ... // initializing here?
}
I can't find a way around that, I could for example do
C name = {...};
Which would perfectly work but for the sake of functionality I wanna know if I can do that for single variables inside the class.

In your example, you only need to write:
name.A[x]=value;
Where value is double and x is between 0 and 9. You can also make a loop if you wish to set values for all or some of its elements.

Related

Problems in accessing variables in nested classes

I am trying to access the variable i declare in my nested class, but i fail to get the answer i want at the console. The result i want is 100, but all that i get is a long number. I cannot seem to find the error. This is what i wrote:
#include <iostream>
using namespace std;
class shpia {
public:
int tot;
class dhoma1 {
public:
int gjatesi;
int di() {
return gjatesi * gjatesi;
}
};
dhoma1 dh1;
void redi(){
cout<<dh1.di();
}
};
int main()
{
shpia::dhoma1 k;
k.gjatesi = 10;
shpia r;
r.redi();
return 0;
}
There's nothing surprising about your result. You seem to think
shpia::dhoma1 k;
k.gjatesi=10;
will define a dhoma1 for all shpia objects you will create. This is wrong. You just defined a shpia::dhoma1 object that has nothing to do with shpia objects.
When you then define
shpia r;
this will create in r another dhoma1, unrelated to the first one, which is not initialized. Hence when you print the square you're getting non-sense.
You are accessing uninitialized memory.
Here you create an instance of the nested class, and initialize its member:
shpia::dhoma1 k;
k.gjatesi=10;
And here you create an instance of the main class, which has nothing to do with the k. It already has a nested class member variable defined itself (r.dh1)
shpia r;
r.redi();
return 0;
Because of this new declaration, the nested class of r has no defined value and when you call redi(), you will access undefined memory and therefore get some random number. Depending on the actual runtime layout of your application, this value can change. It is undefined and you have to define it before you use it.
To fix this, you should use the nested class member of the main class instead, like this:
shpia r;
r.dh1.gjatesi = 10;
r.redi();
return 0;

Array of values within an ENUM?

I have this code
enum type {NOTHING, SOMETHING, SOMETHINGELSE}
type *x;
At the moment I use x[765] == SOMETHING for example, How would I store other values for example
x[765] == SOMETHINGELSE;
x[765].position == 43.5;
x[765].somevar == 12;
I will apologize for my poor wording within my question im just starting out in C++, I know what I want i'm just not to sure on how to ask it.
Thanks.
It looks as if you're looking for a way to structure 'knowledge'; this is done with a struct or a class:
#include <vector>
struct Info {
enum thingness { nothing, something };
// 'member' variables
thingness howMuch;
int a_counter;
float position;
};
int main(){
Info object;
object.howMuch=Info::something;
object.a_counter=1;
object.position=5.4;
You can group these kinds of objects into a container - typically an std::vector:
// a container of InterestingValues
std::vector<Info> container(300);
container[299].howMuch=Info::nothing;
container[299].a_counter=4;
container[299].position = 3.3;
// or assign rightaway:
container[2] = object;
}
You will have to make yourself a more complex type:
struct type
{
enum flag_type
{
NOTHING, SOMETHING, SOMETHINGELSE
} flag;
double position;
int somevar;
};
and later have an array of this new type.
Get yourself a good book to learn from. A list of good books is available here: The Definitive C++ Book Guide and List
In C++, you are asking how to declare an array of structures. Try this:
struct type {
double position;
int somevar;
};
type *x;
x[765].position = 43.5;
x[765].somevar = 12;
An enum is a replaceable label basically for an int. You need to define a struct or a class.
struct type
{
float position ;
};
type var;
var.position = 3.4;
Your type enum would need to be a member of a class, along with the other fields. For example,
class MyType
{
public:
type t;
double position;
int somevar;
};
With an array of MyType instances
MyType *x;
you would then be able to do what you ask expect you would need to do
x[765].t = SOMETHINGELSE;
to assign to the enum.

Changing integers in classes C++

Okay, so, I am storing a public integer in a class, like this:
class varglobalness{
public:
int xp;
int lvl;
int xpt;
int hp;
};
and, inside of a function I am trying to change the variables by doing this:
v.lvl += 1;
v.xpt += rand()%25+25;
v.xp = 0;
v.hp += rand()%25+5;
When I do this, though, hp doesn't change, xp does change to 0, but xpt and lvl changes to a lot of random numbers.
Any idea how I can properly change these integers without this happening?
Sounds like you're not properly initialising them. If a variable is uninitialised, it'll have an undefined value. You can give them initial values in the constructor:
// varglobalness.h:
class varglobalness {
public:
varglobalness();
// ...
};
// varglobalness.cpp:
varglobalness::varglobalness() : xp(0), lvl(0), xpt(0), hp(0) {}
Or assign them initial values elsewhere, as you prefer. As an aside, you ought to be putting these variables somewhere meaningful and encapsulated, rather than in a bundle of random globals.
Did you initialize the variables? Variables in C++ don't initialize themselves.
//In your constructor
v.lvl = 1;
v.xpt = 0;
v.xp = 0;
v.hp = 0;

Class C++ trace this value!

#include <iostream>
using namespace std;
class t
{ public:
int health; //its members
int speed;
int power;
void attack() // its methods
{ cout<<"I'm attacking"<<endl;
};
};
int main()
{ t A,B,C,D;
A.power = 100;
B.health = 87;
C.speed = 92;
cout<<"A= "<<A.power<<"B= "<<A.health<<"C= "<<A.speed<<endl; // <---
cout<< "My health is "<<C.health<<" My speed is "<<A.speed<<endl;
cout<<"My power is "<<B.power<<endl;
D.attack();
system("pause");
return 0;}
The output result was ::
A= 100 B= 96 C=6234392 <--- From where these values come
A.health and A.speed are just junk values on the stack because you didn't explicitly set them. If you want to initialize all fields of A to zero, you can use memset:
memset(&A, 0, sizeof(A));
You should create a constructor to initialize those values to some default value in the initializer list.
class t {
public:
t() : health(100),power(100),speed(100) {}
// ...
};
This will guarantee that those values are all set to 100, or some default, or even an input parameter, rather than garbage. It's considered much better design since otherwise the initialization of those values would be handled in the constructor that the compiler generates for you behind the scenes.
Uninitialized memory?
Uninitialized variable won't be zero setted at the creation of the class/struct. You need to manualy do it. Otherwise, you will get whatever_is_in_memory_at_that_time.

Accessing a 2D array between two friend classes

My question is how to access and modify a 2D array defined in one class that is friends with another class. Below are some details on my question:
In class A I declare and allocate the appropriate space for my 2D array (pointer-to-pointer) u.
Class A
{
public:
friend class B;
long double **u;
int fun;
void make();
};
void A::make()
{
long double **u = new long double *[nx];
for (int i=0;i<nx;i++)
u[i] = new long double [ny];
int fun = 9;
}
Class A is friends with Class B; I need to use the array I declared in Class A in a function defined in class B. Below is my Class B:
class B
{
public:
void get(A*);
};
void B::get(A *pt)
{
using namespace std;
cout << pt->fun;
cout << pt->u[0][0];
}
I get a Bus error on my second cout pt->u[0][0]. Is there a simple way to use this setup I have to access my u[][] array? I think that I get the error because the pointer points to the 1st entry of my array, thus my whole 2D array is saved in memory as a single row (thinking aloud here). I'm a Fortran guy so this stuff is a little new to me.
Any help or "pointers" to other helpful threads would be appreciated.
Thank you !
Alberto
I think you get error because A::u is not initialized ( in method A::make you initialize a local variable u, not member. You need to change
void A::make()
{
long double **u = new long double *[nx]; // should be just u, or this->u.
There are some problems with your code: nx and ny don't seem to be defined anywhere, and in make you don't initialize A::fun at all, you instead set a local variable named fun which goes out of scope immediately.
As for your error, it sounds like the error stems from the fact that make() has not been called on pt. Ensure that make() is called on the instance you pass to get, otherwise the array u will not be allocated.