Changing public members inside a class

I'm having 4 files :
1.) new.h - where I have declared my variables and only method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #ifndef NEW_H
#define NEW_H


#include "stdio.h"
#include "stdlib.h"

class myclass{
public:
	myclass();
	int x;
	int y ;
	int sum();
};

#endif  



2.) Now I have my new.cpp file where I have written the functionality of the method declared in new.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "new.h" //including the header file

myclass::myclass() //constructor
{

}


int myclass::sum() //method implementation
{
myclass MC;
int a=MC.x+MC. y;
printf("%d \n",a);
return a;
}




3.) I have a main.cpp file to call the functions defined

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include "new.h"
#include "stdio.h"
#include <iostream>
#include "initial.cpp"


int main()
{
	initial(); //to call the initial.cpp 
	myclass MC; //brickpi3 class object 
	MC.sum(); // calling the method
	printf("Completed \n"); 
}



4.) Now I want to change the values of members declared in new.h through another file initial.cpp ( where I want to give the values for x and y)

1
2
3
4
5
6
7
8
9

#include "new.h"

void initial(){
	myclass MC;
	int  MC.x = 420;
	int  MC.y = 640;
	return;
}



Is it possible to change the values from a different file and still get the methods work??
If so, please help I new to programming and I'm struck over here.

NOTE: I'm not supposed to change new.h or new.cpp and maybe I can declare them as static, other than that I can't really change the new.h or new.cpp
Last edited on
You shouldn't be #including a .cpp file, it's not the proper way to do things. Only #include header files.

I'm not sure if you're understanding the difference between a class and an object.
1
2
3
4
	myclass MC;
	int  MC.x = 420;
	int  MC.y = 640;
	return;

MC is an object of type (class) myclass. (Declaring MC.x and MC.y as local ints doesn't make sense, either).

As of right now, your initial() function (1) doesn't even compile and (2) would only be changing a local object, and nothing about the class itself.

Since you have x and y and public in your class, you can access them through an object of the class.

Would something like this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ... { necessary #includes } 

// passes an object of type myclass by reference
// so that it can be changed by the function
void initial(myclass& mc)
{
    mc.x = 420;
    mc.y = 640;
}

// ...

int main()
{
	myclass MC;
	initial(MC);
	MC.sum(); // calling the method
	printf("Completed \n"); 
}


__________________

1
2
3
4
5
6
7
int myclass::sum() //method implementation
{
myclass MC;
int a=MC.x+MC. y;
printf("%d \n",a);
return a;
}

This is weird. Are you sure you're not allowed to change this? You're declaring a local MC object that is default constructed. It has nothing to do with your MC object in main.

1
2
3
4
5
6
int myclass::sum() //method implementation
{
    int a = x + y;
    printf("%d \n",a);
    return a;
}
Last edited on
From new.cpp
1
2
3
4
5
6
7
int myclass::sum() //method implementation
{
	myclass MC;
	int a=MC.x+MC. y;
	printf("%d \n",a);
	return a;
}


I'm not supposed to change new.h or new.cpp


Are you sure this is the original contents of new.cpp? It does not make any sense. This function is just wrong.

In line 3 you declare MC which has no value for x or y. I forget whether these values are initialized to 0 or whether they just get garbage values of whatever happened to be there.

These values (MC's x and y) are added together and returned. The contents of x and y of the object being worked on are ignored.

I suspect the version of the function you were given looks like this:
1
2
3
4
5
6
int myclass::sum() //method implementation
{
	int a=x+y;
	printf("%d \n",a);
	return a;
}


Also, in your step 4, you create an object in line 5, populate its members in lines 6 and 7, and it goes out of scope in line 9, so nobody can use it. You need to either pass the object in by reference (void initial(myclass& MC){...} or return the object (myclass initial(){...}).
Thanks much @Ganado , @doug4

I understand What I'm doing wrong and thanks for explaining!!!

Topic archived. No new replies allowed.