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语言中gets()函数知识
导语:gets从标准输入设备读字符串函数,其可以无限读取,不会判断上限,以回车结束读取,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出。。下面是C语言中gets()函数知识,一起来学习下吧:gets()函数用于从缓冲区中读取字符串,其原型如下:char*gets(cha...
-
C与C++之间相互调用的实例方法
如果使用C语言编写的DLL,希望从中导出函数给C或C++的模块访问,则应使用__cplusplus预处理器宏确定正在编译的语言。如果是从C++语言模块使用,则用C链接声明这些函数。如果使用此技术并为DLL提供头文件,则这些函数可以原封不动地由C和C++模块使用。下面是小编为大家...
-
浅谈高职C语言课程的教学分析与设计
C语言作为学院网络技术专业的人门课程,旨在通过锻炼学生的逻辑思维,墙养学生在职业岗位中实际应用的能力.目前,该专业的学生毕业后主要从事网站开发和网络管理等方面的工作,而在这些领域几乎都以C语言作为应用的开发工具.但从历年教学实践来看,教师往往付出的精力多...
-
C语言的第一个程序
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的`编程语言。下面小编带大家看看第一个C语言程序。第一个C语言程序实例说明输出"Hello,world!"是...