C ++ Class Definition 1, Definition 1
Recently, I was so busy that I had to watch a shop in the store, and my friends had a wedding banquet. I 've been so tired over the past few days.
After reading the book in the afternoon, I saw the class and practiced it myself.
Note: 1. the class is a data type/its child change is an object
Format:
Class Name
{
Public: // public... member private: // private... member
};
A code named Myclass:
1/* 2 a simple Class Example 3 */4 # pragma hdrstop 5 # include <tchar. h> 6 # include <iostream> 7 # pragma argsused 8 using namespace std; 9 10 class Myclass 11 {12 public: // The public keyword indicates the public attributes and methods, it can be accessed or called by the outside world. 13 int Add (int x, int y) // declare the function 14 {15 return x + y in the class; // Add the two numbers 16} 17 18 19 }; // note that the semicolon at the end of the class definition cannot be omitted. 20 21 int _ tmain (int argc, _ TCHAR * argv []) 22 {23 Myclass M; // create an object point M24 int I, n, sum = 0; 25 cin> I; // Enter 26 cin> n; 27 sum = M. add (I, n); // call the Add function under Class M to implement addition operation and assign the return value of the function to the sum variable 28 cout <"Operation Result: "<sum <endl; // output result 29 system (" pause "); 30 return 0; 31 32} 33 //---------------------------------------------------------------------------
Illustration: