Class Template with Overloaded Operators

The class template can also be declared for a class having operator overloaded member functions. The syntax for declaring operator overloaded functions is the same as class template members and overloaded functions.

//template class for operator overloading complex class
#include <iostream.h>
template<class T>
class Complex
{
T real, T imag;
void getdata()
{
cin>>real>>imag ;}
void putdata()
{
cout<<real<<imag;}
Complex operator + (Complex C2);
};
template<Class T>
Complex<T>Complex<T>::operator+(Complex<T>(2))
{
Complex<T>+;
t.real=real+C2.real;
t.imag=imag+C2.imag;
return(t);
}
void main()
{
Complex<int>C1,C2,C3;
C1.getdata();
C2.getdata();
C3=C1+C2;
C3.putdata();
Complex<float>C4,C5,C6;
C4.getdata();
C5.getdata();
C6=C4+C5;
C6.putdata();
}
Read More Topics
Pure virtual function in C++
Virtual base class
Operator overloading in C++
Static member function in C++

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply