Professional Documents
Culture Documents
h>
#include<conio.h>
class X{
};
void operator!(X x){
cout<<" Overloaded !(x)\n";
}
class Y{
int x;
public:
void operator!();
};
void Y::operator!(){
Y y;
cout<<"y. Overloaded !( )";
}
main(){
clrscr();
X x;
Y y;
!x;
!y;
}
Output:
Overloaded !(x)
y.Overloaded!()
#include<iostream.h>
#include<conio.h>
class Point{
int x,y;
public:
Point();
void operator-();
};
Point::Point(){
x=10;
y=15;
void Point::operator-(){
x=~x;
y=~y;
cout<<x<<" "<<y;
main(){
clrscr();
Point y;
-y
getch();
Output:
-11 -16
#include<iostream.h>
#include<conio.h>
class Point{
public:
int x,y;
public:
Point();
Point operator-();
};
Point::Point(){
x=2;
y=10;
Point Point::operator-(){
//Y y;
x=~x;
y=~y;
//cout<<x<<" "<<y;
return *this;
main(){
clrscr();
//X x;
Point y;
//!x;//operator!(x)
y=-y;//y.perator!()
cout<<y.x<<y.y;
getch();
Output:
-3-11
#include<iostream.h>
#include<conio.h>
class Point{
public:
int x;
public:
Point();
int operator^(Point);
};
Point::Point(){
cin>>x;
int pw=1;
for(int i=1;i<=p.x;i++)
pw*=x;
return pw;
main(){
clrscr();
cout<<"Enter value of x\n";
Point x;
Point y;
getch();
Output:
Enter value of x
Enter value of y
11
Result is 2048
#include<iostream.h>
#include<conio.h>
class Date{
int d,m,y;
public:
Date();
friend ostream &operator<<(ostream &, Date &);
friend istream &operator>>(istream &, Date &);
}
Date::Date(){
d=m=y=0;
}
ostream &operator<<(ostream &o, Date &d){
o<<"\n"<<d.d<<"-"<<d.m<<"-"<<d.y;
return o;
}
istream &operator>>(istream &i,Date &d){
i>>d.d>>d.m>>d.y;
return i;
}
main(){
Date d;
cin>>d;
cout<<d;
}
#include<iostream.h>
#include<conio.h>
class Date{
int d,m,y;
public:
Date();
friend ostream &operator<<(ostream &, Date &);
friend istream &operator>>(istream &, Date &);
friend Date operator-(Date,Date);
void show();
} ;
void Date::show(){
cout<<endl<<y<<" Year "<<m<<" Months "<<d<<" Days ";
}
Date::Date(){
d=m=y=0;
}
Date operator-(Date td,Date db){
Date D;
D.y=td.y-db.y;
if(td.m<db.m){
D.y=D.y-1;
td.m=td.m+12;
}
D.m=td.m-db.m;
if(td.d<db.d){
D.m=D.m-1;
td.d+=30;
}
D.d=td.d-db.d;
return D;
}
ostream &operator<<(ostream &o, Date &d){
o<<"\n"<<d.d<<"-"<<d.m<<"-"<<d.y;
return o;
}
istream &operator>>(istream &i,Date &d){
cout<<"Enter Date:\n";
i>>d.d>>d.m>>d.y;
return i;
}
main(){
clrscr();
Date db;
cin>>db;
cout<<db;
Date td;
cout<<"\nEnter todays Date ...\n";
cin>>td;
cout<<td;
Date a=td-db;
a.show();
getch();
}
Type conversion
#include<iostream.h>
#include<conio.h>
class Student{
int rno;
public:
Student(int);
void display();
};
/* following constructor builds Student type object from int */
Student::Student(int r){
rno=r;
}
void Student::display(){
cout<<"Roll No "<<rno<<endl;
}
main(){
Student s(5); //Implicite Call to constrctor…
s.display();
Student s1;
S1=10; // Implicite Call to constrctor…
S1.display();
Student s2=Student(100); //Explicite Call to constrctor…
S2.display();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Student{
char nm[10];
public:
Student(){
strcpy(nm,"\0");
}
void display();
void operator =(char []);
};
void Student::operator =(char s[]){
strcpy(nm,s);
}
void Student::display(){
cout<<"Name is "<<nm<<endl;
}
main(){
clrscr();
Student st;
st="DJ";
st.display();
getch();
}
Class to Basic Type…
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Test{
int a;
public:
Test(){
a=10;
}
operator int();
};
Test::operator int(){
return a;
}
main(){
Test t;
clrscr();
int i=t; // or int i=int(t)
cout<<"i="<<i;
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
class String{
char a[10];
public:
String(){
strcpy(a,"DJ");
}
operator char*();
};
String::operator char*(){
return a;
}
main(){
String s;
clrscr();
char *n=s;
cout<<"Name = "<<n;
getch();
}
…Class to Class…