c++protected(c++protected和private的区别)
本篇文章给大家谈谈c++protected,以及c++protected和private的区别对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
C++中,类的protected究竟有什么作用?
脊郑判 protected专樱改门就是为继承(子类)设计的 用public继承,基类所有的访问标识在子类不变, protected还是protected 。
C++编程思想中是这么说的,public意味着随后的定义对所有人都适用;private意味着除了该类型的创建者和该类型的内部成员函数之外,任何人都无法访问这些定义;而protected是继承丛卖的类可以访问protected的成员,但不能访问private的成员。
c++protected是什么意思
C++:protected访问说明符 。一个类自身的对象是可以访问protected的成员的 ,protected类型的成员在一个类中与pravite类型的成员性质亦一样的,只是在派生继承时性质不同,pravite不能被派生出来的类(也就是子类)调用(访问),而protected是可以被派生出来的类调用的。
protected专门就是为继承烂枝(子类)设计的 用public继承 那么基类所有的访问标识在子类不变 protected还是protected protected只有类册冲本州历歼身 和类的子类可以访问,对象是无法访问的。
有这么一条原则:在继承的时候,无论用什么方式(public.protected.private),子类随时都可以访问父类的非private变量或函数。
C++:访问权限protected的问题
#include iostream
using namespace std;
class Shape{
protected:
virtual double getArea() =0;
virtual double getCircumference()=0;
virtual void print(){
cout"Circumference = "getArea()"\n";
cout"Area = " getCircumference()"\n\n";
}
};
class Circle:public Shape {
public:
Circle(double radius = 0){
this-radius = radius;
}
double getArea(){
return Pi* radius* radius;
}
double getCircumference(){
return 2*Pi* radius;
}
private:
//const static double Pi =3.14;const static类型的变量,只有int能这么写,其他类型必须在类外初始化
double radius ;
};
const double Circle :: Pi = 3.14;
class Triangle:public Shape{
public:
Triangle(double width ,double height){
this-width = width;
this-height=height;
}
double getArea(){
return width * height;
}
double getCircumference(){
return 2*width + 2*height;
}
private:
double width , height;
};
int main(int argc, char *argv[])
{
cout "Let's create a Circle .\nEnter a radius: ";
double Radius = 0;
cin Radius;
Circle MyCircle(Radius);
MyCircle.print();
cout "Let's create a Triangle .\nEnter the width and height : ";
double width ,height;
cin width height;
Triangle MyTriangle(width ,height);
MyTriangle.print();
return 0;
}
在说说protect继承的问题,在主函数定义的Circle对象,cirle是从shape共有继承下来的,所有对于circle来说,print函数是保护类型的,当然他的对象是访问不了自己的保护成员的。
protected允许其子类来访问,这句话的意思是在类定义内,子类可以访问访问父类的protect
例如:
#include iostream
using namespace std;
class Shape{
protected:
virtual double getArea() =0;
virtual double getCircumference()=0;
virtual void print(){
cout"Circumference = "getArea()"\n";
cout"察正哗Area = " 败行清困getCircumference()"\n\n";
}
};
class Circle:public Shape {
public:
Circle(double radius = 0) : radius(radius)
{
}
double getArea(){
return Pi* radius* radius;
}
double getCircumference(){
return 2*Pi* radius;
}
void print()
{
Shape::print();
}
private:
const static double Pi ;
double radius ;
};
const double Circle :: Pi = 3.14;
class Triangle:public Shape{
public:
Triangle(double width ,double height){
this-width = width;
this-height=height;
}
double getArea(){
return width * height;
}
double getCircumference(){
return 2*width + 2*height;
}
void print()
{
Shape::print();
}
private:
double width , height;
};
int main(int argc, char *argv[])
{
cout "Let's create a Circle .\nEnter a radius: ";
double Radius = 0;
cin Radius;
Circle MyCircle(Radius);
MyCircle.print();
cout "Let's create a Triangle .\nEnter the width and height : ";
double width ,height;
cin width height;
Triangle MyTriangle(width ,height);
MyTriangle.print();
return 0;
}
[img]C# private和protected区别
private只有内部可以看正梁核到,并且只能内部调用。
而Protected虽然可以被外界看到,渣则但外界却不能调用。
按字面意思,前者是我私有的,在口袋里放着举掘,比如说一颗很大的钻石,照耀于世当然很危险;后者是受保护的,虽然你看见我手里拿着个手机,你却不敢抢去用。因为那时不合理法的,并且被看到了也没什么。
差不多就这个意思了。
选择用那个,你就看看这个东西是钻石还是手机。
关于c++protected和c++protected和private的区别的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。