class Fraction {
public:
int whole;
int numerator;
int denominator;
};
struct Fraction {
int whole;
int numerator;
int denominator;
};
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;
}
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.
void Fraction::display()
{
if (this.whole) cout << this.whole << " ";
if (this.numerator) cout << this.numerator << "/"
<< this.denominator;
return;
}
#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
#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
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;
}