当前位置:首页 » 密码管理 » 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-02-05 04:43:07 浏览:579
python查文档 发布:2025-02-05 04:27:49 浏览:496
javaxmldom 发布:2025-02-05 04:27:40 浏览:9
linux修改内存大小 发布:2025-02-05 04:26:05 浏览:997
ftp命令复制文件 发布:2025-02-05 04:26:00 浏览:303
python好用的ide 发布:2025-02-05 04:14:18 浏览:516
id密码开头是多少 发布:2025-02-05 04:11:51 浏览:101
数据结构c语言ppt 发布:2025-02-05 04:11:45 浏览:43
如何用学习机配置的笔写字 发布:2025-02-05 04:09:15 浏览:395
5岁编程 发布:2025-02-05 04:06:21 浏览:653