Class Addition Problem

Hello every body...kindly check out this code....

#include<iostream.h>
#include<conio.h>



class vector
{
float x_comp1,y_comp1,z_comp1,x_comp2,y_comp2,z_comp2;
float add_x,add_y,add_z;

public:

void v_input1(float x,float y,float z)
{
x_comp1=x;
y_comp1=y;
z_comp1=z;

cout<<"\nThe vector is "<<x_comp1<<"i + "<<y_comp1<<"j + "<<z_comp1<<"k";


}

void v_input2(float x,float y,float z)
{
x_comp2=x;
y_comp2=y;
z_comp2=z;

cout<<"\nThe vector is "<<x_comp2<<"i + "<<y_comp2<<"j + "<<z_comp2<<"k";


}

void addition(void)
{

add_x=(x_comp1 + x_comp2);
add_y=(y_comp1 + y_comp2);
add_z=(z_comp1 + z_comp2);

cout<<"The addition is: "<<add_x<<"i + "<<add_y<<"j + "<<add_z<<"k";


}





};

main()
{
vector A,B,add;
cout<<"Vector 1"<<endl;
A.v_input1(1,2,3);
cout<<"\n\nVector 2"<<endl;
B.v_input2(4,5,6);
cout<<"\n\n";
add.addition();








getch();
}



Now the problem is that when i run the program, i get correct value of the two vectors, but i am not getting the right value for the addition...it might be the x_comp1 and so on are not getting those values even i am assigning them.....i am new to classes so pls help me out what is the actual problem...thanks
Last edited on
Your problem appears to be that you are declaring multiple instances of your vector object instead of just one instance.

try:
1
2
3
4
5
6
7
8
9
10
11
main()
{
   vector X;
   cout << "Vector 1" << endl;
   X.v_input1(1, 2, 3);
   cout << "\n\nVector 2" << endl;
   X.v_input2(4, 5, 6);
   cout << "\n\n";
   X.addition();

   getch(); 


This would match your definition and implementation of a vector above (which isn't a proper vector - i'm assuming vector in terms of single dimensional matrix from you addition implementation).
hey SIK...it is working fine now...thanks....but what was the logic behind your code....and why my code was not working???? what is the problem in creating multiple instances of the same class????
Last edited on
hey awaise17, when you create multiple instances as in your initial main function you would then have a seperate piece of memory reserved for each instance of the vector class.

so in your case you would have an instance for object A, object B and object add.

Each of these objects will have their own set of member variables, ie x_comp1, x_comp2, ..., add_z

Therefor when you call A.v_input1(1, 2, 3), you are setting only object A's x_comp1, y_comp1 and z_comp1 values.

When you call B.v_input2(3, 4, 5), you are setting only object B's x_comp2, y_comp2 and z_comp2 values.

Also note that the member values (x_comp1, ... add_z) of each of these vector objects A, B, add will be undefined/garbage upon creation. You would need a contructor to have these initialized to something safe like zero at object creation.

Consequently the remaining values of your objects A and B that were not set is still garbage and so is your entire add object.

Therefor when you called the addition method on your add object it, applied the add operation to a bunch of garbage values (ie. it does not reference objects A and B at all, so setting the input 1 & 2 values in these objects was futile)
help appreciated...thanks :-)
Topic archived. No new replies allowed.