//Write a program to overload the operator < and >such that it compares two strings //and returns the appropriate result .the string should be read and printed using the //overlloaded stream operator #include<iostream> #include<string.h> #include<stdio.h> #include<stdlib.h> using namespace std; class A{ public: char s1[100],s2[100]; public: int operator <(A &obj){ int n=strcmp(s1,obj.s2); if(n<0) return 1; else return 0; } int operator >(A &obj){ int n=strcmp(s1,obj.s2); if(n>0) return 1; else return 0; } friend istream& operator >> (istream &in,A &obj){ in>>obj.s1; in>>obj.s2; return in; } friend ostream& operator << (ostream &out,int n){ out<<n; return out; } }; int main() { A a; cin>>a; cout<<(a<a)<<endl; cout<<(a>a)<<endl; return 0; }
overloading less than(”) operator April 28, 2010
friend function implemented
//Write a program using friend function which will add two distances //stored in two different classes.The first class holds the distance // in m & cm and the second class holds the distance in feet & inch #include<iostream> using namespace std; class B; class A{ float m,cm,res; public: A():m(20.0),cm(2.0){} void fun(B obj); }; class B{ float feet,inch; public: B():feet(9),inch(3){} friend void A::fun(B obj); }; void A::fun(B obj){ res=m*100.0+cm+obj.feet*12.0*2.54+obj.inch*2.54; res=res/100; cout<<res<<"metre\n"; } int main() { A a; B b; a.fun(b); return 0; }
TopCoder SRM 147 Div2 February 13, 2010
TopCoder SRM 147 Div2
C++
–SUBMITTED–[149 points]
#include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> using namespace std; class CCipher{ public: string decode(string cipherText,int shift){ for(int i=0;cipherText[i]!='\0';i++) cipherText[i]='A'+(cipherText[i]-'A'+26-shift)%26; return cipherText; } }; /*int main() { CCipher c; string s=c.decode("VQREQFGT",2); cout<<s<<endl; return 0; } */
TopCoder SRM 262 – DivToZero C++ February 12, 2010
TopCoder Single Round Match 262 Round 1 – Division II, Level One
–AC–
c++
The format in which i submitte it :-
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; class DivToZero{ int i,j; char s[20]; public: string lastTwo(int num,int factor){ if(num%factor==0){ j=num%100; if(j<10) sprintf(s,"0%d",j); else sprintf(s,"%d",j); //cout<<j<<endl; return s; } else{ num=num/100; num=num*100; for(i=num;;i++) { if(i%factor==0) break; } j=i%100; //cout<<num<<endl; if(j<10) sprintf(s,"0%d",j); else sprintf(s,"%d",j); return s; } } };
here is a snapshot of the arena
The format in which i tested in g++ on my system for each of test case is below
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; class DivToZero{ int i,j; char s[20]; public: string lastTwo(int num,int factor){ if(num%factor==0){ j=num%100; if(j<10) sprintf(s,"0%d",j); else sprintf(s,"%d",j); //cout<<j<<endl; return s; } else{ num=num/100; num=num*100; for(i=num;;i++) { if(i%factor==0) break; } j=i%100; //cout<<num<<endl; if(j<10) sprintf(s,"0%d",j); else sprintf(s,"%d",j); return s; } } }; int main() { DivToZero a; string p=a.lastTwo(32442,99); cout<<p<<endl; return 0; }
Nested Friends c++
[..from Eckel c++]
Making a structure(or class) nested doesnot automatically give it access to private members.To accomplish this,we must follow a particular form, first declare (without defining ) the nested structure ,then declare it as s friend ,and finally define the structure.The structure definition must be separate from the friend declaration ,otherwise it would be seen by the compiler as a non-member.
Friend c++ demonstration
[..from C++ Eckel]
//Friend -demonstration #include<iostream> using namespace std; struct X; struct Y{ void f(X*); }; struct X{ private: int i; public: void initialize(); friend void g(X*,int);//global friend friend void Y::f(X*);//struct member friend friend struct Z; //entire struct is a friend friend void h(); };
Operators that we can’t overload.Why?
[..from Bruce Eckel c++]
There are certain operators that cannot be overloaded.The general reason for the restriction is safety.If these operators were overloadable ,it would somehow jeopardizez or break safety mechanisms,make things harder ,or confuse existing practise. (more…)
Recent Comments