Tuesday, 9 June 2015

Program to illusrate data conversion b/w built-in data types and user defined data types (int & float)



Code for Program to illusrate data conversion b/w built-in data types and user defined data types (int & float) in C++ Programming



 



 



 
 #include<iostream.h>
 #include<conio.h>


 /*************************************************************************///-----------------------------  distance  ------------------------------///*************************************************************************/class distance
    {
       private:
        int feet;
        float inches;

       public:
        distance()
           {
              feet=0;
              inches=0;
           }

        distance(float meter)
           {
              float f=3*meter;
              feet=f;
              inches=12*(f-feet);
           }

        distance(int f,float i)
           {
              feet=f;
              inches=i;
           }

        void show_distance()
           {
              cout<<"\t Value of feet = "<<feet<<endl;
              cout<<"\t Value of inches = "<<inches<<endl;
           }

        operatorfloat()
           {
              float m=inches/12;
              m+=(feet/3);
              return m;
           }
    };

 /*************************************************************************//*************************************************************************///-----------------------------  Main( )  -------------------------------///*************************************************************************//*************************************************************************/

 main()
    {
       clrscr();

       distance d_1(2.35);
       distance d_2;

       cout<<"\n ***** Basic to User ***** \n"<<endl;

       cout<<" Value of distance d_1 is : "<<endl;
       d_1.show_distance();

       d_2=1.00;

       cout<<"\n Value of distance d_2 is : "<<endl;
       d_2.show_distance();


       distance d_3(5,10.25);

       float m=float(d_3);

       cout<<"\n ***** User to Basic ***** \n"<<endl;

       cout<<" Value of distance d_3 is : "<<m<<endl;

       m=d_2;

       cout<<"\n Value of distance d_2 is : "<<m<<endl;

       getch();
       return 0;
    }

PROGRAM OF OVERLOADING ARITHMETIC OPERATORS ON OBJECTS OF CLASS FLOAT HAVING AN DATA MEMBERS OF TYPE FLOAT


Code for PROGRAM OF OVERLOADING ARITHMETIC OPERATORS ON OBJECTS OF CLASS FLOAT HAVING AN DATA MEMBERS OF TYPE FLOAT in C++ Programming



 



 
#include<iostream.h>
#include<conio.h>

class FLOAT
{
    float no;
    public:
    FLOAT(){}
    void getdata()
    {
        cout<<"\n ENTER AN FLOATING NUMBER :";
        cin>>no;
     }
     void putdata()
     {
        cout<<"\n\nANSWER IS                   :"<<no;
     }
     FLOAT operator+(FLOAT);
     FLOAT operator*(FLOAT);
     FLOAT operator-(FLOAT);
     FLOAT operator/(FLOAT);
};
FLOAT FLOAT::operator+(FLOAT a)
{
    FLOAT temp;
    temp.no=no+a.no;
    return temp;
}
FLOAT FLOAT::operator*(FLOAT b)
{
    FLOAT temp;
    temp.no=no*b.no;
    return temp;
}
FLOAT FLOAT::operator-(FLOAT b)
{
    FLOAT temp;
    temp.no=no-b.no;
    return temp;
}
FLOAT FLOAT::operator/(FLOAT b)
{
    FLOAT temp;
    temp.no=no/b.no;
    return temp;
}


main()
{
  clrscr();
  FLOAT a,b,c;
  a.getdata();
  b.getdata();

  c=a+b;
  cout<<"\n\nAFTER ADDITION OF TWO OBJECTS";
  c.putdata();
  cout<<"\n\nAFTER MULTIPLICATION OF TWO OBJECTS";
  c=a*b;
  c.putdata();
  cout<<"\n\nAFTER SUBSTRACTION OF TWO OBJECTS";
  c=a-b;
  c.putdata();
  cout<<"\n\nAFTER DIVISION OF TWO OBJECTS";
  c=a/b;
  c.putdata();
  getch();
}

Program to illustrate operator overloading for strings



Code for Program to illustrate operator overloading for strings in C++ Programming



 



 
#include<iostream.h>
 #include<string.h>
 #include<conio.h>

 /*************************************************************************///------------------------------  string  -------------------------------///*************************************************************************/classstring
    {
       private:
        char str[80];

       public:
        string()  { strcpy(str,"ttt"); }
        string(char s[])  { strcpy(str,s); }
        void display()  { cout<<str<<endl; }
        stringoperator+(string );
    };


 /*************************************************************************///--------------------------  operator+(string)  ------------------------///*************************************************************************/stringstring::operator+(string ss)
    {
       string temp;

       if(strlen(str)+strlen(ss.str)<80)
      {
         strcpy(temp.str,str);
         strcat(temp.str,ss.str);
      }

       else
      {
         cout<<"string overflow"<<endl;
         temp=0;
      }
       return temp;
    }


 main()
    {
       clrscr();

       string s1(" Best Wishes");
       string s2(" Happy New Year");
       string s3;

       s1.display();
       s2.display();

       s3=s1+s2;

       s3.display();

       getch();
       return 0;
    }

