OVERLOADING PREFIX AND POSTFIX INCREMENT

OVERLOADING PREFIX AND POSTFIX INCREMENT
#include<iostream>
using namespace std;
class counter{
private:
int count;
public:
counter(){//default constructor
count=0;
}
counter(int x){
    count=x;
}
int getcount(){
return count;
}
void operator++(){
count++; 
}
void operator ++(int){
count++;
}

};
int main(){
    counter s2(12);
cout<<"the value of constructor is = "<< s2.getcount()<<endl;
counter s3,s4;
++s3;
++s4;
    s4++;

cout<<" s3 "<<s3.getcount()<<endl;
cout<<" s4 "<<s4.getcount()<<endl;

}


OVERLOADING >> AND <<:
#include<iostream>
using namespace std;
class distanc{
private:
int height,width;
public:
distanc(){
height=0;
width=0;
}


friend ostream&operator<<(ostream &o,distanc & d);
friend istream&operator>>(istream &i,distanc & e);
};
ostream&operator<<(ostream &o,distanc & d){
o<<d.height<<endl;
o<<d.width<<endl;
return o;
};
istream&operator>>(istream &i,distanc & e){
i>>e.height>>e.width;
return i;
};
int main(){
cout<<"enter two numbers "<<endl;
distanc  s1;
cin>>s1;
cout<<"output of 2 numbers "<<endl;
cout<<s1;

}

Previous
Next Post »

Comments:

Disqus Shortname