#include<iostream.h>
#include<conio.h>
class atype{
int a[3];
public:
atype(int i,int j,int k)
{
a[0]=i;
a[1]=j;
a[2]=k;
}
int operator[](int i){return a[i];}
};
void main()
{
atype ob(1,2,3);
cout<<ob[1];
getch();
}
Output:
2
Example:2
#include<iostream.h>
#include<conio.h>
class atype{
int a[3];
public:
atype(int i,int j,int k)
{
a[0]=i;
a[1]=j;
a[2]=k;
}
int &operator[](int i){
if(i<0||i>2){cout<<"Error";}
return a[i];
}
};
void main()
{
atype ob(1,2,3);
cout<<ob[1];
ob[1]=34;
ob[3]=33;
getch();
}
Example: 3
When overloading() it is a new way to call a function
#include<iostream.h>
#include<conio.h>
class atype{
int a[3];
public:
atype(int i,int j,int k)
{
a[0]=j;
a[1]=j;
a[2]=k;
}
void show()
{
cout<<a[0]<<""<<a[1]<<""<<a[2]<<"";
}
atype operator()(int i,int j,int k){
a[0]=i;
a[1]=j;
a[2]=k;
return *this;
}
};
void main()
{
clrscr();
atype ob(1,2,3);
ob(5,6,7);
ob.show();
getch();
}
Output:
567
#include<conio.h>
class atype{
int a[3];
public:
atype(int i,int j,int k)
{
a[0]=i;
a[1]=j;
a[2]=k;
}
int operator[](int i){return a[i];}
};
void main()
{
atype ob(1,2,3);
cout<<ob[1];
getch();
}
Output:
2
Example:2
#include<iostream.h>
#include<conio.h>
class atype{
int a[3];
public:
atype(int i,int j,int k)
{
a[0]=i;
a[1]=j;
a[2]=k;
}
int &operator[](int i){
if(i<0||i>2){cout<<"Error";}
return a[i];
}
};
void main()
{
atype ob(1,2,3);
cout<<ob[1];
ob[1]=34;
ob[3]=33;
getch();
}
Example: 3
When overloading() it is a new way to call a function
#include<iostream.h>
#include<conio.h>
class atype{
int a[3];
public:
atype(int i,int j,int k)
{
a[0]=j;
a[1]=j;
a[2]=k;
}
void show()
{
cout<<a[0]<<""<<a[1]<<""<<a[2]<<"";
}
atype operator()(int i,int j,int k){
a[0]=i;
a[1]=j;
a[2]=k;
return *this;
}
};
void main()
{
clrscr();
atype ob(1,2,3);
ob(5,6,7);
ob.show();
getch();
}
Output:
567
No comments:
Post a Comment