C++: Special operator overloading - Applications solved


1) Make a program that creates a certain power class, that calculates result given by raising a number to a power (assuming a positive integer power exponent) using "this" pointer.
#include <iostream>
using namespace std;
#include<conio.h>

class power
{
	double b;
	int e;
	double val;
public:
	power(double base, int exp);
	double calculates() {return this->val ;}
};
power::power(double base, int exp)
{
this->b=base;
this->e=exp;
this->val=1;
if(exp==0) return;
for(;exp>0;exp--)
this->val=this->val*this->b;
}
void main()
{
	power x(4,3);
	power y(2.5,1);
	power z(5.7,0);
	cout<<x.calculates()<<endl;
	cout<<y.calculates()<<endl;
	cout<<z.calculates()<<endl;
	getch();
  
  int k;
  cin>>k;
}
2) Using a class members: a vector of 3 elements, initialized by a builder implement indexing operator. Required to perform a reliable index of arrays otherwise requires leaving the program.
#include <iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>

class index{
	int a[3];
public:
	index(int i,int j,int k)
	{
		a[0]=i;
		a[1]=j;
		a[2]=k;
	}
	int& operator[](int i);
};
int& index::operator[](int i)
{
	if(i<0 || i>2)
	{
		cout<<endl<<"Error limit."<<'\n';
		exit(1);
	}
	return a[i];
}
void main()
{
	index ob(1,2,3);
	for(int i=0;i<3;i++)
		cout<<ob[i]<<endl;
ob[2]=25; //this allocation is only possible because of the return value as reference
         
	cout<<ob[2];
	getch();
	ob[3]=30;
 
  int k;
  cin>>k;
}
3) Make a program that uses overloading operator "=".
#include <iostream>
using namespace std;
#include<conio.h>
#include<string.h>

class sir
{
	char* a;
public:
	//constructor
	sir (char b[]);
 
	//constructor de copiere
	sir (sir &x);
 
	void operator=(sir &x);
	void display(char *);
};
inline sir::sir(char b[])
{
	a=new char[strlen(b)+1];
	strcpy(a,b);
}
inline sir::sir(sir &x)
{
	a=new char[strlen(x.a)+1];
	strcpy(a,x.a);
}
void sir::operator=(sir& x)
{
	delete a;
	a=new char[strlen(x.a)+1];
	strcpy(a,x.a);
}
void sir::display(char *z)
{
	cout<<"The string "<<z<<" is: "<<a<<endl;
}
void main()
{
	sir a("a string ");
	cout<<"Enter a desired string: ";
	char s[256];
	cin.getline(s,sizeof(s),'\n');
	sir b(s);
	sir c=a;
	a.display("a");
	b.display("b");
	c.display("c");
	a=b;
	a.display("a");
	b.display("b");
	getch();
 
  int k;
  cin>>k;
}
4) Develop an abstract data type for processing a two-dimensional arrays, and achieving this major operations on this, by operator overloading.
#include <iostream>
using namespace std;
#include<conio.h>
#include<string.h>

class arrays
{
	int m,n; // m - number of rows, n - number of columns
	int* mat; // arrays, for which will allocate memory in the HEAP storage
public:
	arrays(int=1,int=1); // constructor initialization parameter
	int* operator()(int,int); /* Special operator overloading 'function call' for
	determination of the arrays element in a certain position, position on the parameter */
	friend istream& operator>>(istream&,arrays&); /* operator overloading 'extraction
	the flow 'for reading elements of arrays*/
	friend ostream& operator<<(ostream&,arrays); /*  operator overloading 'insertion
	flow 'to display elements of arrays*/
	friend arrays operator+(arrays&,arrays&); /* overloading operator + for adding
	two arrays*/
	friend arrays operator-(arrays&); /* overloading operator '-' to determine Opus
	of arrays*/
	friend arrays operator-(arrays&,arrays&); /* overloading operator '-' for difference
	two arrays*/
	friend arrays operator~(arrays&); // a transposed arrays
};
// constructor initialization parameter
inline arrays::arrays(int i,int j)
{
	m=i;
	n=j;
	mat=new int[m*n]; /*allocate enough memory in the HEAP
	to keep a whole array m * n elements*/
}
/* Special operator overloading "function call" to determine
an element of arrays from a certain position, position on the parameter */
int* arrays::operator()(int i,int j)
{
	return mat+i*n+j; // line the element of the position "i" and column "j" of arrays
}
// operator overloading 'extraction of flow' for reading elements of arrays
istream& operator>>(istream& x,arrays& a)
{
	for(int i=0;i<a.m;i++)
	for(int j=0;j<a.n;j++)
	{
		cout<<"\telem["<<i+1<<"]["<<j+1<<"]= ";
		x>>*a(i,j); // call operator '()' overloaded
	}
return x;
}
// operator overloading 'insertion flow' to display elements of arrays
ostream& operator<<(ostream& x,arrays a)
{
	for(int i=0;i<a.m;i++)
	{
		for(int j=0;j<a.n;j++)
		x<<'\t'<<*a(i,j);
		x<<endl;
	}
return x;
}
// overloading operator + for adding two arrays
arrays operator+(arrays& a,arrays& b)
{
	arrays c;
	c.m=a.m; //sau, c.m=b.m;
	c.n=a.n; //sau, c.n=b.n;
	for(int i=0;i<c.m;i++)
		for(int j=0;j<c.n;j++)
			*c(i,j)=*a(i,j)+*b(i,j);
	return c;
}
// overloading operator '-' for opposing a determination arrays
arrays operator-(arrays& a)
{
	arrays c;
	c.m=a.m;
	c.n=a.n;
	for(int i=0;i<c.m;i++)
		for(int j=0;j<c.n;j++)
			*c(i,j)=-*a(i,j);
	return c;
}
// overloading operator '-' for the difference of two arrays
arrays operator-(arrays& a,arrays& b)
{
	return a+(-b);
}
// overloading operator '~' for a transposed arrays
arrays operator~(arrays& a)
{
	arrays c;
	c.m=a.m;
	c.n=a.n;
	for(int i=0;i<c.m;i++)
		for(int j=0;j<c.n;j++)
			*c(j,i)=*a(i,j);
	return c;
}
void main()
{
	int m,n; // m - number of lines, n - number of columns
	cout<<"Enter size of arrays:\n";
	cout<<"\tnumber of lines: ";
	cin>>m;
	cout<<"\tnumber of columns: ";
	cin>>n;
arrays a(m,n),b(m,n); // call the constructor initialization parameter
                          
	cout<<"Read the arrays A:\n";
	cin>>a; // call the function that overloads operator '>>'
	cout<<"Read the arrays B:\n";
	cin>>b;
	cout<<"Display the arrays A:\n";
	cout<<a; // call the function that overloads operator '<<'
	cout<<"Display the arrays B:\n";
	cout<<b;
	cout<<endl<<"Enter the key to continue ..."<<endl;
	getch();
 
	cout<<"Display A+B:\n";
	cout<<a+b; // call the functions that overload operators '+' and '<<'
	arrays c(m,n);
	c=(-a)-(~b); // call the functions that overload operators '- unary', '- binary' and '~'
                      
	cout<<"Display expression -A - ~B:\n";
	cout<<c;
 
  int k;
  cin>>k;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.