How to write a C Program Friend & Operator: Vector Example in C Programming Language ?
Solution For C Program :
//C Program Friend & Operator: Vector Example.
#include <iostream>
#include <cmath>
using namespace std;
class Vector{
private:
// Data (variable part)
double x;
double y;
public:
friend void printVector(Vector v);
// Operation (function part)
//Constructive
Vector(){
x = 0;
y = 0;
}
Vector(double a, double b){
x = a;
y = b;
}
double getX(){
return x;
}
double getY(){
return y;
}
Vector operator+(Vector b){
Vector c(x+b.x, y+b.y);
return c;
}
double getLength(){
return sqrt(x*x + y*y);
}
};
void printVector(Vector v){
cout << "(" << v.x << "," << v.y << ")" << endl;
}
/*
Vector vectorAdd(Vector a, Vector b){
Vector c(a.x+b.x, a.y+b.y);
return c;
}
*/
int main(){
Vector a(3, 4), b(4, 5);
/*
a.getX();
b.getX();
*/
Vector c = a + b;//a.operator+(b);
printVector(c);
return 0;
}
Program related to C Programming Language :
- C Program Array Index Example
- C Program String - Alphabet Count Example
- C Program Character toupper() Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program if - while - for Example
- C Program Switch - Case Example
- C Program 0.1 + 0.2 != 0.3 Solved
- C Program if - else if - else Example
- C Program typedef Example
- C Program scanf & gets Example
- C Program Friend & Operator: Point2D Example
- C Program Friend & Operator: Vector Example
- C Program Recursion Example
- C Program File Input & Output Example
- C Program Structure Example-2
- C Program Structure Example
- C Program Pointer Example
- C Program Function Example
- C Program String Example
- C Program String Count Example
- C Program String Example
- C Program Character Example
- C Program Sort Example
- C Program sizeof & memcpy Example
- C Program Array Example
- C Program to Array
- C Program print fill zero & mod/div
- C Program Double, Float Equal
- C Program nested for
- C Program for
- C Program if
- C Program System
- C Program Operation
- C Program to printf & scanf
- Example C Program Cuda Malloc
- C Program Side Length
Learn More :
Vector
- Given a numerical value, check if at least one of the elements of the vector is equal to the numerical value if so, say where in the negative case, say that there is no.
- C program to Given a vector (integer or real), determine what is the maximum value of element what is the position in which this element is located
- C Program for distance vector algorithm to find suitable path for transmission
- Read 10 real numbers using a vector and show the least of them C Program
- C Program produsul scalar a doi vectori .
- C Program The dot product of two vectors
Example
- C Program String - Alphabet Count Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program Switch - Case Example
- C Program if - else if - else Example
- C Program Friend & Operator: Point2D Example
- C Program Recursion Example
- C Program Structure Example-2
- C Program Structure Example
- C Program Pointer Example
- C Program Function Example
- C Program String Example
- C Program Character Example
- C Program sizeof & memcpy Example
- C Program Array Example
- C Program Side Length Example
- C Program Pipes()
- C Program 0-1000 to Ordinary
- C Program Roboturtle
- C Program to Destroys a Mailbox
- C Program to Destroys a semaphore
- C Code for Testing the pH Meter V1.0
- APA 102c test program
- How To Retarget the C Library printf function to the UART.