C ++ Class Definition 1, Definition 1

Source: Internet
Author: User

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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.