类中的 public, protect, private

public, protect, private 是实现面向对象的封装性的关键。控制了类的接口访问权限,并控制了子类对父类的继承关系。

  1. public 修饰的变量和函数在类的内部和外部都可以访问;
  2. private 修饰的变量和函数只有在类的内部可以访问;
  3. protected 修饰的变量和函数在类的内部可以访问,还可以在派生类中访问
  • public 和 private 的区别。public 的变量和函数在类的内部和外部都可以访问,而 private 只在类的内部可以访问。例如:
#include <iostream>
using namespace std;

class Humen
{
    public:                                 // 公共成员 
        Humen(char* name, int age = 0);     // 构造函数 
        char* getName();                    // 获取姓名成员函数 
        char* name;                         // 姓名成员函数
    private:
        int getAge();                       // 获取年龄成员函数 
        int age;                            // 年龄成员函数 
};

Humen::Humen (char* n, int a)
{
    name = n;
    age  = a;
}

char* Humen::getName()
{
    return name;
}

int Humen::getAge()
{
    return age;
}

int main()
{
    int   age  = 0;
    char* name = NULL;
    Humen h("yang", 10);
    
    name = h.name;                          // 类的外部可以访问公有变量,正确 
    // age  = h.age;                        // 类的外部不能访问么有变量,错误 
    
    cout << h.getName() << endl;
    cout << h.getAge () << endl;
    
    return 0;
}      
  • protected 和 private 区别。如果类没有派生出其他类,那么 protected 和 private 是完全相同的,protected 和 private 一样只能在类的内部访问,不能在类的外部访问。但是如果在派生其他类的情况下,protected 和 private 的区别就体现出来了。private 修饰的变量只能在类的内部访问,在其派生类中也不能访问;而 protected 修饰的变量和函数可以在派生类中访问,例如:
#include <iostream>
using namespace std;

class Humen
{
    public:                                 // 公共成员
        Humen(char* name, int age = 0);
        char* getName();
        int   getAge ();
    protected:                              // 保护成员 
        char* name;
    private:                                // 私有成员 
        int   age;
};

class Women : public Humen                  // 公有继承 
{
    public:                                 // 公共成员
        Women(char* n, int a = 0) : Humen(n, a) {}
        char* getName();                  
        int   getAge ();
};

Humen::Humen (char* n, int a)
{
    name = n;
    age  = a;
}

int Women::getAge()
{
    return 15;
}

char* Women::getName()
{
    return name;
}

int main()
{
    int   age = 0;
    char* name;
    Humen h("yang", 20);
    
    // name = h.name;                          // 类的外部不能访问私有变量,错误 
    // age  = h.age;                           // 类的外部不能访问保护变量,错误
    
    Women w("yang", 20);
    cout << w.getName() << endl;
    cout << w.getAge () << endl;
    
    return 0;
}



  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Version 3.2.2 (08/09/2010) * New: o New feature in keygen - extract user field from license file o New UI of keygen, user now can define [user-bitset names] o UI: new menu option [Save project as] o Add special feature [Hide file from Antivirus] - available ONLY in special license and version (to get it you may contact with support) * Update/Fix: o Update of loader (bug names #missed librarys) Version 3.2.1 (28/05/2010) * New: o New trial options: [use limitations of days count], [use limitations of executions count] o New API: GetTrialExecCount, GetTrialDaysCount, GetKeyEndDate o Add some special compatibility with antiviruses o Improve console version * Update/Fix: o DLL: Small fixes for option [Stolen Resources] o DLL: Compatibility fixes for ActiveX Version 3.2.0 (11/05/2010) * New: o Finish localization o Public demo version o Small help updates * 3.2.0 beta 3 (20/04/2010) o New: + DLL support (special compatibility with Photoshop plugins) + License Manager is ready + New option [Use clock manipulation control] + Improve anti-debug, stolen resources + Remove option [Entry point virtualization] + Add new option [Allow to make changes in RTTI (delphi, vc, bcb)] + Protector and keygen now save configuration in registry o Update/Fix: + Fix small bugs (thx to users) * 3.2.0 beta 2 (22/03/2010) o New: + License Manager now has a beta status + Option [live protection] was included in option [debug and trace block] + We continue works on loader: 98% ready to support dll, 60% ready to support x64, 80% ready to support .NET o Update/Fix: + SDK was updated + Some improves to better support of C files and QT library * 3.2.0 beta 1 (04/02/2010) o New: + Licensing: LicenseID now has three mode depending: hardware and platform, hardware, independent + Licensing: protected application now control all system clock manipulations + Added debug functions: reset clock manipulation, reset application expired + New option: use application execution time limit + New option: only one executed process in memory + Added new messages in List of messages + Start building of License Manager + Keygen now can be protect manually + Keygen now can save key in file + Default keyname now license.bin (old .unlock) o Update/Fix: + Small GUI updates + Improve metamorph and vm + Fix small bugs (thx to users) + Remove api LoadLibraryX

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值