公有继承

#include <iostream>


using namespace std;
class point{
   float x;
   protected:
       float y;
   public:
    float z;
    point(float x,float y,float z){
    this -> x = x;this -> y =y;this -> z =z;
    }
    void setx(float x){this -> x = x;}
    void sety(float y){this -> y = y;}
    float getx(){return x;}
    float gety(){return y;}
    void showp(){
    cout << '(' << x <<','<< y <<','<< z << ')' ;//在公有继承的派生类中,直接访问基类的保护成员y和公有成员z;但是不能直接访问基类的私有成员x
        }
};
class sphere :public point{
  float radius;
  public:
      sphere(float x,float y,float z,float r):point(x,y,z){
      radius = r;
      }
      void shows(){
      cout << '(' << getx() <<',' << y << ',' << z << ")," << radius << endl;
      }
} ;//公有继承point类,派生类sphere类;


int main()
{
    sphere s(1,2,3,4);
    s.shows();
    cout << '(' << s.getx() <<',' <<s.gety() <<','<< s.z <<")\n";//派生类对象s只能访问类中的公有成员不访问类中的私有成员和保护成员
    return 0;
}

猜你喜欢

转载自blog.csdn.net/huzi99/article/details/80821236
今日推荐