Classes and Objects
  1. The Class.

    The class is struct that binds functions (methods) as well as data items (properties) in a definition.

    While structs default to public. The class definition defaults to private. Public properties and methods will need to be declared.

    Classes can work the same as structs. We could substitute the following code
       class Fraction {
          public:
             int whole; 
    	 int numerator; 
             int denominator;
       };
    


    For our code in the fraction program.
       struct Fraction {
          int whole; 
          int numerator; 
          int denominator;
       };
    

  2. Adding Code.

    Member functions called methods may either be in the class definition as:

    class Fraction
    {
       public:
            void  display()        //   display a fraction
            {
                if (whole)     cout << whole << " ";
    	    if (numerator) cout << numerator << "/" 
                                    << denominator;
                return;
            }
       protected:
          int whole, numerator, denominator;
    }
    
    Or defined afterwards using the scope resolution operator :: like.
    class Fraction
    {
       public:
          void  display();        //   display a fraction
       protected:
          int whole, numerator, denominator;
    }
    
    
    
    void Fraction::display()
    {
       if (whole)     cout << whole << " ";
       if (numerator) cout << numerator << "/" 
                           << denominator;
       return;
    }
    
    
  3. Calling member function
       Fraction a;    // Instanciation of object a of class Fraction.
    
       a.display();   // display object a;
    
    Note is is how we reference public data items or fields of a structure. Unqualified member references in display resolve to a.

  4. The name of the object used to call the member function has a name.
    Which name?

    this name.
    So code can also be written using the this object reference. Like:
    void Fraction::display()
    {
       if (this.whole)     cout << this.whole << " ";
       if (this.numerator) cout << this.numerator << "/" 
                                << this.denominator;
       return;
    }
    


  5. The data members and member functions may be either public, protected or private
    1. public
      A class member accessible outside of the class.
    2. protected
      A class member accessible to other member of the same class and members of a subclass (inheritance). Protected members are not accessible publicly.
    3. private
      A class member only accessible to other members of the same class.

  6. The constructor

    Whenever an object is instanciated and special member function called the constructor is called. The constructor function has the same name as the class.
    #include <iostream>
    using namespace std;
    
    class Little {
       public:
          int item;
       Little()   // Constructor
       {
          item=1;
          cout << "called constructor" <<endl;
       }
    };
    
    int main()
    {
       int a;
       Little b,c,d;
       system("pause");
       return 0;
    }
    
    called constructor
    called constructor
    called constructor
    
  7. Multiple constructors.
    Constructors can take arguments to pass initialization values to the object. This is a form of function overloading.
    #include <iostream>
    using namespace std;
    
    class Little {
       public:
          int item;
          Little()   // Constructor
          {
             item=1;
             cout << "called constructor" <<endl;
          }
          Little(int x) // Constructor with value
          {
             item=x;
             cout << " Called constructor with value of " 
                  << x << endl;
          }
    };
     
    int main()
    {
       int a;
       Little b,c,d;
       Little e(3),f(4),g(5);
       system("pause");
       return 0;
    }
    called constructor
    called constructor
    called constructor
     Called constructor with value of 3
     Called constructor with value of 4
     Called constructor with value of 5
    
  8. Classy Fractionions.

    Enter e to end
    1 1/2
    
    Adding 1 1/2  +   =  1 1/2
    3 7/8
    
    Adding 3 7/8  +  1 1/2 =  5 3/8
    4
    
    Adding 4   +  5 3/8 =  9 3/8
    5/7
    
    Adding 5/7  +  9 3/8 =  10 5/56
    e
    Press any key to continue
    

    CFraction.h

    //
    // Class Fractionions Header
    //
    #ifndef CFRACT
    #define CFRACT
    
    int   gcd(int,int);   // Prototype for Euler's function
    
    class Fraction
    {
       public:
                   Fraction();        //  constructor           
          void     display();         //  display a fraction
          int      get();             //  get a fraction
          Fraction add(Fraction b);   //  add two fractionsa
    
    protected:
       int whole, numerator, denominator;
    };
    #endif
    

    CFractionM.cpp

    //
    //    Class Fractionions Modules
    //
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include "CFraction.h"
    
    using namespace std;
    
    Fraction::Fraction()  //  Constructor
    {
       denominator=1;
       numerator=whole=0;
    }
    
    void Fraction::display()
    {
       if (whole)     cout << whole << " ";
       if (numerator) cout << numerator << "/" 
                           << denominator;
       return;
    }
    
    int Fraction::get()
    {
       char *p,line[81];
       int a[3]={0,0,0};
       int count=0;
    
       cin.getline(line,80);
       if(line[0] == 'e') return 0;
       if(cin.eof())      return 0;
    
       p=strtok(line," /");
       do {
          a[count]=atoi(p);
          count++;
          p=strtok(NULL," /");
       } while(p);
       count--;
    
       numerator=whole=0;
       denominator=1;
    
       switch (count) {
          case 0: whole=a[0]; 
                  break;
          case 1: numerator=a[0]; 
                  denominator=a[1]; 
                  break;
          case 2: whole=a[0];
                  numerator=a[1];
                  denominator=a[2];
       }
       return 1;
    }
    
    Fraction Fraction::add(Fraction b)
    {
       Fraction answer;
       int common,worka, workb,numer, x;
    
       common = denominator * b.denominator;
    
       worka=whole * denominator + numerator;
       workb=b.whole * b.denominator + b.numerator;
    
       numer = worka * b.denominator + workb * denominator;
    
       answer.whole = numer / common;
       numer = numer % common;
       x=gcd(common,numer);
       answer.numerator=numer/x;
       answer.denominator=common/x;
    
       return answer;
    }
    
    
    /* Euler's Algorithm to find the greatest common divisor */
    int gcd(int a, int b)
    {
       if(b) return (gcd(b,a%b));
       else  return a;
    }
    

    CFraction.cpp

    //
    // Main Class Fraction
    //
    #include <iostream>
    #include "CFraction.h"
    
    using namespace stad;
    
    int main()
    {
       Fraction sum,item;
    
       cout << " Enter e to end" << endl;
       while(item.get()) {
          cout << endl << "Adding ";
          item.display();
          cout << "  +  ";
          sum.display();
          sum=sum.add(item);
          cout << " =  ";
          sum.display();
          cout << endl;
       }
       system("pause");
       return 0;
    }