C++:访问权限protected的问题
发布网友
发布时间:2022-04-23 03:18
我来回答
共4个回答
热心网友
时间:2023-08-17 05:42
#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;
}
热心网友
时间:2023-08-17 05:42
const static double Pi =3.14; 只有静态常量整型数据成员才可以在类中初始化
你是public继承的,protected是不能被公有继承的。还有你就是没事找事,方法也加了protected,你方法不公有干什么,保护变量就好了。protected允许其子类来访问的也不是你这样的,是这样的,给出代码:
#include <iostream>
#include <string>
using namespace std;
class Lyhvehicle
{
protected:
string name;
string colour;
public:
Lyhvehicle(){}
Lyhvehicle(string a,string b)
{
name=a;
colour=b;
}
virtual void show()
{
cout<<"车的种类 "<<name<<endl;
cout<<"车的颜色 "<<colour<<endl;
}
virtual string Getwheel()
{
return name;
}
virtual string Getweight()
{
return colour;
}
};
class Lyhcar:public Lyhvehicle
{
int num;
public:
Lyhcar(string a,string b,int c):Lyhvehicle(a,b) //是这样访问父类的私有或保护对象的
{
num=c;
}
void show()
{
cout<<"轿车的种类 "<<Getwheel()<<endl;
cout<<"轿车的颜色 "<<Getweight()<<endl;
cout<<"轿车的载人数 "<<num<<endl;
}
};
class Lyhtruck:private Lyhvehicle
{
double zweight;
public:
Lyhtruck(double b,string c,string d):Lyhvehicle(c,d)
{
zweight=b;
}
void show()
{
cout<<"卡车的种类 "<<Getwheel()<<endl;
cout<<"卡车的颜色 "<<Getweight()<<endl;
cout<<"卡车的载重量 "<<zweight<<endl;
}
};
void main()
{
Lyhvehicle a("车","蓝色");
Lyhcar b("汽车","红色",4);
Lyhtruck c(2.1,"卡车","紫色");
a.show();
b.show();
c.show();
}
热心网友
时间:2023-08-17 05:43
代码如下:
#include <iostream>
using namespace std;
class Shape{
public:
void fun(){print();}
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;
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.fun();
cout << "Let's create a Triangle .\nEnter the width and height : ";
double width ,height;
cin >> width >>height;
Triangle MyTriangle(width ,height);
MyTriangle.fun();
return 0;
}
热心网友
时间:2023-08-17 05:43
在shape的基类print函数前加一个public:
你的print是protected,子类中是无法调用的,在子类里是private的
另外
const static double Pi =3.14;我的编译器报错,把他调到类外面去,或者改成其他的形式