Skip to main content

Posts

Showing posts with the label Operator Overloading

C++ Program On Function Overloading-Find Area of Cube, Sphere.

This is a sample C++ program on function overloading. Using function overloading, this program can find area of figure like Cube, Sphere. #include <iostream.h> #include <conio.h> #include <math.h> void volume (int); void volume (float); void main() {     int a, r,ch;     cout<<" *********MENU********* "<<'\n';     cout<<" 1. Cube "<<'\n';     cout<<" 2. Sphere "<<'\n';     cout<<" Enter Choice: ";     cin>>ch;     if (ch==1)        {            cout<<" Enter Side: ";            cin>>a;            volume(a);        }     else if(ch==2)        {            cout<<" Enter Radius: ";           ...

C++ Program On Operator Overloading-Cube Of A Number.

This is a sample C++ Program on Operator Overloading. Using Operator Overloading, this program will find Cube of a number. #include <iostream.h> #include <conio.h> class Calc {     public:        int operator ( ) (int num)          {              int cb = num * num * num;              return (cb);          } }; int main ( ) {     clrscr ( );     Calc cube;     int a, res;     cout<<" Enter A Number: ";     cin>>a;     res = cube (a);     cout<<" Cube Of " << a << " Is = " << res ;     getch ( );     return 0; } OUTPUT: Enter A Number: 6 Cube Of 6 Is = 216

C++ Program Using Operator Overloading-Add Complex Numbers

This is a sample program on operator overloading. Using operator overloading, here we add two complex numbers. #include <iostream.h> #include <conio.h> #include <iomanip.h> class complex {     private:        double real, img;     public:        complex ( )          {              real = img = 0.0;          }        void read ( )          {              cout<<"\n Enter Real & Imaginary Part: ";              cin >> real >> img;          }        complex operator + (complex cc2)          {              complex temp;             ...

C++ Program On Operator Overloading-Compare Two Strings.

This is a sample program on operator overloading. Using operator overloading, here the user enters a string and compare it with a predefined string. #include <iostream.h> #include <conio.h> #include <string.h> class string {     char st [30];     public:        string ( )          {              strcpy (st, " ");          }        string (char s [ ] )          {              strcpy (st, s);          }        void display ( )          {              cout << st;          }        int operator >= (string);        void read ( )         ...

C++ Program On Operator Overloading-Concatenate Two Strings.

This is a sample C++ program on Operator Overloading. Using Operator Overloading, here we will concatenate two strings. #include <iostream.h> #include <conio.h> #include <string.h> #define SIZE 40 class string {     char st [SIZE];     public:       string ( )         {            strcpy (st, " ");         }       string (char s[ ] )         {            strcpy (st, s);         }       void show ( )         {            cout << st;         }     string operator+ (string); }; string string :: operator+ (string ss2) { string temp; strcpy (temp.st, st); strcat (temp.st, ss2.st); return (temp); } int main ( ) { clrscr ( ); string s1 =...