目录
c和c++中结构体的区别
类的封装
c语言中的行为和属性封装存在的问题
c++中对事物的封装——类的封装
尽量将成员变量设置为private
代码示例
c和c++中结构体的区别
c语言中struct只有变量,不能存放函数,也就是数据(属性)和行为(方法)是分离的
c++中结构体是可以存放函数的,也就是数据(属性)和行为是封装在一起的
struct _stu() //c
{
int a;
int b[5];
};
struct _stu()//c++
{
int a;
int b[5];
void peintf_stu()
{
cout << a << endl;
}
};
类的封装
c语言中的行为和属性封装存在的问题
 
 现实世界的事物所具有的共性就是每个事物都具有自身的属性,一些自身具有的行为,所以如果我 
 
 
 们能把事物的属性和行为表示出来,那么就可以抽象出来这个事物。比如我们要表示人这个对象,在 c 语言中,我们可以这么表示 
 : 
 
#include <iostream>
#include <string.h>
using namespace std;
struct Person{
    char name[64];
    int age;
};
struct Animal{
    char name[64];
    int age;
};
void PersonEat(struct Person* person){
    printf("%s 在吃人吃的饭!\n",person->name);
}
void AnimalEat(struct Animal* animal){
    printf("%s 在吃动物吃的饭!\n", animal->name);
}
int main(){
    struct Person Person;
    struct Animal animal;
    strcpy(Person.name, "小明");
    PersonEat(&Person);
    strcpy(animal.name, "dog");
    AnimalEat(&animal);
    return 0;
}
 
  在  
  c  
  语言中,行为和属性是分开的,那么万一调用错误,将会导致问题发生。 
 
 
  
  从这个案例我们应该可以体会到,属性和行为应该放在一起,一起表示一个具有属性和行为的对象。 
 
 
 c++中对事物的封装——类的封装
c++将行为和属性封装在一起
类和结构体的区别在于 类对成员可以进行访问的权限控制,结构体不可以
| 访问属性 | 属性 | 对象内部 | 对象外部 | 
| public | 公有 | 可访问 | 可访问 | 
| protected | 保护 | 可访问 | 不可访问 | 
| private | 私有 | 可访问 | 不可访问 | 
类中的函数 可以直接访问类中的成员
class Person{
    //人具有的行为(函数)
    public://公有的
        int mTall; //多高,可以让外人知道
    protected://保护的
        int mMoney; // 有多少钱,只能儿子孙子知道
    private://私有的
        int mAge; //年龄,不想让外人知道
    void show()
    {
        cout << mTall <<"";
        cout << mMoney <<"";//内部可访问
        cout << mAge <<"";//内部可访问
    }
};
int main(){
    Person p;
    p.mTall = 220;
    //p.mMoney 保护成员外部无法访问
    //p.mAge 私有成员外部无法访问
    p.Dese();
    return EXIT_SUCCESS;
}
保护的和私有的区别是 保护的子类可以访问 私有的子类不可访问
尽量将成员变量设置为private
 
  成员变量设置为私有的,优点 
 
 
  
          对变量的设置时的控制 
 
 
  
          可以给变量设置制度权限 
 
 
  
          可以给变量设置只写权限 
 
 
  
          可以给变量设置可读可写权限 
 
 
 class AccessLevels{public ://对只读属性进行只读访问int getReadOnly(){ return readOnly; }//对读写属性进行读写访问void setReadWrite( int val){ readWrite = val; }int getReadWrite(){ return readWrite; }//对只写属性进行只写访问void setWriteOnly( int val){ writeOnly = val; }private :int readOnly; // 对外只读访问int noAccess; // 外部不可访问int readWrite; // 读写访问int writeOnly; // 只写访问};
代码示例
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
    public:
        void init(int init_age,char *init_name)
        {
            if(age >= 0 && age <= 100)
            {
                age = init_age;
            }
            if(init_name != NULL)
            {
                strcpy(name,init_name);
            }
        }
        void get_age(int get_age)
        {
            if(age >= 0 && age <= 100)
            {
                age = get_age;
            }
        }
        void get_name(char *get_name)
        {
            if(get_name != NULL)
            {
                strcpy(name,get_name);
            }
        }
        void set()
        {
            cout << age <<" "<<endl;
            cout << name <<" "<<endl;
        }
    private:
        int age;
        char name[8];
}person;
void test()
{
    person.init(50,"name");
    person.set();
    person.get_age(25);
    person.get_name("bob");
    person.set();
}
int main()
{
    test();
    return 0;
}
编译运行




















