The C Program Structure The program structure in











- Slides: 11
The C++ Program Structure The program structure in C++ consists of three parts as follows 1. Class Definition: Each class (or a hierarchy of classes) is defined in a separate “header” file named class_name. h 2. Member Functions Definition: The body of member functions declared in each class are defined in a file named class_name. cpp. This file must contain an include directive to include the code in the header file. 3. Main Program Function: Each program must have a function called main(), needed to instantiate and manipulate objects. This file is called program_name. cpp, and it must also include the header file defining the class.
The C++ Program Structure File: graphics. h System Library Header Files Main File: Progam_name. cpp File #include <graphics. h> #include “Class. A. h” #include “Class. B. h” main() { File: Class. B. h File: Class. A. h Classes Header Files Classes Program Files File: Class. A. cpp #include “Class. A. h” File: Class. B. cpp #include “Class. B. h”
The C++ Program Structure graphics. h is a library header file containing the declarations of graphics functions and variables used in function main(). Class. A. h is a user defined header file containing the declarations for Class. A Class. B. h contains the declarations for Class. B Class. A. cpp contains the definitions of the member functions of class. A Class. B. cpp contains the definition of the member functions of Class. B Program_name. cpp contains the definition of of function main().
The C++ Program Structure Example: The PIXEL Program /* point. h--Lab. 1 assignment*/ //describe the file contents // point. h contains two classes: // class Location describes screen locations in X and Y coordinates // class Point describes whether a point is hidden or visible //global data types and data structures declaration enum Boolean {false, true}; //enum is a type specifier for discrete data types, //Boolean is a data type for variables with false and true values
The C++ Program Structure //File pont. h continues // the declaration of class Location { protected: // allows derived class to access private data int X; int Y; public: // these functions can be accessed from outside Location(int Init. X, int Init. Y); // The constructor function int Get. X(); // returns the value of X int Get. Y(); // returns the value of Y };
The C++ Program Structure //File pont. h continues // the declaration of class POINT class Point : public Location { // derived from class Location // public derivation means that X and Y are protected within Point protected: Boolean Visible; // classes derived from Point will need access public: Point(int Init. X, int Init. Y); // constructor void Show(); // make the object visible void Hide(); // make the object invisible Boolean Is. Visible(); // returns the value of the variable Visible void Move. To(int New. X, int New. Y); // Change the Location data };
The C++ Program Structure /* File POINT 2. CPP--Lab 1 assignment */ // POINT 2. CPP contains the definitions for the Point and Location // classes member functions that are declared in the file point. h // start with the include for header files #include <graphics. h> #include "point. h" // member functions for class Location: : Location(int Init. X, int Init. Y) { X = Init. X; Y = Init. Y; }; int Location: : Get. X(void) { return X; };
The C++ Program Structure // File point 2. cpp continues int Location: : Get. Y(void) { return Y; }; // member functions for the Point class: These assume // the main program has initialized the graphics system Point: : Point(int Init. X, int Init. Y) : Location(Init. X, Init. Y) { Visible = false; // make invisible by default }; void Point: : Show(void) { Visible = true; putpixel(X, Y, getcolor()); // uses default color };
The C++ Program Structure // File point 2. cpp continues void Point: : Hide(void) { Visible = false; putpixel(X, Y, getbkcolor()); // uses background color to erase }; Boolean Point: : Is. Visible(void) { return Visible; }; void Point: : Move. To(int New. X, int New. Y) { Hide(); // make current point invisible X = New. X; // change X and Y coordinates to new location Y = New. Y; Show(); // show point at new location };
The C++ Program Structure /* File: PIXEL. CPP--Lab 1 assignment*/ // PIXEL. CPP demonstrates the Point and Location classes // compile with POINT 2. CPP and link with GRAPHICS. LIB //start by including header files #include <graphics. h> // declarations for graphics library #include <conio. h> // for getch() function #include "point. h" // declarations for Point and Location classes int main() { // initialize the graphics system int graphdriver = DETECT, graphmode; initgraph(&graphdriver, &graphmode, ". . \bgi"); // the bgi directory is in under the // turbocpp directory.
The C++ Program Structure // File pixel. cpp continues //instantiate an object called Apoint, make it visible, and then make it move // itself across the screen Point APoint(100, 50); // Initial X, Y location is at 100, 50 APoint. Show(); // APoint turns itself on getch(); // Wait for keypress APoint. Move. To(300, 150); // APoint moves to 300, 150 getch(); // Wait for keypress APoint. Hide(); // APoint turns itself off getch(); // Wait for keypress closegraph(); // Restore original screen return 0; }