Hi,
I am new to object oriented programming. As a part of my work I need to generate 100 points(x,y) making a line. I need ten such lines. I tried to make a line class and then 10 objects of line each to store 100 points with x and y coordinates making it a 100X2 array. I am struck here. How to retain the values. I have even tried with my elementary pointer knowledge but that is not working either.
#include <windows.h>
#include <stdio.h>
#include <math.h>
usingnamespace std;
class Line
{
public:
float x,y;
float Lpts[100][2];
void line1(int);
};
int main(){
int Lno=10;
Line L[10];
for (int k=1;k<=Lno;k++)
{
L[k].line1(k);
}
cout<<L[6].Lpts[40][0]<<" "<<L[6].Lpts[40][1]; //here coordinates of 40th point in sixth line should come
return 0;
}
void Line::line1(int k)
{
int i=0;
float Lpts[100][2];
for(i= 0.0f;i<=100;i++)
{
y = k+(i*0.10);
x = k;
Lpts[i][0]=x;
Lpts[i][1]=y;
i++;
}
return Lpts;
}
I am sorry if its too elementary but thats the problem now for me. Thanks in advance.
remove line 38. Lpts[100][2] is already a member of the Line class so you don't need to define it again. This is just making a local member which is destroyed at the end of the function.
Also remove line 55. Void functions don't return anything. Also, you don't use the return value in the main code.
class Line
{
public:
float x,y;
float Lpts[100][2];
void line1(int);
public:
Line()
{}
};
int main()
{
int Lno=10;
Line L[10];
for (int k=1; k<Lno; k++)
{
L[k].line1(k);
}
cout<<L[6].Lpts[40][0]<<" "<<L[6].Lpts[40][1]; //here coordinates of 40th point in sixth line should come
getchar();
return 0;
}
void Line::line1(int k)
{
int i = 0;
for(i = 0; i <100; i++)
{
y = k + (i * 0.10);
x = k;
Lpts[i][0] = x;
Lpts[i][1] = y;
}
}