The parts of the class definition are:
The throttle class---a new data type that manipulates and stores the status of a throttle. A toy example of a class that might be used in a flight simulator. There are 4 throttle functions:
class Throttle
{
public:
// Mutator functions
void shut_off( );
void shift(int amount);
// Inspector functions
double flow( ) const;
bool is_on( ) const;
private:
int position, max_position;
};
header file: throttle.h implementation file: throttle.cxx
#ifndef THROTTLE_H
#define THROTTLE_H
class Throttle
{
...
}
#endif
As with any other data type, you declare variables of that type. These variables are called objects or instances.
Throttle plane, car;
Calling member functions:
car.shift(3);
/ | \ \
/ | \ actual parameter
object | \
name member member
access function
function
Void Throttle::shut_off( )
{
position = 0;
}
double Throttle::flow( ) const
{
return position/max_position;
}
Throttle car, plane;
car.shut_off( ); // sets car's position variable to 0
plane.shut_off ( ); // sets plane's position variable to 0
bool Throttle::is_on( ) const
{
return (flow( ) > 0);
}
class Throttle
{
public:
// Constructor
Throttle(int size);
// Modification member functions
....
Throttle::Throttle(int size )
{
max_position=size;
position = 0;
}
class Throttle
{
public:
// Constructors
Throttle ( ); // the default constructor
Throttle (int size); // one parameter constructor
Throttle (int size, int start); // two parameter constructor
.....
class Throttle
{
public:
// Constructor
Throttle (int size=5, int start=0);
int set_date(int year, int month=1, int day=1);
set_date(1997,12,5); or set_date(1997, 12); or set_date(1997);
class Foo { public: Foo(int num); void fun1(Foo f); void fun2(const Foo f); void fun3(Foo f) const; ... }Which of the member functions can alter the PRIVATE member variables of the Foo object that activates the function?