c++课件Objects and Classes.pptVIP

  • 8
  • 0
  • 约1.48万字
  • 约 40页
  • 2017-01-01 发布于河南
  • 举报
c课件ObjectsandClasses

08 Objects and Classes datatype variable_name = initialization_value; int year = 2010; float score_of_cpp[124]={0}; char *ptr = {“79 golden medal”}; struct point { int x; int y; } centre_point; centre_point.x=0; centre_point.y=0; Consider a computer adventure game Among the objects in the game might be: You, the player Your enemies, e.g. dragons Your weapons Obstacles such as locked doors, rivers, etc. The prize e.g. energy or a fair maiden Consider a computer adventure game Among the objects in the game might be: You, the player A player will have data values to represent certain attributes, e.g. the state of their health or the weapons they possess. A player must be able to perform functions such as walk, run, attack an enemy, and rescue the fair maiden to win. Your enemies, e.g. dragons Your weapons Obstacles such as locked doors, rivers, etc. The prize e.g. energy or a fair maiden pseudo code representations of the classes class player { // functions: walk( ) run( ) jump( ) attack( ) rescue( ) ... //data: state_of_health type_of_weapon ... } ; class player Team[4] ; pseudo code representations of the classes Constructing a class in C++ class bank_account { public: void open( int acc_no, double initial_balance ) ; void deposit( double amount ) ; void withdraw( double amount ) ; void display_balance() ; private: int account_number ; double balance ; } ; private and public members The keywords private and public specify the access control level for the data and function members of the class. Data members declared with private access control are accessible only to member functions of the class and unavailable to any functions that are not members of the class. This is called information hiding and prevents the data from being changed except from within the class. Members declared with public access are accessible in any part of a program. access private and public members The class member function definition class bank_account { public:

文档评论(0)

1亿VIP精品文档

相关文档