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