Showing posts with label Polymorphism. Show all posts
Showing posts with label Polymorphism. Show all posts

Sunday, September 4, 2011

String compare using operator overloading


In C Language, we use strcmp() or strcmpi() functions to compare two strings. In C++ programs also we use these functions to perform the same job. Operator overloading in C++ helps us to use equality operator to compare two strings in C++ programs.

Here is a C++ program to compare two strings using operator overloading.

#include< iostream.h >
#include< conio.h >
#include< string.h >
const int SIZE=40;
class String
{
private:
char str [SIZE];
public:
String ()
{
strcpy (str," ");
}
String (char ch [])
{
strcpy (str, ch);
}
void getString ()
{
cout<< "Enter the string: -";
cin.get (str, SIZE);
}
int operator == (String s)
{
if (strcmp (str, s.str)==0)
return 1;
else
return 0;
}
};
void main ()
{
clrscr ();
String s1, s2, s3;
s1="Satavisha";
s2="Suddhashil";
s3.getString ();
if (s3==s1)
cout<< "1st and 3rd string are same.";
else if (s2==s3)
cout<< "2nd and 3rd string are same.";
else
cout<< "All strings are unique.";
getch ();
}

Operator overloading is an important chapter in and Students can ask any related question through this blog.

Overloading binary and unary operators in C++ programs


We are working on overloading of operators in C++ programs. In the following program both unary and binary operators are overloaded . I think this C++ program would help students. 

#include< iostream.h >
#include< conio.h >
#include< stdio.h >
#include< string.h >
class Count
{
private:
char str[40];
char address[40];
int age;
public:
Count()
{
strcpy(str,"***");
strcpy(address,"!!!!");
age=0;
}
void operator ++ ()
{
cout<< "\nEnter Your name:-";
gets(str);
}
void operator--()
{
cout<< "\nEnter your address( Town/City):-";
cin>>address;
}
void operator +()
{
cout<< "\nEnter your age:-";
cin>>age;
}
void operator -()
{
cout<< "\nName="<< str;
cout<< "\nAddress="<< address;
cout<< "\nAge="<< age;
}
};
void main ()
{
clrscr ();
Count c1;
char ch;
cout<< "\nWant to enter name (y/n):-";
cin>>ch;
if(ch=='y')
++c1;
cout<<"\nWant to enter address (y/n):-";
cin>>ch;
if(ch=='y')
--c1;
cout<< "\nWant to enter age (y/n):-";
cin>>ch;
if(ch=='y')
+c1;
-c1;
getch ();
}

Overloading binary operators in C++ programs


Operator overloading is a very useful chapter in C++. Here is a C++ program demonstrating the technique of overloading binary operator. Hope this will be helpful to students.

#include< iostream.h >
#include< conio.h >
#include< string.h >
class Count
{
private:
char str1 [10], str2 [10];
public:
void get ()
{
cout<< "Enter the 1st string: -";
cin>>str1;
cout<< "Enter the 2nd string: -";
cin>>str2;
}
char *operator + ()
{
strcat (str1, str2);
return str1;
}
};
void main ()
{
clrscr ();
Count c1;
char str3 [20];
c1.get ();
strcpy (str3, +c1);
cout<< "\nAfter Concatenate  ="<< str3;
getch ();
}

We have used different string manipulating functions in C++ programs. Suppose we wand to append one string at the end of other string, we normally invoke library function strcat(). The same operation can done using the + operator through operator overloading in C++ programs. Similar related operations can also be done.

#include< iostream.h >
#include< conio.h >
#include< string.h >
class String
{
private:
char str [100];
public:
String ()
{
strcpy (str," ");
}
String (char ch [])
{
strcpy (str, ch);
}
void display ()
{
cout<< endl<< str;
}
String operator + (String s)
{
String t;
strcpy (t.str, str);
strcat (t.str, s.str);
return t;
}
};
void main ()
{
clrscr ();
String s1, s2,s3;
s1="Satavisha ";
s2="Suddhashil";
s1.display ();
s2.display ();
s3=s1+s2;
s3.display ();
getch ();
}


Any doubt or question can be asked through comments.

Operator Overloading in C++ programs


Operator overloading is one of the most exciting feature of object-oriented programming. Operator overloading provides normal C++ operators, such as +, -, * etc additional meanings when they are applied to user defined data types. Normally we know that operators like + works only with basic types like int, float etc. Using overloading we can apply these operators on objects also.

Here is one of the C++ programs to demonstrate operator overloading.

#include< iostream.h>
#include< conio.h>
class Count
{
private:
int count;
public:
Count ()
{
count=0;
}
int getCount ()
{
return count;
}
void operator ++ ()
{
count++;
}
};
void main ()
{
clrscr ();
Count c1, c2;
cout<< "\nc1="<< c1.getCount ();
cout<< "\nc2="<< c2.getCount ();
++c1;
++c2;
cout<< "\nc1="<< c1.getCount ();
cout<< "\nc2="<< c2.getCount ();
getch ();
}

The unary operator ++ acts on objects c1 and c2 because the keyword operator is used   and which is a must to overload the ++ operator in the function operator ++ ().  This syntax tells the compiler to call the member function whenever the operator ++ is encountered, provided the operand is of type Count, the class here. If the operator ++ is used on variables of predefined type like int or float then the compiler will use its built-in routine to increment the value of the variable of int or float type.

Saturday, September 3, 2011

Operator Overloading in C++ programs Part I


Operator overloading is one of the most exciting feature of object-oriented programming and this is included in cbse syllabus. Operator overloading provides normal C++ operators, such as +, -, * etc additional meanings when they are applied to user defined data types. Normally we know that operators like + works only with basic types like int, float etc. Using overloading we can apply these operators on objects also.

Here is one program to demonstrate operator overloading.

#include< iostream.h>
#include< conio.h>
class Count
{
private:
int count;
public:
Count ()
{
count=0;
}
int getCount ()
{
return count;
}
void operator ++ ()
{
count++;
}
};
void main ()
{
clrscr ();
Count c1, c2;
cout<< "\nc1="<< c1.getCount ();
cout<< "\nc2="<< c2.getCount ();
++c1;
++c2;
cout<< "\nc1="<< c1.getCount ();
cout<< "\nc2="<< c2.getCount ();
getch ();
}

The unary operator ++ acts on objects c1 and c2 because the keyword operator is used   and which is a must to overload the ++ operator in the function operator ++ ().  This syntax tells the compiler to call the member function whenever the operator ++ is encountered, provided the operand is of type Count, the class here. If the operator ++ is used on variables of predefined type like int or float then the compiler will use its built-in routine to increment the value of the variable of int or float type.

Subscribe via email

Enter your email address:

Delivered by FeedBurner