Class in c++

hello everyone,
I am working on finite element and I have two text files which one of them is the element connectivity and the other one is the mesh coordinate. I am trying to calculate the area of each element by using objective oriented programming. the problem is my class doesn't work properly and I don't get any output as element area. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stdio.h>
#include<conio.h>
#include<algorithm>
#include<ctime> 
#include<iomanip>
#include<list>

using namespace std;
#define nconec    3
#define dimension 2
const int n_node = 246;
const int n_elem = 434;
double x[n_node];
double y[n_node];

template<typename T>
void read_data(ifstream& fin, vector <vector <T> >& m, int ROWS, int COLS)
{

	for (int row = 0; row < ROWS; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			fin >> m[row][col];
		}
	}
}
template<typename T>
void print_data(const vector <vector<T> >& m, int ROWS, int COLS)
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			cout << std::setw(6) << m[i][j];
		}
		cout << endl;
	}
}


class Elem_Area {
private:
	double *x;
	double *y;
	int node_0;
	int node_1;
	int node_2;

public:
	void setternode0(double Node_0) { node_0 = Node_0; }
	int getternode0() { return node_0; }

	void setternode1(double Node_1) { node_1 = Node_1; }
	int getternode1() { return node_1; }

	void setternode2(double Node_2) { node_2 = Node_2; }
	int getternode2() { return node_2; }

	void setterX(double *X) { x = X; }
	double getterX() { return *x; }

	void setterY(double *Y) { y = Y; }
	double getterY() { return *y; }
		
void getarea(double *X, double *Y,int Node_0, int Node_1, int Node_2)

	{
	double element_area= abs(0.5*(((x[node_0] - x[node_2])*(y[node_1] - y[node_2])) - ((y[node_0] - y[node_2])*(x[node_1] - (x[node_2])))));
	}

};


int main()
{
	int no_rows = n_elem;
	int no_cols = 4;
	vector<vector<int>> connectivity(no_rows, vector<int>(no_cols));
	ifstream myfile1;
	myfile1.open("connec.txt");
	read_data(myfile1, connectivity, no_rows, no_cols);
	ofstream fout;
	print_data(connectivity, no_rows, no_cols);
	no_rows = n_node;
	vector<vector<double>> coordinate(no_rows, vector<double>(no_cols));
	ifstream myfile2;
	myfile2.open("mesh.txt");
	read_data(myfile2, coordinate, no_rows, no_cols);
	print_data(coordinate, no_rows, no_cols);

	for (int i = 0; i < n_node; i++)
	{
		x[i] = coordinate[i][1];

	}
	
	for (int i = 0; i < n_node; i++)
	{
		y[i] = coordinate[i][2];

	}


	Elem_Area *elem_area[n_elem];


	for (int i = 0; i < n_elem; i++)
	{

		int node_0=((connectivity[i][1] - 1));
		int node_1=((connectivity[i][2] - 1));
		int node_2=((connectivity[i][3] - 1));
		
		elem_area[i]->getarea(x, y, node_0, node_1, node_2);
		

	}

	fout.open("elem_area.txt");
	fout << "variables = elem_area " << endl;

	fout << "zone I = " << n_elem << endl;
	for (int i = 0; i < n_elem; i++)
	{
		fout << i << "       " << elem_area[i] << ' ';
		fout << endl;
	}
	fout.close();

	return 0;

}
 
Last edited on
For a start, things like this:
 
double *x

should be:
 
double x


Not all problems are best solved with OO.
Last edited on
Why not pointer?
What are the pointers pointing to? Nothing. You don't allocate (nor delete) any dynamic memory in your class.
I need pointer because in the getarea void I need to define x[node_0,1or 2] and y[node_0, 1 or 2]. Otherwise how I can fix that?
Use pointers, particularly raw pointers, only when they really are necessary. If you have to ask why not pointer, then you don't have solid rationale why you should have pointers.


Your compiler should warn you that in:
1
2
3
4
5
6
void Elem_Area::getarea( double *XXX, double *YYY, int ZZZ, int WWW, int VVV )
{
  double aaa = (x[node_0] - x[node_2])*(y[node_1] - y[node_2]);
  double bbb = (y[node_0] - y[node_2])*(x[node_1] - (x[node_2]) );
  double element_area = abs( 0.5*( ( aaa ) - ( bbb )) );
}

The function parameters (XXX, YYY, ZZZ, WWW, VVV) are not used at all.
Furthermore, you do absolutely nothing with the local variable element_area.


Worse, in your main() you have:
1
2
3
Elem_Area* elem_area[n_elem];
// ...
elem_area[i]->getarea( x, y, node_0, node_1, node_2);


Q: What does happen in:
1
2
Elem_Area* ptr; // uninitialized pointer
ptr->getarea( 0, 0, 0, 0, 0 );

A: We call member function of an object that does not exists. Undefined behavior.


Guess what is recommended about global variables?
1
2
double x[n_node];
double y[n_node];

Yes, use only when they really are necessary.


How about all the includes? Less is more.

On line 75 element_area is not used. Perhaps better if it was a member of the class?

On line 132, will that print a pointer value ie the this pointer for each item? You need to tell it how to print the class.
Otherwise how I can fix that?

Consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Point { double x; double y; };

double calcarea( Point p0, Point p1, Point p2 )
{
  double aaa = (p0.x - p2.x)*(p1.y - p2.y);
  double bbb = (p0.y - p2.y)*(p1.x - p2.x);
  return abs( 0.5*( aaa - bbb ) );
}

std::vector<Point> points;
// ...
std::vector<double> elem_area(n_elem); // EDIT: wrong parentheses. Were []. Must be ().
// ...
elem_area[i] = calcarea( points[node_0], points[node_1], points[node_2] );
Last edited on
Many thanks for your answer.
In these code the preference is using class. My problem is I don't know how to read a text file into the class objects.
I think there will be a problem here
elem_area[i] = calcarea( points[node_0], points[node_1], points[node_2] );
No operator "=" matches those operands
In these code the preference is using class. My problem is I don't know how to read a text file into the class objects.

The "preference" is to use classes and you don't know how to use classes?

There is "operator overloading" for << and >> to write/read objects into stream.

However, you had:
1
2
3
4
5
6
for (int i = 0; i < n_node; i++) {
  x[i] = coordinate[i][1];
}
for (int i = 0; i < n_node; i++) {
  y[i] = coordinate[i][2];
}

which is easy to replace with:
1
2
3
4
5
std::vector<Point> points( n_node );
for (int i = 0; i < n_node; i++) {
  points[i].x = coordinate[i][1];
  points[i].y = coordinate[i][2];
}


I had a typo in previous comment.
I had an array of empty vectors (the []) while I did intend to create one vector with n_elem elements. (the ())
Last edited on
I never worked on objective oriented programming and this is my first programming.Thanks for your answer
Thanks
Topic archived. No new replies allowed.