C++ 的三種訪問權限與三種繼承方式
我們知道C++中的類,有三種訪問權限(也稱作訪問控制),它們分別是public、protected、private。要理解它們其實也很容易,以下是為大家分享的C++ 的三種訪問權限與三種繼承方式,供大家參考借鑑,歡迎瀏覽!
父類:
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; //職稱
};
調用方法:
void test()
{
Person person("張三", 22);
Info(); //public屬性,對外部可見
cout << person.m_name << endl; //protected屬性,對外部不可見
cout << person.m_age << endl; //private屬性,對外部不可見
}
void test()
{
Person person("張三", 22);
Info(); //public屬性,對外部可見
cout << person.m_name << endl; //protected屬性,對外部不可見
cout << person.m_age << endl; //private屬性,對外部不可見
}
總結
我們對C++類三種方式控制權限總結如下,這與Java中的三種對應的訪問權限是一樣的。
qq%e6%88%aa%e5%9b%be20161104113813
三種繼承方式
C++中繼承的方式還有多種,也分別都用public、protected、private表示。這與Java不一樣,Java只有繼承的概念,默認是public繼承的。
1. 三種繼承方式不影響子類對父類的訪問權限,子類對父類只看父類的訪問控制權。
如下面三種繼承方式都能訪問父類中的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. 繼承方式是為了控制子類(也稱派生類)的調用方(也叫用户)對父類(也稱基類)的訪問權限。
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, "副教授");
Info();
cout << endl;
TeacherInfo();
}
void TestPublic()
{
Teacher teacher("李四", 35, "副教授");
Info();
cout << endl;
TeacherInfo();
}
結果:
姓名:李四
年齡: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, "副教授");
Info(); //錯誤,因為Teacher採用了private的繼承方式,外部不可訪問。
cout << endl;
TeacherInfo();
}
void TestPrivate()
{
Teacher teacher("李四", 35, "副教授");
Info(); //錯誤,因為Teacher採用了private的繼承方式,外部不可訪問。
cout << endl;
TeacherInfo();
}
3. public、protected、private三種繼承方式,相當於把父類的public訪問權限在子類中變成了對應的權限。
如protected繼承,把父類中的public成員在本類中變成了protected的訪問控制權限;private繼承,把父類的public成員和protected成員在本類中變成了private訪問控制權。
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, "副教授");
Info(); //錯誤,基類Person的ShowInfo此時對Teacher相當於protected的,外部不可以被訪問
cout << endl;
TeacherInfo();
}
void TestProtected()
{
Teacher teacher("李四", 35, "副教授");
Info(); //錯誤,基類Person的ShowInfo此時對Teacher相當於protected的,外部不可以被訪問
cout << endl;
TeacherInfo();
}
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語言合併排序及實例代碼
歸併排序也稱合併排序,其算法思想是將待排序序列分為兩部分,依次對分得的兩個部分再次使用歸併排序,之後再對其進行合併。下面是小編分享的C語言合併排序及實例代碼,一起來看一下吧。合併排序僅從算法思想上了解歸併排序會覺得很抽象,接下來就以對序列A[0],A[l]…,A...
-
C語言學習攻略
導語:C語言作為最流行的程序設計語言,是任何一個計算機專業及其愛好者們都必須掌握的一門編程語言。下面就由小編為大家介紹一下C語言學習攻略,歡迎大家閲讀!一、C語言學習中存在的問題(一)基礎薄弱,無法適應C語言學習的課程通常安排在大一,這個時候,學生們剛剛進入...
-
C語言指針的長度和類型講解
對於初學者深入理解C語言程序設計有很好的參考價值,下面是小編為大家整理的C語言指針的長度和類型講解,歡迎參考~一般來説,如果考慮應用程序的兼容性和可移植性,指針的長度就是一個問題,在大部分現代平台上,數據指針的長度通常是一樣的,與指針類型無關,儘管C標準沒有規...
-
如何使用C語言求N的階乘
使用C語言求N的階乘的方法是很多小夥伴都想知道的,下面小編給大家介紹如何使用C語言求N的階乘,歡迎閲讀!如何使用C語言求N的階乘用遞歸法求N的.階乘程序調用自身稱為遞歸(recursion).它通常把一個大型複雜的問題層層轉化為一個與原問題相似的規模較小的問題來求...