How to write a C Program Friend & Operator: Point2D Example in C Programming Language ?
Solution For C Program :
//C Program Friend & Operator: Point2D Example.
#include <iostream>
//#include "Point2D.h"
using namespace std;
class Point2D{
private:
// Data (variable part)
double x;
double y;
public:
friend void OutputPoint2D(Point2D v);
// Operation (function part)
//Constructive
Point2D(){
x = 0;
y = 0;
}
Point2D(double a, double b){
x = a;
y = b;
}
double getX(){
return x;
}
double getY(){
return y;
}
Point2D operator*(Point2D b){
Point2D c(x*b.x, y*b.y);
return c;
}
};
void OutputPoint2D(Point2D v){
cout << "(" << v.x << "," << v.y << ")" << endl;
}
int main(){
int x, y;
cin >> x >> y;
Point2D p1(x, y);
cin >> x >> y;
Point2D p2(x, y);
Point2D p3;
p3 = p1 * p2;
OutputPoint2D( p3 );
return 0;
}