Thursday, October 16, 2014

C++ Programs



The OOP with C++ is an introductory course to C++ and the best efforts have been taken cover most of the topics specified in the VTU curriculum.

The Notes of OOP with C++ for III Semester CSE/ISE of VTU may refer to the following link:
In case of mistakes, the author (i.e. myself) doesn't take sole loss incurred by any individual after referring to the documents, it is only for academic purposes the notes have been typed and consolidated in its present form, the further versions may be soon released based on the feedback obtained. Thank you and All the Best !.


Mail to get the C++ Notes to:     rajkumar.manju@gmail.com
C++ Lab Programs



Design, develop, and execute a program in C++ to create a class called DATE with methods to accept two valid dates in the form dd/mm/yy and to implement the following operations by overloading the operators + and -. After every operation the results are to be displayed by overloading the operator <<.
i. no_of_days = d1 – d2; where d1 and d2 are DATE objects, d1 >=d2 and no_of_days is an integer.
ii. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer.
                   
 


#include < iostream.h >
#include < stdlib.h >
#include < conio.h >
class date{
  int dd,mm,yy;
public:
  int leap;
  date(){dd=0;mm=0;yy=0;}
  date(int d,int m, int y){
    dd=d; mm=m;  yy=y;
    if(y%4==0 || y%400==0 && y%100!=0)
       leap=1;
    else
       leap=0;

    if(!leap && m==2 && d>28)
     {
       cout<<"Non leap year can't have more than 28 days...\n!!!error";
       getch();
       exit(0);
     }
    if(m>13)
     {
       cout<<"Month can't be greater than 12...\n!!!error";
       getch();
       exit(0);
     }
    if(d>get_month_days(m))
     {
       cout<<"Number of days exceeds in the month...\n!!!error";
       getch();
       exit(0);
     }
  }
  date operator+(int days);
  int operator-(date d);
  int get_month_days(int month);
  friend ostream& operator<<(ostream &out, date &d);
};

