24)类

1)其实类也是一种数据类型,只不过这种数据类型是  自己定义的  和那个结构体类似,而对象就是我们定义的变量,对象的类型是类

2)类有自己的  函数  有自己的变量    而且  函数和类有自己的权限修饰符

   叫成员变量  和  成员函数

3)那么 类和对象  和结构体之间的区别是啥:

    如果要是普通的结构体,里面只是含有变量,  但是要是想要函数操作这些变量,就可以在结构体外部创建函数,然后传入你就建立的结构体,在函数内部,操作结构体

    (结构体内部也可以有函数)

    如果要是类的话,我们可以直接在类的内部创建函数,然后函数直接操作类内的变量

 

 1 #include<iostream>
 2 using namespace std;
 3 class sutdent
 4 {
 5 public:
 6     char name[64];
 7     int sex;
 8 public:
 9     sutdent(int sex)
10     {
11         this->sex=sex;
12     }
13 public:
14     void Print_my()
15     {
16         cout<<sex<<endl;
17 
18     }
19 };
20  struct Student
21  {
22     char *name;
23     int sex;
24  };
25  void Print_MY(struct Student &st1)
26  {
27      st1.name="dhskf";
28      st1.sex=10;
29      cout<<st1.name<<endl<<st1.sex<<endl;
30  }
31  int main()
32  {
33      sutdent stu(100);
34      stu.Print_my();
35      cout<<endl;
36      struct Student s1;
37      struct Student &s2=s1;
38      Print_MY(s2);
39      cout<<endl;
40     
41  }
42 
43 
44 //你看,我完成同样的功能,可以用结构体,也可以用类,但是实现功能的函数,一个在类的里面,一个在结构体外面,通过传入结构体引用,然后调用函数,也是完成了类中函数一样的功能。

 

4)在

    public下面定义的函数和方法,可以在类的内部和外部都可以访问

    private下面定义的方法和变量,只能在类的内部访问,但是子类不可以访问

    protected下面定义的变量和方法,能在类种使用,子类能使用

 

    public和private实现了封装的含义,那么provated实现了继承的含义,就是,子类享用父类的变量和函数

 

posted @ 2018-01-03 10:38  小油菜1  阅读(163)  评论(0编辑  收藏  举报