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
// ... { 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;
}
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(){...}).