C++の3種類のアクセス権と3種類の継承方式
3つのアクセス権
C++のクラスには、public、protected、privateの3つのアクセス権(アクセス制御とも呼ばれる)があることがわかります.それらを理解するのも簡単ですが、次の例を見てみましょう.親:
サブクラス:
呼び出し元:
まとめ
C++クラスの3つの方式制御権限は,Javaにおける3つの対応するアクセス権限と同じであることをまとめた.
アクセス権
public
protected
private
本クラスに対して
表示
表示
表示
サブクラス
表示
表示
非表示
外部(呼び出し元)
表示
非表示
非表示
3つの継承方式
C++で継承する方法はいろいろありますが、それぞれpublic、protected、privateで表されています.これはJavaとは異なり、Javaは継承の概念しかなく、デフォルトはpublicが継承しています.1.3つの継承方法は、親に対する子のアクセスに影響しません.次の3つの継承方法では、親クラスのpublicおよびprotectedメンバーにアクセスできます.
. 2. 継承方式は、サブクラス(派生クラスとも呼ばれる)の呼び出し元(ユーザとも呼ばれる)の親クラス(ベースクラスとも呼ばれる)へのアクセス権を制御するためである.
public継承
結果:
名前:李四年齢:35
氏名:李四年齢:35職名:副教授
private継承:
. 3. public,protected,privateの3つの継承方式は,親クラスのpublicアクセス権限をサブクラスで対応する権限に変えたことに相当する.protectedが継承すると、親クラスのpublicメンバーが本クラスでprotectedのアクセス制御権限になります.privateは継承し、親クラスのpublicメンバーとprotectedメンバーを本クラスでprivateアクセス制御権に変更します.
protected継承:
C++のクラスには、public、protected、privateの3つのアクセス権(アクセス制御とも呼ばれる)があることがわかります.それらを理解するのも簡単ですが、次の例を見てみましょう.親:
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; //
};
呼び出し元:
void test()
{
Person person(" ", 22);
person.ShowInfo(); //public ,
cout << person.m_name << endl; //protected ,
cout << person.m_age << endl; //private ,
}
まとめ
C++クラスの3つの方式制御権限は,Javaにおける3つの対応するアクセス権限と同じであることをまとめた.
アクセス権
public
protected
private
本クラスに対して
表示
表示
表示
サブクラス
表示
表示
非表示
外部(呼び出し元)
表示
非表示
非表示
3つの継承方式
C++で継承する方法はいろいろありますが、それぞれpublic、protected、privateで表されています.これはJavaとは異なり、Javaは継承の概念しかなく、デフォルトはpublicが継承しています.1.3つの継承方法は、親に対する子のアクセスに影響しません.次の3つの継承方法では、親クラスの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; //
};
. 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; //
};
void TestPublic()
{
Teacher teacher(" ", 35, " ");
teacher.ShowInfo();
cout << endl;
teacher.ShowTeacherInfo();
}
結果:
名前:李四年齢: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; //
};
void TestPrivate()
{
Teacher teacher(" ", 35, " ");
teacher.ShowInfo(); // , Teacher private , 。
cout << endl;
teacher.ShowTeacherInfo();
}
. 3. public,protected,privateの3つの継承方式は,親クラスの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; //
};
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;
};