C++ Programming Tutorial

 
 
 

Object Oriented Programming

Structures

Example of structures Passing structures to function Passing structure pointers to function Pointers and an array of structure Relationship between union and structure Array of structure Nested structures Self-referential structures Linked List

Classes

Classes with inline functions Classes without using inline functions Example of constructor in classes Passing values to constructor in classes Overloading of constructors in classes Example of destructor in classes Example of static class data Example of static member functions Constant objects, constant member functions Strings as member of classes Use of arrays as data items in classes Example of array of objects in classes Use of pointer this using header file this.h Obj initialize, assign by default member wise copy

Inheritance - Containership

Example of Inheritance Over riding base class member fn in derived Diff in public, protected, private inheritance Multi-level inheritance Multiple inheritance Example of containership

Virtual Functions - Polymorphism

Example of Polymorphism Example of Pure Virtual functions

Operator Overloading - Type Casting

Increment operator ++ overloading w/o return type Increment operator ++ overloading - return type Decrement operator -- overloading w/o return type Decrement operator -- overloading - return type Operator overloading for strings Comparision operator < overloading Binary operator + overloading creating new object Binary operator + overloading w/o creating object Binary operator - overloading creating object Binary operator - overloading w/o creating object Binary operator * overloading w/o creating object Stream insertion >> , extraction << operators Data conversion b/w built-in data types Data conversion - built-in, user defined data type Conversion- built-in, user defined data type- char Data conversion user defined data type - function Conversion user defined data type - constructor

Friend Functions and Classes

Use of friend functions Use of friend classes Operator overloading using friend function All Operator overloading using friend function Addition of 2 matrix using friend function Example of friend function of a class

Template Functions and Classes

Interchange value using function template Example of template classes Template based array input Template to find minimum value in an array Template of vector class

File Processing - Command Line Arguments

Open file File operation program - diff type of mode Read from one text file, writing in new text file File read, write, update and display operations Read decimal from file - convert into hexadecimal Read words from file and remove palindrome Read device data from text file and show extension Example of binary files

General Programs

Number of days b/w two given dates Capitalize first charcter of every word in string Copy n num of characters from one to another Word is palindrome or not using classes & pointers Simulation of the Solar System Add and Subtract functions on Big Number Class
 
 

Image

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



 //------------------------------  day  ----------------------------------//


 class day
    {
       private:
        int date;
        int month;
        int year;

       public:
        day()  {  date=0; month=0; year=0;  }
        void get_date();
        void show_days(day,day);
        void show_date();
        int next_month(int,int);
        int next_year(int);
        int operator>(day);
    };


 //---------------------------  get_date( )  -----------------------------//


 void day::get_date()
    {
       cout<<\"\\tEnter the date = \";
       cin>>date;

       cout<<\"\\tEnter the month = \";
       cin>>month;

       cout<<\"\\tEnter the year = \";
       cin>>year;
    }


 //--------------------------  show_date( )  -----------------------------//


 void day::show_date()
    {
       cout<<\"\\n\\t Date is     \";
       cout<<((date<10)?\"0\":\"\")<<this->date<<\" : \";
       cout<<((month<10)?\"0\":\"\")<<this->month<<\" : \"<<this->year<<endl;
    }


 //---------------------------  show_days(day,day)  ----------------------//


 void day::show_days(day d1,day d2)
    {
    long d=0;

    if(d2.date<d1.date && d2.month!=d1.month && d2.year>=d1.year)
       {
          d2.date+=next_month(d2.month-1,d2.year);
          d2.month--;
       }

    for(int count_1=d1.date;count_1<d2.date;count_1++)
       d++;

    if(d2.month<d1.month && d2.year>d1.year)
       {
          d2.month+=12;
          d2.year--;
       }

    for(int count_2=d1.month;count_2<d2.month;count_2++)
       d+=next_month(count_2,d1.year);

    for(int count_3=d1.year;count_3<d2.year;count_3++)
       d+=365+next_year(count_3);

    cout<<\"\\t Number of days b/w date1 & date2 = \"<<d<<\" days\"<<endl;
    }


 //---------------------------  operator>(day)  --------------------------//


 int day::operator>(day d)
    {
       long d1;
       long d2;

       d2=date+(month*30)+(year*360);
       d1=d.date+(d.month*30)+(d.year*360);
       return (d2>d1?1:0);
    }


 //-------------------------  next_month(int,int)  -----------------------//


 int day::next_month(int m,int y)
    {
       int d;

       if(m<1)
      m+=12;

       else if(m>12)
      m-=12;

       switch(m)
      {
         case 1 : d=31;
              break;

         case 2 : d=28+next_year(y);
              break;

         case 3 : d=31;
              break;

         case 4 : d=30;
              break;

         case 5 : d=31;
              break;

         case 6 : d=30;
              break;

         case 7 : d=31;
              break;

         case 8 : d=31;
              break;

         case 9 : d=30;
              break;

         case 10 : d=31;
              break;

         case 11 : d=30;
              break;

         case 12 : d=31;
              break;
      }

       return d;
    }


 //---------------------------  next_year(int)  --------------------------//


 int day::next_year(int y)
    {
       if(y%4==0 || y%4==0 || y%100==0)
      return 1;

       else
      return 0;
    }



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



 main()
    {
       clrscr();

       day date_1;
       day date_2;
       day date_3;

       cout<<\"\\n Enter the date in the pattern i.e  dd/mm/yy \"<<endl;

       cout<<\"\\n  First Date :\"<<endl;
       date_1.get_date();

       cout<<\"\\n  Second Date :\"<<endl;
       date_2.get_date();

       cout<<\"\\n********** First Date **********\"<<endl;
       date_1.show_date();

       cout<<\"\\n********** Second Date **********\"<<endl;
       date_2.show_date();

       cout<<\"\\n********** Result **********\\n\"<<endl;
       if(date_1>date_2)
      date_3.show_days(date_2,date_1);

       else
      date_3.show_days(date_1,date_2);

       getch();
       return 0;
    }

 
 
Didn't find what you were looking for? Find more on Program to find number of days b/w two given dates