Introduction: Diagram (and C++ Code) Explaining Classes, Objects, Variables and Methods Using Airplanes

About: Occupation: tech support
Diagram (and C++ code) that explains how classes, objects, methods and variables work in object oriented programming using airplanes as a real world analogy. The class is called "Airplane" and the objects are different types of airplanes: Cessna Skyhawk, Gulfstream G150 and a Boeing 727. Variables: TopSpeed, Range Methods: TakeOff, Climb, Land
// Airplane class/object
// Matt R
// Used Code::Blocks IDE with GCC compiler

#include
#include

using namespace std;

class Airplane {
public:
int TopSpeed;
int Range;
string EngineType;
// Methods
void Takeoff() {
cout << "The Cessna Skyhawk is taking off from runway." << endl;
} // end method TakeOff
void Climb() {
cout << "The Cessna Skyhawk is climbing to 1,500 feet." << endl;
} // end Climb method
void Land() {
cout << "The Cessna Skyhawk is landing onto the runway." << endl;
} // end Land method
}; // end class Airplane

int main() {
// Create object of class Airplane
Airplane CessnaSkyhawk;
// Set variables
CessnaSkyhawk.TopSpeed = 142;
CessnaSkyhawk.Range = 640;
// Print out variables
cout << "The Cessna Skyhawk top speed is: " << CessnaSkyhawk.TopSpeed << endl;
cout << "The Cessna Skyhawk range is: " << CessnaSkyhawk.Range << endl;
// Call methods from class Airplane
CessnaSkyhawk.Takeoff();
CessnaSkyhawk.Climb();
CessnaSkyhawk.Land();

return 0;
} // end main function

Supplies

  1. C++ compiler like gcc.
  2. IDE like CodeBlocks
  3. Computer

Step 1: Diagram Explaining Classes, Objects, Variables and Methods Using Airplanes

Diagram (and C++ code) that explains how classes, objects, methods and variables work in object oriented programming using airplanes as a real world analogy.The class is called "Airplane" and the objects are different types of airplanes: Cessna Skyhawk, Gulfstream G150 and a Boeing 727.Variables: TopSpeed, RangeMethods: TakeOff, Climb, Land
// Airplane class/object
// Matt R
// Used Code::Blocks IDE with GCC compiler

#include
#include

using namespace std;

class Airplane {
public:
int TopSpeed;
int Range;
string EngineType;
// Methods
void Takeoff() {
cout << "The Cessna Skyhawk is taking off from runway." << endl;
} // end method TakeOff
void Climb() {
cout << "The Cessna Skyhawk is climbing to 1,500 feet." << endl;
} // end Climb method
void Land() {
cout << "The Cessna Skyhawk is landing onto the runway." << endl;
} // end Land method
}; // end class Airplane

int main() {
// Create object of class Airplane
Airplane CessnaSkyhawk;
// Set variables
CessnaSkyhawk.TopSpeed = 142;
CessnaSkyhawk.Range = 640;
// Print out variables
cout << "The Cessna Skyhawk top speed is: " << CessnaSkyhawk.TopSpeed << endl;
cout << "The Cessna Skyhawk range is: " << CessnaSkyhawk.Range << endl;
// Call methods from class Airplane
CessnaSkyhawk.Takeoff();
CessnaSkyhawk.Climb();
CessnaSkyhawk.Land();

return 0;
} // end main function

Step 2: Diagram in ASCII Format

```
+----------------------------------------------------------+
| Airplane |
| (Parent Class) |
+-----------------------------------+----------------------+
\|/
+-----------------------------------+----------------------+
| Attributes |
| |
| - TopSpeed: integer |
| - Range: integer |
+----------------------------------------------------------+
|
\|/
+----------------------------------+---------------------+
| Methods |
| |
| - TakeOff(): void |
| - Climb(): void |
| - Land(): void |
+--------------------------------------------------------+
|
Objects of Class |
\|/
+-----------------+---+ +-----------+-------+ +-------------------+
| Cessna Skyhawk | |Gulfstream G150 | | Boeing 727 |
| (Object) | | (Object) | | (Object) |
| - TopSpeed, Range | | - TopSpeed, Range| | - TopSpeed, Range |
| - TakeOff(), | | - TakeOff(), | | - TakeOff(), |
| Climb(), Land() | | Climb(), Land() | | Climb(), Land() |
+---------------------| +-------------------+ +--------------------+