C++ Accessing variables in a function in one class from a different class - class

I'm trying to code a program with multiple classes such the one of the class reads the variables from a text file and the other classes use these variables for further processing.
The problem I'm facing is that I'm having trouble passing the variables from one class to another class, I did try "friend" class and also tried to use constructors but failed
to get the desired output.
The best I could do was
suppose I have class 1 and class 2, and I have a variable "A=10" declared and initialised in class 1, with the help of constructor I inherit it in class 2;
when I print it in class 1, it gives a correct output as 10 but when I print it in class 2 it gives an output as 293e30 (address location)
Please guide me on how to this.
Class1
{
public:
membfunc()
{
int A;
A = 10;
}
}
Class2
{
public:
membfunc2()
{
int B;
B = A + 10;
}
membfunc3()
{
int C, D;
C = A + 10;
D = B + C;
}
}
If i print variables, i expect to get
A = 10, B = 20, C = 20, D = 40
But what I get is
A = 10, B=(252e30) + 10

I think your problem was that you were defining local variables in your member functions, instead of creating member variables of a class object.
Here is some code based on your sample to demonstrate how member variables work:
class Class1
{
public:
int A;
void membfunc()
{
A=10;
}
};
class Class2
{
public:
int B;
int C;
int D;
void membfunc2(Class1& class1Object)
{
B = class1Object.A + 10;
}
void membfunc3(Class1& class1Object)
{
C = class1Object.A + 10;
D = B + C;
}
};
(Full code sample here: http://ideone.com/cwZ6DM.)
You can learn more about member variables (properties and fields) here: http://www.cplusplus.com/doc/tutorial/classes/.

Related

Java object creation storage space

Given the following code:
class cl
{
int aa = 5;
String st = “Top“;
}
class c2
{
public static void main ()
}
c1 jj = new c1();
}
How is st stored in jj ?
Just as a Reference To the actual String „Top“ or is the Full String Stores within jj ?
Thanks for any help.

Initialize a final variable in a constructor in Dart. Two ways but only one of them work? [duplicate]

This question already has an answer here:
Is there a difference in how member variables are initialized in Dart?
(1 answer)
Closed 1 year ago.
I'm trying to understand the following example where I try to initialize a final variable in a constructor.
1st example - works
void main() {
Test example = new Test(1,2);
print(example.a); //print gives 1
}
class Test
{
final int a;
int b;
Test(this.a, this.b);
}
2nd example doesn't work
void main() {
Test example = new Test(1,2);
print(example.a); //compiler throws an error
}
class Test
{
final int a;
int b;
Test(int a, int b){
this.a = a;
this.b = b;
}
}
and when i remove final then it works again
void main() {
Test example = new Test(1,2);
print(example.a); //print gives 1
}
class Test
{
int a;
int b;
Test(int a, int b){
this.a = a;
this.b = b;
}
}
what is the difference between the constructor in the 1st and the 2nd constructor why final initialization works with the first and doesn't with the 2nd.
Can anyone explain that to me please?
THanks
You cannot instantiate final fields in the constructor body.
Instance variables can be final, in which case they must be set exactly once. Initialize final, non-late instance variables at declaration, using a constructor parameter, or using a constructor’s initializer list:
Declare a constructor by creating a function with the same name as
its class (plus, optionally, an additional identifier as described in
Named constructors). The most common form of constructor, the
generative constructor, creates a new instance of a class
syntax in the constructor (described in https://www.dartlang.org/guides/language/language-tour#constructors):

C++11 rvalue references vs references vs pointers vs "normal" members

I have code like this:
class A{
};
class B{
B(A &&a) : a(std::move(a)){};
A a;
}
A a{};
B b{ std::move(a) };
// a is unusable
I use rvalue, because I do not want object to be copy-ed.
I am thinking of changing the class B to use references:
class A{
};
class B{
B(A &a) : a(a){};
A &a;
}
A a{};
B b{ a };
// a is usable
Will there be any negative effects of this, except live-time concerns? I do not reassign the a object, so I do not need pointers. but should I consider them?
Why not let the caller decide whether to move or copy its A object?
class A {};
class B {
public:
B(A a_param) : a(std::move(a_param)) {}
private:
A a;
};
void f1() {
A a;
B b{a}; // Makes a copy, a is still usable.
}
void f2() {
A a;
B b{std::move(a)}; // Does a move, a is not usable.
}
void f3() {
B b{A{}}; // Does a move of the temporary object.
}

UnityScript - How To Loop Through Public Properties of A Class

I have the following script in UnityScript, which is called JavaScript in Unity Editor but is not quite the same especially for looping through objects.
public class UpgradeProfile extends MonoBehaviour {
public var brakeSpeed : float = 0;
public var jumpForce : float = 0;
public var maxJumps : int = 1;
};
How can I loop through all the properties of this class and, for example, log the values or sum them with the values of another member of the same class?
Note: UnityScript is not JavaScript or C# so answers relating to those languages do not answer this question.
This works for me to get the properites and values.
#pragma strict
public var test1 = 10;
public var test2 = 11;
function Start ()
{
for(var property in this.GetType().GetFields())
{
Debug.Log("Name: " + property.Name + " Value: " + property.GetValue(this));
}
}
And this prints out
Name: test1 Value: 10
Name: test2 Value: 11
And if you want to do this with another component, replace this with a component instead

How do you import a variable from the main actions panel to a class file?

I've searched long for this one, but nobody seems to give a clear answer. I'm a beginner, and I want to import a variable from the main "F9" actions panel into a class file so it can read it (example: in main .fla file, I have a variable var myNumber:Number = 1; how would I import it into a class file so the program can read it?)
You can create a “Document Class”. In this class you can place your variable. Where will you use myNumber variable?
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public var myNumber:Number = 1;
public function Main()
{ }
}
}
You can "share" your variable when you create an instance of your class by passing it in the class constructor, or using any other public function.
Take this example :
MyClass.as :
package {
public class MyClass {
private var number:int;
public function MyClass(num:int = 0) {
// assign the value of the variable to a private one for a later use
this.number = num;
// use directly the value of the variable
trace('the number is passed in the constructor : ' + num);
}
public function set_number(num:int):void {
// assign the value of the variable to a private one for a later use
this.number = num;
}
public function use_number(num:int):void {
// use the value of the variable
trace(num + ' x ' + num + ' = ' + (num * num));
}
}
}
test.fla :
import MyClass;
var my_number:int = 1234;
var my_class:MyClass = new MyClass(my_number); // using the class constructor
my_class.set_number(my_number); // using a function to set a private var value
my_class.use_number(my_number); // using a function to do some operations
Hope that can help.