date date::operator+(int days)
{
 int i;
 for(i=1;i<=days;i++)
 {
  dd++;
  if(dd>get_month_days(mm) && mm<13 br="">   {
     mm++;
     if (mm==13) {mm=1; yy++;}
     dd=1;
   }
 }
 return *this;
}

int date::operator-(date d2)
{
  int diff=0, mon_days,month;
  if(yy   {
    cout<<"Not possible to find the difference\nSecond date is greater\n";
    return -1;
   }
  int diff=0;
  while(dd!=d2.dd || mm!=d2.mm || yy!=d2.yy)
  {
   d2.dd++;
   if(d2.dd>get_month_days(d2.mm) && d2.mm<13 br="">    {
     d2.mm++;        //incr month if days crosses the no. of days of the month
     if (d2.mm==13) {d2.mm=1; d2.yy++;}   
     d2.dd=1;                //reset days to 1 on incrementing month
    }
    diff++;
   }
return diff;
}

int date::get_month_days(int month)
{  int days;
   switch (month)
   {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:days=31; break;

    case 2:if(yy%4==0 || yy%400==0 && yy%100!=0)
         days=29;
       else
         days=28;
       break;
    case 4: case 6: case 9: case 11:days=30; break;
   }
   return days;
}
ostream& operator<<(ostream& out,date &d)
{
 out< return out;
}

int main()
{
 int no_of_days=0;
 int days=0;
 int day,mon,year;
 clrscr();
 cout<<"\nEnter the two dates in the format (dd mm yyyy)";
 cout<<"\nEnter first date:";
 cin>>day>>mon>>year;
 date d1(day,mon,year);
 cout<<"\nEnter the second date(less than first date):";
 cin>>day>>mon>>year;
 date d2(day,mon,year);
 cout<<"\nThe dates are:";
 cout< cout< no_of_days=d1-d2;
 cout<<"\nThe difference is :"< cout<<"\nEnter the no. of days to be added:";
 cin>>days;
 d1=d1+days;
 cout<<"\nThe new date is:";
 cout< getch();
return 0;
}



/*Design, develop, and execute a program in C++ to create a class called OCTAL, which has the
characteristics of an octal number.
Implement the following operations by writing an appropriate constructor and an overloaded operator +.
i. OCTAL h = x ; where x is an integer
ii. int y = h + k ; where h is an OCTAL object and k is an integer.
Display the OCTAL result by overloading the operator <<. Also display the values of h and y.
*/
#include < iostream >
#include < cmath >
using namespace std;
class OCTAL{
    int oct;
    int dec;
public:
    OCTAL(){
        oct=0;
        dec=0;
    }
    OCTAL(int n){
        oct = decimal_oct(n);
    }
    int decimal_oct(int);
    int octal_decimal(int);
    int operator+(int);
    friend ostream & operator<<(ostream& out,OCTAL Oct);
};


int OCTAL::decimal_oct(int n)
{
 int rem, i=1, octal=0;
    while (n!=0)            //decimal to octal
    {
        rem=n%8;
        n/=8;
        octal+=rem*i;
        i*=10;
    }
   return octal;
}

int OCTAL::octal_decimal(int n) /* Function to convert octal to decimal */
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(8,i);
        ++i;
    }
    return decimal;
}

int OCTAL :: operator+(int k)
{
    dec = octal_decimal(oct);
    return (dec+k);
}

ostream & operator <<(ostream &out, OCTAL O)
{
    out<    return out;
}
int main()
{
    int x,k,y;
    cout<<"\nEnter the value of x:";
    cin>>x;

    OCTAL h=x;  //invokes the single parameterized constructor
    cout<<"\nThe output of h=x in octal is:"<
    cout<<"\nEnter the value of k:";
    cin>>k;

    y=h+k;      //invokes the operator +() overloaded function
    cout<<"The output of y=h+k in octal is:"<
    return 0;
}






Design, develop, and execute a program in C++ to create a class called BIN_TREE that represents a Binary Tree, with member functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and demonstrate the traversals.


#include < iostream >
#include < cstdlib >
using namespace std;
class TREE
{
 public:
    int data;
    TREE *left;
    TREE *right;
};
class BIN_TREE
{
    TREE *tree;
    public:
        BIN_TREE()
        {
            tree=NULL;
        }
        void insert(int);
        void inorder(TREE *tree);
        void preorder(TREE *tree);
        void postorder(TREE *tree);
        TREE *tree_ret()
        {
            return tree;
        }
};
void BIN_TREE::insert(int item)
{
    TREE *cur,*prev,*temp;
    temp=new TREE;
    temp->data=item;
    temp->left=temp->right=NULL;
    if(tree==NULL)
    tree=temp;
    else
    {
        prev=NULL;
        cur=tree;
        while(cur!=NULL)
        {
            prev=cur;
            if(item
< temp->data)
              cur=cur->left;
            else
              cur=cur->right;
        }
        if(item < temp->data)
          prev->left=temp;
        else
          prev->right=temp;
    }
}
void BIN_TREE::inorder(TREE *tree)
{
    TREE *temp;
    temp=tree;
    if(temp!=NULL)
    {
        inorder(temp->left);
        cout<data<<"\t";
        inorder(temp->right);
    }
}
void BIN_TREE::preorder(TREE *tree)
{
    TREE *temp;
    temp=tree;
    if(temp!=NULL)
    {
        cout<data<<"\t";
        preorder(temp->left);
        preorder(temp->right);
    }
}
void BIN_TREE::postorder(TREE *tree)
{
    TREE *temp;
    temp=tree;
    if(temp!=NULL)
    {
        postorder(temp->left);
        postorder(temp->right);
        cout<data<<"\t";
    }
}

int main()
{
    BIN_TREE BT;
    TREE *tree;
    int ch;
    int item;
    while(1)
    {
        cout<<"\n 1.insert \n 2.display \n3.exit";
        cout<<"\n enter the choice";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"enter data to insert";
                cin>>item;
                BT.insert(item);
                break;
            case 2:
                cout<<"the contents of BT\n";
                cout<<"\ninorder\n";
                tree=BT.tree_ret();
                BT.inorder(tree);
                cout<<"\npreorder\n";
                tree=BT.tree_ret();
                BT.preorder(tree);
                cout<<"\npostorder\n";
                tree=BT.tree_ret();
                BT.postorder(tree) ;
            break;
            default:
                exit(0);
        }
    }
return 0;
}