当前位置:首页 » 密码管理 » c继承访问权限

c继承访问权限

发布时间: 2023-06-14 18:23:40

⑴ 子类可以访问父类中定义的默认变量吗

int a;
没有任何修饰的属性是包访问权限。。

只要那个子类和他同包就能访问。。 如果不再同一个包中就不能访问。。。。

JAVA中有四种访问权限:private,protected,public,还有就是什么都不写。

private私有访问权限,只有在同一个类里才能访问。

public公共访问权限,所有的类都可以访问,无论在不在一个包中。

什么都不写,默认变量,是包访问权限,也就是说在同一个包类就可以访问。

protected是继承访问权限,在同一个包中的可以访问,对于不再同一个包中的类,如果该类是父类的子类,则可访问。不再同一个包中的不是继承的类就不可以访问。

默认变量和protected变量的共同点是在同一个包中都可以访问,但是如果不在同一个包中,但是有继承关系,protected变量就可以访问。但默认变量只要是不同的包,无论有没有继承关系都不能访问。

希望能帮到你~!

⑵ 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;
}

热点内容
服务器无硬件是什么意思 发布:2025-07-04 21:01:57 浏览:761
存储服务器的硬盘如何格式化 发布:2025-07-04 20:59:54 浏览:113
联想服务器的bmc叫什么 发布:2025-07-04 20:52:29 浏览:817
三星手机如何建文件夹 发布:2025-07-04 20:42:34 浏览:475
苹果11的摄像头是什么配置 发布:2025-07-04 20:32:20 浏览:59
plc编程入门视频 发布:2025-07-04 20:26:02 浏览:927
小米电视设置界面密码是多少 发布:2025-07-04 20:26:01 浏览:989
linux软件测试 发布:2025-07-04 20:12:40 浏览:274
小数加减法计算法则 发布:2025-07-04 20:11:49 浏览:691
文件如何定时上传至服务器 发布:2025-07-04 20:06:17 浏览:861