Program to find whether a year is a leap year or not


Code for Program to find whether a year is a leap year or not in C++ Programming



 



 
#include<iostream.h>
 #include<conio.h>


 main()
    {
       clrscr();

       int year;

       cout<<"\n Enter the year = ";
       cin>>year;

       if(year%4==0 || year%400==0 || year%100==0)
      cout<<"this is a leap year"<<endl;

       else
      cout<<"this is not a leap year"<<endl;

       getch();
       return 0;
    }

Program to illustrate the use of the break statement


Code for Program to illustrate the use of the break statement in C++ Programming



 



 
 #include<iostream.h>
 #include<conio.h>

 /*************************************************************************//*************************************************************************///-----------------------------  Main( )  -------------------------------///*************************************************************************//*************************************************************************/

 main()
    {
       clrscr();

       cout<<"\n ********  Break Statement  *********"<<endl;

       for(int count=1;count<=10;count++)
      {
         if(count==5)
        break;

         cout<<" "<<count;
      }

       cout<<"\n\n Counting was broken at count = "<<count<<endl;

       getch();
       return 0;
    }

Program that take font and background color and text input from a user and display it in center aligned


Code for Program that take font and background color and text input from a user and display it in center aligned in C++ Programming



 



 



 
#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

cout<<"\t0 - 7 ARE LIGHT COLORS\n\n";

cout<<"\t0 - 14 ARE DARK COLORS\n\n\n ";

cout<<"\nEnter value of TEXTCOLOR:-";

int tc;

cin>>tc;

cout<<"Enter value of TEXTBACKGROUND:-";

int tb,i;

cin>>tb;

clrscr();

textbackground(tb);

textcolor(tc);

clrscr();

cout<<"|";

for(i=0;i<39;i++)

{

cout<<"--";

}

cout <<"|";

cout<<" ";

for(i=0;i<25;i++)
cout<< "=";

cout<<"Program to display entered value in center";

for(i=0;i<25;i++)
cout<<"=";

cout<<endl;

cout<<"|";

for(i=0;i<39;i++)

{

cout<<"-- ";

}

cout<<"|";

char name[50];

cout<<"\n\nEnter Anything to Align it in Center:-";

cin>>name;

cout<<endl<<endl;

int c=0;

while ( name[c] != '\0' )

c++;

int space = (80 - c)/2;

for(i=1;i<space;i++)

cout<<" ";

cout<<name<<endl;

getch();

}
[/Code]

Program to illusrate the use of stream insertion and extraction operators


Code for Program to illusrate the use of stream insertion and extraction operators in C++ Programming



 



 
 #include<iostream.h>
 #include<conio.h>

 /*************************************************************************///------------------------------         --------------------------------///*************************************************************************/class number
    {
       private:
        int data_1;
        int data_2;

       public:
        number()
           {
              data_1=0;
              data_2=0;
           }

        number(int num_1,int num_2)
           {
              data_1=num_1;
              data_2=num_2;
           }

        friend ostream &operator<<(ostream &s,number &obj);
        friend istream &operator>>(istream &s,number &obj);
    };


 /*************************************************************************///-----------------  &operator<<(ostream &,number &)  -------------------///*************************************************************************/

 ostream &operator<<(ostream &s,number &obj)
    {
       s<<"("<<obj.data_1<<","<<obj.data_2<<")";
       return s;
    }

 /*************************************************************************///-----------------  &operator>>(istream &,number &)  -------------------///*************************************************************************/

 istream &operator>>(istream &s,number &obj)
    {
       s>>obj.data_1>>obj.data_2;
       return s;
    }

 /*************************************************************************//*************************************************************************///-----------------------------  Main( )  -------------------------------///*************************************************************************//*************************************************************************/

 main()
    {
       clrscr();

       number obj_1(5,6);
       number obj_2(7,8);
       number obj_3;

       cout<<"\n Value of obj_1 = "<<obj_1<<endl;
       cout<<"\n Value of obj_2 = "<<obj_2<<endl;

       cout<<"\n Enter the value of obj_3 : "<<endl;
       cin>>obj_3;
       cout<<"\n Value of obj_3 = "<<obj_3;

       getch();
       return 0;
    }