-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51parametrized_constructor_example.cpp
38 lines (36 loc) · 1.17 KB
/
51parametrized_constructor_example.cpp
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
#include<iostream>
#include<cmath>
using namespace std;
// constrcutor has same name as its class
// so if we define a fn inside a class with the same name as of the class
// and without a return type
// that fn will become a constructor
class point{
int x, y;
static int a; // creating a static integer
public: // as constructors must always be declared under the public section
point(int a, int b){ // this fn is a constructor as it has the same name as of it's class
// and no return type // and it's a parametrized constructor
x = a;
y = b;
}
void disp(){
cout<<"The point p"<<a<<" is: ("<<x<<","<<y<<")"<<endl;
a++;
}
friend void distance(point p1, point p2);
};
int point :: a = 1; // initializing the static integer
void distance(point p1, point p2){
int X, Y, D;
X = pow((p1.x-p2.x),2);
Y = pow((p1.y-p2.y),2);
D = pow((X+Y),(0.5));
cout<<"And the integer distance between them is: "<<D;
}
int main(){
point p1(3,2), p2(9,7);
p1.disp(), p2.disp();
distance(p1,p2);
return 0;
}