c++类的定义《一》

最近好忙,一来要在店里看店,二来朋友办结婚酒,搞的我这几天好疲惫啊···博客又有好几天没提笔了。

下午简单看了下书,看到了类的部分,自己动手练习了一下

笔记:1.类是数据类型 / 它的变童就是对象 

格式:

class 类名   

{

        public:
            //公有的...成员

        private:
            //私有的...成员

};

一个名为Myclass类的代码:

 1 /*
 2   一个简单的类例子
 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:   //关键字public 表示公开的属性和方法, 外界可以直接访问或者调用。
13     int Add(int x,int y)  //在类内声明函数
14     {
15        return x + y; //两数相加
16     }
17 
18 
19 }  ;   //需注意类定义结束部分的分号不能省略。
20 
21 int _tmain(int argc, _TCHAR* argv[])
22 {
23     Myclass M;  //用定义好的类创建一个对象 点M
24     int i,n,sum = 0;
25     cin >> i ;   //输入
26     cin >> n ;
27     sum = M.Add(i,n) ;   //调用类M下的 Add函数 实现加法运算  把函数返回值赋给sum变量
28     cout << "运算结果:" << sum << endl ;   //输出结果
29     system("pause");
30     return 0;
31 
32 }
33 //---------------------------------------------------------------------------

图解:

原文地址:https://www.cnblogs.com/hkleak/p/4992060.html