当前位置:首页 » 密码管理 » 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;
}

热点内容
maven编译乱码 发布:2025-03-16 09:13:13 浏览:5
微信棋牌游戏源码 发布:2025-03-16 09:06:17 浏览:112
华为平板访客如何访问存储卡 发布:2025-03-16 09:04:35 浏览:511
如何查看自己的qq号和密码 发布:2025-03-16 09:03:05 浏览:312
为什么安卓杀后台越来越严重 发布:2025-03-16 08:42:34 浏览:882
python解析json 发布:2025-03-16 08:36:22 浏览:569
奥丁镇服务器怎么进 发布:2025-03-16 08:34:04 浏览:6
在优酷看视频会缓存到c盘吗 发布:2025-03-16 08:29:05 浏览:258
口罩辊轴编程 发布:2025-03-16 08:21:52 浏览:581
网易我的世界官方开服务器 发布:2025-03-16 08:16:57 浏览:44