C++ 的三種訪問權(quán)限與三種繼承方式
三種訪問權(quán)限
我們知道C++中的類,有三種訪問權(quán)限(也稱作訪問控制),它們分別是public、protected、private。要理解它們其實也很容易,看下面了一個例子。
父類:
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年齡:" << m_age << endl;
}
protected:
string m_name; //姓名
private:
int m_age; //年齡
};
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年齡:" << m_age << endl;
}
protected:
string m_name; //姓名
private:
int m_age; //年齡
};
子類:
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見
cout << "年齡:" << m_age << endl; //錯誤,private屬性子類不可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見
cout << "年齡:" << m_age << endl; //錯誤,private屬性子類不可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
調(diào)用方法:
void test()
{
Person person("張三", 22);
person.ShowInfo(); //public屬性,對外部可見
cout << person.m_name << endl; //protected屬性,對外部不可見
cout << person.m_age << endl; //private屬性,對外部不可見
}
void test()
{
Person person("張三", 22);
person.ShowInfo(); //public屬性,對外部可見
cout << person.m_name << endl; //protected屬性,對外部不可見
cout << person.m_age << endl; //private屬性,對外部不可見
}
總結(jié)
我們對C++類三種方式控制權(quán)限總結(jié)如下,這與Java中的三種對應的訪問權(quán)限是一樣的。
qq%e6%88%aa%e5%9b%be20161104113813
三種繼承方式
C++中繼承的方式還有多種,也分別都用public、protected、private表示。這與Java不一樣,Java只有繼承的概念,默認是public繼承的。
1. 三種繼承方式不影響子類對父類的訪問權(quán)限,子類對父類只看父類的訪問控制權(quán)。
如下面三種繼承方式都能訪問父類中的public和protected成員。
class Teacher : /*public*/ /*protected*/ private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見
//cout << "年齡:" << m_age << endl; //錯誤,private屬性子類不可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
class Teacher : /*public*/ /*protected*/ private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "姓名:" << m_name << endl; //正確,protected屬性子類可見
//cout << "年齡:" << m_age << endl; //錯誤,private屬性子類不可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
2. 繼承方式是為了控制子類(也稱派生類)的調(diào)用方(也叫用戶)對父類(也稱基類)的訪問權(quán)限。
public繼承
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
void TestPublic()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo();
cout << endl;
teacher.ShowTeacherInfo();
}
void TestPublic()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo();
cout << endl;
teacher.ShowTeacherInfo();
}
結(jié)果:
姓名:李四
年齡:35
姓名:李四
年齡:35
職稱:副教授
private繼承:
class Teacher : private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
class Teacher : private Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
void TestPrivate()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //錯誤,因為Teacher采用了private的繼承方式,外部不可訪問。
cout << endl;
teacher.ShowTeacherInfo();
}
void TestPrivate()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //錯誤,因為Teacher采用了private的繼承方式,外部不可訪問。
cout << endl;
teacher.ShowTeacherInfo();
}
3. public、protected、private三種繼承方式,相當于把父類的public訪問權(quán)限在子類中變成了對應的權(quán)限。
如protected繼承,把父類中的public成員在本類中變成了protected的訪問控制權(quán)限;private繼承,把父類的public成員和protected成員在本類中變成了private訪問控制權(quán)。
protected繼承:
class Teacher : protected Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
class Teacher : protected Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowTeacherInfo()
{
ShowInfo(); //正確,public屬性子類可見
cout << "職稱:" << m_title << endl; //正確,本類中可見自己的所有成員
}
private:
string m_title; //職稱
};
void TestProtected()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //錯誤,基類Person的ShowInfo此時對Teacher相當于protected的,外部不可以被訪問
cout << endl;
teacher.ShowTeacherInfo();
}
void TestProtected()
{
Teacher teacher("李四", 35, "副教授");
teacher.ShowInfo(); //錯誤,基類Person的ShowInfo此時對Teacher相當于protected的,外部不可以被訪問
cout << endl;
teacher.ShowTeacherInfo();
}
class Leader : public Teacher
{
public:
Leader(const string& name, int age, const string& title, string position)
: Teacher(name, age, title), m_position(position)
{
}
void ShowLeaderInfo()
{
ShowInfo(); //基類Person的ShowInfo此時相當于protected的,但子類仍可以訪問
ShowTeacherInfo(); //ShowTeacherInfo仍然是public的,可以訪問
cout << m_position << endl;
}
private:
string m_position;
};
class Leader : public Teacher
{
public:
Leader(const string& name, int age, const string& title, string position)
: Teacher(name, age, title), m_position(position)
{
}
void ShowLeaderInfo()
{
ShowInfo(); //基類Person的ShowInfo此時相當于protected的,但子類仍可以訪問
ShowTeacherInfo(); //ShowTeacherInfo仍然是public的,可以訪問
cout << m_position << endl;
}
private:
string m_position;
};
以上所述是小編給大家介紹的C++ 的三種訪問權(quán)限與三種繼承方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
您可能感興趣的文章
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10c語言求1+2+...+n的解決方法
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解約瑟夫環(huán)的數(shù)學優(yōu)化方法
- 01-10深入二叉樹兩個結(jié)點的最低共同父結(jié)點的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計- 解析最少換車次數(shù)的問題詳解
- 01-10c語言 跳臺階問題的解決方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什


