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 Fract {
    public:
    int whole, numerator, denominator;
    };
    


    For our code in the fraction program.
    struct Fract {
    int whole, numerator, denominator;
    };
    
  2. Adding Code.

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

    class Fract
    {
    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 Fract
    {
    public:
    void  display();        //   display a fraction
    protected:
    int whole, numerator, denominator;
    }
    
    
    
    void Fract::display()
    {
    if (whole) cout << whole << " ";
    if (numerator) cout << numerator << "/" << denominator;
    return;
    }
    
    
  3. Calling member function
    Fract a;    // Instanciation of object a of class Fract.
    
    a.display();   // display object a;
    
    Note is is how we reference public data items or fields of a structure. Unqualified member refernces in display resolve to a.

    So the code looks like:

    void Fract::display()
    {
    if (whole) cout << whole << " ";
    if (numerator) cout << numerator << "/" << denominator;
    return;
    }
    
    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 Fract::display()
    {
    if (this.whole) cout << this.whole << " ";
    if (this.numerator) cout << this.numerator << "/" << this.denominator;
    return;
    }
    


  4. 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.

  5. The constructor

    Whenever an object is instantiated and special member function called the constructor is called. The constructor function has the same name as the class.
    #include <iostream.h>
    class Little {
    public:
    int item;
    Little()   // Constructor
    {
     item=1;
     cout << "called constructor" <<endl;
    }
    };
    void main()
    {
    int a;
    Little b,c,d;
    }
    
    called constructor
    called constructor
    called constructor
    
  6. Multiple constructors.

    Constructors can take arguments to pass initialization values to the object. This is a form of function overloading.

    #include <iostream.h>
    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;
         }
    };
     
    void main()
    {
         int a;
         Little b,c,d;
    	 Little e(3),f(4),g(5);
    }
    called constructor
    called constructor
    called constructor
     Called constructor with value of 3
     Called constructor with value of 4
     Called constructor with value of 5
    
  7. Classy Fractions.

    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
    

    CFract.h

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

    CFractM.cpp

    //
    //    Class Fractions Modules
    //
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include "CFract.h"
    
    Fract::Fract()  //  Contructor
    {
    denominator=1;
    numerator=whole=0;
    }
    
    
    void Fract::display()
    {
    if (whole) cout << whole << " ";
    if (numerator) cout << numerator << "/" << denominator;
    return;
    }
    
    int Fract::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;
    }
    
    Fract Fract::add(Fract b)
    {
    Fract 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;
    }
    

    CFract.cpp

    //
    // Main Class Fraction
    //
    #include <iostream.h>
    #include "CFract.h"
    
    void main()
    {
       Fract 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;
     }
    }