C++: Functions friend and overloading mathematical operators - Applications solved


1) Required to implement the concept of two-dimensional vector. In this sense we say that two-dimensional vector is an ordered system of two real numbers. The vectors we define the following operations:
- Operation of attribution of values ​​in array elements
- The sum of two vectors;
- Difference of two vectors;
- The product of a vector and a scalar;
- A vector negativity;
- Scalar product of two vectors;
- Display the vector components in the form of a pair of elements.

Write a program that reads two pairs of numbers that represent components of vectors a and b, then calculates and displays:
- Sum: a + b;
- Difference: a-b;
- Amount: 3 * a +4 * b;
- Difference: a * 2 b * 4;
- Amount:-a-b;
- Scalar product of two vectors.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
 
class vb
{
	float e1,e2; //vector elements
	public:
	//constructor initialization parameter
	vb(float, float);
	//rewriting default constructor initialization
	vb(){}
	//sum of two vectors: r=vb1+vb2
	vb operator+(vb);
	//negativity of vector: r=-vb
	vb operator-();
	//difference of two vectors: r=vb1-vb2
	vb operator-(vb);
	//product of a vector and a number: r=vb*a
	vb operator*(int);
	//product of a number and a certain vector: r=a*vb
	friend vb operator*(int, vb);
	//scalar product of two vectors r=(vb1,vb2)
	double operator*(vb);
	// read a vector
	void read(char*);
	//displaying a vector
	void display(char*);
};
 
inline vb::vb(float a, float b)
{
	e1=a;
	e2=b;
}
 
vb vb::operator + (vb v) //sum of two vectors
{
	vb r;
	r.e1=e1+v.e1;
	r.e2=e2+v.e2;
	return r;
}
 
vb vb::operator - () //negativity of vector
{
	vb r;
	r.e1=-e1;
	r.e2=-e2;
	return r;
}
 
vb vb::operator - (vb v) //difference of two vectors
{
	return (*this)+(-v);
}
 
vb vb::operator * (int a) //product of a vector and a number
{
	vb r;
	r.e1=a*e1;
	r.e2=a*e2;
	return r;
}
 
vb operator * (int a, vb v) //product of a number and a vector
{
	return v*a;
	/** operator is overloaded for the product of a vector and
 a number by the function previous */
}
 
double vb::operator * (vb v) //scalar product of two vectors
{
	return e1*v.e1+e2*v.e2;
}
 
void vb::read(char *s)
{
	cout<<"\nEnter elements vector "<<s<<":\n";
	cout<<"\tfirst element: "; cin>>e1;
	cout<<"\tThe second element: "; cin>>e2;
}
 
void vb::display(char* string)
{
	cout<<"\nVector "<<string<<": ("<<e1<<","<<e2<<")"<<endl;
}
 
void main()
{
	vb a;
	a.read("a");
	a.display("a");
	cout<<"Elements vector <b> we directly enter to initialization.";
	vb b(2.5,1);
	b.display("b");
	vb c;
	c=a+b;
	c.display("a+b");
	c=a-b;
	c.display("a-b");
	c=3*a+4*b;
	c.display("3*a+4*b");
	c=a*2-b*4;
	c.display("a*2-b*4");
	c=-a-b;
	c.display("-a-b");
	cout<<"\nCartesian product: "<<a*b<<endl;
 
	int g;
	cin >>g;
}
2) Write a program that reads from the keyboard two arrays and calculates sum, difference and transposed them.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
 
class arrays
{
	int n; //no. lines, or columns, the arrays is square
	int m[10][10];
	public:
	arrays();
	void create(int x, char* s);
	arrays operator +(arrays a); //addition of two arrays
	arrays operator -(arrays a); //difference of two arrays
	arrays operator ~(); //transposes of an array
	void display(char* s);
};
 
inline arrays::arrays()
{
	n=0;
}
 
void arrays::create(int x, char* s)
{
	cout<<"Enter data arrays "<<s<<":\n";
	n=x;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
		{
			cout<<s<<"["<<i+1<<","<<j+1<<"]=";
			cin>>m[i][j];
		}
}
 
arrays arrays::operator + (arrays a)
{
	arrays temp;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			temp.m[i][j]=m[i][j]+a.m[i][j];
	temp.n=n;
	return temp;
}
 
arrays arrays::operator - (arrays a)
{ 
	arrays temp;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			temp.m[i][j]=m[i][j]-a.m[i][j];
	temp.n=n;
	return temp;
}
 
arrays arrays::operator ~ ()
{
	arrays temp;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			temp.m[i][j]=m[j][i];
	temp.n=n;
	return temp;
}
 
void arrays::display(char *s)
{
	cout<<"\nThe arrays "<<s<<" is:"<<endl;
	int i,j;
	for(i=0;i<n;i++)
	{
		cout<<'\t';
		for(j=0;j<n;j++)
		cout<<m[i][j]<<" ";
		cout<<'\n';
	}
}
 
void main()
{
	arrays a,b,t,s,d;
	int x;
	cout<<"Enter the size of the two arrays: ";
	cin>>x;
	a.create(x,"A");
	b.create(x,"B");
	cout<<endl;
	a.display("A");
	b.display("B");
	s=a+b;
	s.display("A+B");
	d=a-b;
	d.display("A-B");
	t=~a;
	t.display("~A");
 
	int g;
	cin >>g;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.