Skip to content

Commit 8d6a3f0

Browse files
committed
Constructor Point with vector
1 parent 6a6df56 commit 8d6a3f0

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

point/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ int main() {
4646
for (int j = 0; j < items[i].coord.size(); ++j)
4747
cout << "Item [" << i << "] [" << j << "] = " << items[i].coord[j] << endl;
4848

49+
cout << "Initialize with vector" << endl;
50+
51+
Point w({0.51, 10.45, 20.35});
52+
53+
cout << "Point w " << w << endl;
54+
4955
return 0;
5056
}
5157

point/point.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ using namespace std;
99
// Constructor
1010
Point::Point(int dimen) {
1111

12-
// Could not accept dimension less then 1, don't return an error, but set default
13-
if (dimen < 1)
12+
// Could not accept dimension less then 2, don't return an error, but set default
13+
if (dimen < 2)
1414
dimen= 2;
1515

1616
coord.resize(dimen);
@@ -19,6 +19,20 @@ Point::Point(int dimen) {
1919

2020
} // Constructor
2121

22+
Point::Point(vector<double> p_coord)
23+
{
24+
coord = p_coord;
25+
size_t dimen = coord.size();
26+
27+
if (dimen < 1)
28+
{
29+
coord.resize(2);
30+
if (dimen == 0)
31+
coord[0] = 0.0;
32+
coord[1] = 0.0;
33+
}
34+
}
35+
2236
// Function to calculate distance to other point
2337
double Point::dist(Point other) {
2438
double sum = 0;

point/point.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Point {
1919

2020
// Constructor
2121
Point(int dimen = 2);
22+
Point(vector<double> p_coord);
2223

2324
// Function to calculate distance to other point
2425
double dist(Point other);

vector/vectorArg.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ int main (int argc, char* argv[])
2828
for (int i = 0; i < s; i++)
2929
cout << names[i] << "\n";
3030

31+
for (string st : names)
32+
cout << st << endl;
33+
3134
}

0 commit comments

Comments
 (0)