Archive
C++ Program to implement Operator Overloading (>>, <<) – Q19
Q19. Program to implement Operator Overloading (>>, <<):
Write a class which contains a string as private data member. Add a member function IsPalindrome that returns TRUE/FALSE depending upon whether the str is palindrome or not. Overload the <> operator to insert the string and print the string respectively along with appropriate message.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <string.h>
#include <conio.h>
enum bool {false, true};
class Cpal{
private:
char *str;
public:
Cpal(){}
bool IsPalindrome();
friend ostream& operator<<(ostream &, const Cpal &);
friend istream& operator>>(istream &, const Cpal &);
};
bool Cpal :: IsPalindrome(){
int len, i, j;
bool valid;
len = strlen(str);
for(i=0, j=len-1; i<len/2; i++, j--){
if(str[i] == str[j])
valid = true;
else{
valid = false;
break;
}
}
return valid;
}
istream& operator>>(istream &istr, const Cpal &Opal){
return istr.get(Opal.str, 80);
}
ostream& operator<<(ostream &ostr, const Cpal &Opal){
return ostr<<Opal.str;
}
void main(){
Cpal Opal;
clrscr();
cout<<"\n Enter a String: ";
cin>>Opal;
cout<<"\n The String: ";
cout<<Opal;
if(Opal.IsPalindrome())
cout<<"\n\t is a Palindrome.";
else
cout<<"\n\t is not a Palindrome.";
getch();
}
Output:
Enter a String: able was i i saw elba
The String: able was i i saw elba
is a Palindrome.
Enter a String: my name is Manoj
The String: my name is Manoj
is not a Palindrome.
C++ Program to implement Operator Overloading for String Comparison with relational op (+, , ==) – Q18
Q18. Program to implement Operator Overloading for String Comparison with relational op (+, , ==):
Implement the operator overloading for following relational operators for two different strings
– Concatenate two strings using +
– Compare two strings with following overloaded operator >, <, ==
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
#include <string.h>
class CStrings{
private:
char str[20];
public:
void getdata();
friend CStrings operator+(CStrings&, CStrings&);
friend int operator<(CStrings&, CStrings&);
friend int operator>(CStrings&, CStrings&);
friend int operator==(CStrings&, CStrings&);
};
void CStrings :: getdata(){
cout<<"\n Enter a String: ";
cin>>str;
}
CStrings operator+(CStrings &s1, CStrings &s2){
CStrings Os;
strcpy(Os.str, s1.str);
strcat(Os.str, s2.str);
cout<<"\n Concatenation of Str1 & str2: "<<Os.str;
return Os;
}
int operator<(CStrings &s1, CStrings &s2){
if(strcmp(s1.str, s2.str) < 0)
return 1;
else
return 0;
}
int operator>(CStrings &s1, CStrings &s2){
if(strcmp(s1.str, s2.str) > 0)
return 1;
else
return 0;
}
int operator==(CStrings &s1, CStrings &s2){
if(strcmp(s1.str, s2.str) == 0)
return 1;
else
return 0;
}
void main(){
clrscr();
CStrings Ostr, Ostr1, Ostr2;
cout<<"\n Enter the Information:-"<<endl;
Ostr1.getdata();
Ostr2.getdata();
cout<<"\n String Concatenation:-"<<endl;
Ostr = Ostr1 + Ostr2;
cout<<"\n String Comparison:-"<<endl;
if(Ostr1 < Ostr2)
cout<<endl<<"\t str1 is less than str2";
else if(Ostr1 > Ostr2)
cout<<endl<<"\t str1 is greater than str2";
else if(Ostr1 == Ostr2)
cout<<endl<<"\t str1 and str2 are equal.";
getch();
}
Output:
Enter the Information:-
Enter a String: manoj
Enter a String: pandey
String Concatenation:-
Concatenation of Str1 & str2: manojpandey
String Comparison:-
str1 is less than str2
C++ Program to implement Operator Overloading with binary arithmetic op (*,+,-) – Q17
Q17. Program to implement Operator Overloading -with binary arithmetic op (*,+,-):
Define a class complex with two data members real & imag.
Perform the following operations:
– Find the sum of two complex numbers
– Find the difference of two complex numbers
– Find the Product of two complex numbers
– Find the Product of a complex number with a constant
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
class Ccomplex{
private:
double real, img;
public:
Ccomplex(){}
Ccomplex(double r, double i){
real = r;
img = i;
}
friend Ccomplex operator+(Ccomplex &, Ccomplex &);
friend Ccomplex operator-(Ccomplex &, Ccomplex &);
friend Ccomplex operator*(Ccomplex &, Ccomplex &);
friend Ccomplex operator*(Ccomplex &, double);
friend ostream& operator<<(ostream &, Ccomplex &);
friend istream& operator>>(istream &, Ccomplex &);
};
Ccomplex operator+(Ccomplex &c1, Ccomplex &c2){
return Ccomplex(c1.real + c2.real, c1.img + c2.img);
}
Ccomplex operator-(Ccomplex &c1, Ccomplex &c2){
return Ccomplex(c1.real - c2.real, c1.img - c2.img);
}
Ccomplex operator*(Ccomplex &c1, Ccomplex &c2){
return Ccomplex(c1.real * c2.real, c1.img * c2.img);
}
Ccomplex operator*(Ccomplex &c1, double num){
return Ccomplex(c1.real * num, c1.img * num);
}
ostream& operator<<(ostream &Mout, Ccomplex &Oc){
return Mout<<"\n Real: "<<Oc.real<<"\n Img: "<<Oc.img;
}
void main(){
Ccomplex Oc;
Ccomplex Oc1(3.1, 4.1);
Ccomplex Oc2(4.2, 5.7);
double num= 10;
clrscr();
cout<<"\n Sum of two Complex No's:- ";
Oc = Oc1 + Oc2;
cout<<Oc;
cout<<"\n\n Difference of two Complex No's:- ";
Oc = Oc1 - Oc2;
cout<<Oc;
cout<<"\n\n Product of two Complex No's:- ";
Oc = Oc1 * Oc2;
cout<<Oc;
cout<<"\n\n Product of Complex No with Constant:-";
Oc = Oc1 * num;
cout<<Oc;
getch();
}
Output:
Sum of two Complex No’s:-
Real: 7.3
Img: 9.8
Difference of two Complex No’s:-
Real: -1.1
Img: -1.6
Product of two Complex No’s:-
Real: 13.02
Img: 23.37
Product of Complex No with Constant:-
Real: 31
Img: 41
C++ Program to implement Operator Overloading with Unary Operators (++, –) – Q16
Q16. Program to implement Operator Overloading with Unary Operators (++, –):
Define a DATE class with month, day & year as the private data member.
Implement Operator Overloading for finding the next day of given date.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
class CDATE{
private:
int dd, mm, yy;
int _days;
static int months[12];
public:
void Init(){
cout<<"\n Enter a Date (dd mm yyyy): ";
cin>>dd>>mm>>yy;
}
void operator++(){
_days = DaysThisMonth(mm, yy);
if(dd >=_days){
if(mm >= 12){
yy += 1;
mm = 1;
}
else
mm += 1;
dd = 1;
}
else
dd += 1;
cout<<"\n Next Date: "<<dd<<":"<<mm<<":"<<yy;
}
int DaysThisMonth(int, int);
};
int CDATE :: months[12] = { 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int CDATE :: DaysThisMonth(int mm, int yy){
if (mm != 2)
return months[mm-1];
if (yy % 4) // Not leap year
return 28;
if (yy % 100) // It is leap year
return 29;
if (yy % 400) // Not leap year
return 28;
return 29; // It is leap year
}
void main(){
CDATE dt;
clrscr();
dt.Init();
char ch;
while(ch != 'y'){
++dt;
cout<<"\t\t\t Quit (y/n) ?: ";
cin>>ch;
}
cout<<"\n\n Press Any Key to Exit ...";
getch();
}
Output:
Enter a Date (dd mm yyyy): 26 12 2003
Next Date: 27:12:2003 Quit (y/n) ?: n
Next Date: 28:12:2003 Quit (y/n) ?: n
Next Date: 29:12:2003 Quit (y/n) ?: n
Next Date: 30:12:2003 Quit (y/n) ?: n
Next Date: 31:12:2003 Quit (y/n) ?: n
Next Date: 1:1:2004 Quit (y/n) ?: n
Next Date: 2:1:2004 Quit (y/n) ?: n
Next Date: 3:1:2004 Quit (y/n) ?: n
Next Date: 4:1:2004 Quit (y/n) ?: n
Next Date: 5:1:2004 Quit (y/n) ?: y
Press Any Key to Exit …




