|
|
If data members are private within a class how then can
they be updated or even inspected? The answer: via access
functions (operators) which are associated with the class.
These are called member functions.
let us expand our example:
class Fraction {
public:
void create (int, int);// here we have our access functions
private:void print();
int numerator;
};int denominator;
createand printare two functions that access the private
members. One possible set of operator declarations is:
void Fraction :: create (int num, int denom)
{
numerator = num;// this is an update access function
}denominator = denom;
void Fraction :: print ()
{
}cout << ((float) numerator) / ((float) denominator);
Fraction :: Fraction (int num, int denom)
{
numerator = num;// this is one form of the constructor
}denominator = denom;
Fraction f1;// default constructor used here
|
|