1 / 11

The C++ Program Structure

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

haruko
Télécharger la présentation

The C++ Program Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. The C++ Program Structure File: graphics.h File: ClassB.h File: ClassA.h System Library Header Classes Header Files Files Classes Program Files File: Progam_name.cpp #include <graphics.h> #include “ClassA.h” #include “ClassB.h” main() { Main Prog File File: ClassA.cpp #include “ClassA.h” File: ClassB.cpp #include “ClassB.h”

  3. The C++ Program Structure graphics.h is a library header file containing the declarations of graphics functions and variables used in function main(). ClassA.h is a user defined header file containing the declarations for ClassA ClassB.h contains the declarations for ClassB ClassA.cpp contains the definitions of the member functions of classA ClassB.cpp contains the definition of the member functions of ClassB Program_name.cpp contains the definition of of function main().

  4. 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

  5. The C++ Program Structure //File pont.h continues // the declaration of class Location class Location { protected: // allows derived class to access private data int X; int Y; public: // these functions can be accessed from outside Location(int InitX, int InitY); // The constructor function int GetX(); // returns the value of X int GetY(); // returns the value of Y };

  6. 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 InitX, int InitY); // constructor void Show(); // make the object visible void Hide(); // make the object invisible Boolean IsVisible(); // returns the value of the variable Visible void MoveTo(int NewX, int NewY); // Change the Location data };

  7. The C++ Program Structure /* File POINT2.CPP--Lab 1 assignment */ // POINT2.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::Location(int InitX, int InitY) { X = InitX; Y = InitY; }; int Location::GetX(void) { return X; };

  8. The C++ Program Structure // File point2.cpp continues int Location::GetY(void) { return Y; }; // member functions for the Point class: These assume // the main program has initialized the graphics system Point::Point(int InitX, int InitY) : Location(InitX,InitY) { Visible = false; // make invisible by default }; void Point::Show(void) { Visible = true; putpixel(X, Y, getcolor()); // uses default color };

  9. The C++ Program Structure // File point2.cpp continues void Point::Hide(void) { Visible = false; putpixel(X, Y, getbkcolor()); // uses background color to erase }; Boolean Point::IsVisible(void) { return Visible; }; void Point::MoveTo(int NewX, int NewY) { Hide(); // make current point invisible X = NewX; // change X and Y coordinates to new location Y = NewY; Show(); // show point at new location };

  10. The C++ Program Structure /* File: PIXEL.CPP--Lab 1 assignment*/ // PIXEL.CPP demonstrates the Point and Location classes // compile with POINT2.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.

  11. 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.MoveTo(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; }

More Related