[Original] C ++ custom class (1)

Source: Internet
Author: User
1 // TestClass. h /////////////////////////////////////
2 class TestClass
3 {
4 public:
5 void Test ();
6 int;
7 private:
8 int B;
9 public:
10 static int c;
11}; // do not forget to include this Semicolon. Otherwise, the following cpp will prompt the error message indicating that the semicolon is missing in namespace.
12
13 // C ++ BaseTest. cpp /////////////////////////////////////
14 # include "stdafx. h"
15 # include "TestClass. h"
16
17 # include <iostream>
18 using namespace std;
19
20 TestClass tc;
21 // tc. a = 10;
22 // tc. B = 30; // you will find that an error is reported when you initialize the TestClass member variable as a global variable.
23 int TestClass: c = 8; // static variable of the class. The definition does not need to be static before it. Otherwise, an error is returned. It is the shared value of all class objects.
24
25 void TestClass: Test ()
26 {
27 B = 20;
28 cout <a <"" <B <"" <c <endl;
29}
30
31 int _ tmain (int argc, _ TCHAR * argv [])
32 {
33 tc. a = 10; // The member variables of the class object are correctly initialized locally. The member variables of the object belong to the object.
34 // For example, here you have assigned a value to Member a of the object tc. In the Test function, the value of a is the member a of the object tc.
35 cout <tc. a <endl;
36 tc. Test ();
37 while (true)
38 {
39}
40 return 0;
41}

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.