• C++学习(3)


    今天大体看了一下友元类以及类的派生,敲了敲书上的样例,感觉懂了不少:

    1)友元类:如果一个类声明为另一个类的友元,则,该类称为另一个类的友元类。 若A类为B类的友元类,则A类的所有成员函数都是B类的友元函数,都可以访问B类的任何数据成员。

    友元类
     1 //友元类的使用
     2 //友元关系不能传递,B是A友元,C是B的友元,C和A之间,如果没有声明,就没有任何友元关系,不能进行数据共享
     3 //友元关系是单向的 
     4 //在声明A类为B类友元类时,A类必须已经存在 
     5 //该程序计算火车旅途时间 
     6 #include <iostream>
     7 using namespace std;
     8 
     9 class TrainTrip;    //向前引用声明 
    10 
    11 class Clock{
    12     private:
    13             int H,M,S;
    14     public:
    15            void ShowTime()
    16            {
    17                    cout<<H<<":"<<M<<":"<<S<<endl;
    18               } 
    19            void SetTime(int H=0, int M=0, int S=0)
    20            {
    21                    this->H=H,this->M=M,this->S=S; 
    22               }       
    23               Clock(int H=0,int M=0,int S=0)
    24               {
    25                         this->H=H,this->M=M,this->S=S;
    26             }
    27             friend class TrainTrip;      //TrainTrip是Clock的友元 
    28 };
    29 
    30 class TrainTrip{
    31       private:
    32                 char *TrainNo;
    33               Clock StartTime;
    34               Clock EndTime;
    35       public:
    36                TrainTrip(char *TrainNo, Clock S, Clock E)
    37             {
    38                   this->TrainNo=TrainNo;
    39                   StartTime=S;
    40                   EndTime=E;
    41             }      
    42             Clock TripTime()
    43             {
    44                    int tH,tM,tS;            //临时存储小时,分,秒 
    45                    int carry=0;            //借位
    46                   Clock tTime;             //临时存储时间
    47                   (tS=EndTime.S-StartTime.S)>0?carry=0:tS+=60,carry=1;
    48                      (tM=EndTime.M-StartTime.M-carry)>0?carry=0:tM+=60,carry=1;
    49                    (tH=EndTime.H-StartTime.H-carry)>0?carry=0:tH+=24;
    50                    tTime.SetTime(tH,tM,tS);
    51                    return tTime; 
    52               }
    53 } ;
    54 
    55 int main()
    56 {
    57      Clock C1(8,10,10),C2(6,1,2);
    58      Clock C3;
    59      TrainTrip T1("K16",C1,C2);
    60      C3=T1.TripTime();
    61      C3.ShowTime();
    62      system("pause");
    63      return 0;
    64 }

    2)类的继承:

    有三种,public继承,private继承和protected继承。

    一:公有继承

      注意点:基类的公有成员在派生类中仍然为公有成员;基类的私有成员在派生类中,无论派生类的成员还是派生类的对象都无法访问;保护成员在派生类中仍然是保护成员,可以通过派生类的成员函数访问,但不能由派生类对象直接访问。

    Point.h
     1 #include <iostream>
     2 using namespace std;
     3 class Point{
     4       private:
     5                 int X,Y;
     6       public:
     7                Point(int X=0,int Y=0)
     8                {
     9                          this->X=X,this->Y=Y;
    10                  }
    11                  void move(int OffX, int OffY)
    12                  {
    13                     X+=OffX,Y+=OffY;
    14                  }
    15                  void ShowXY()
    16                  {
    17                     cout<<"("<<X<<","<<Y<<")"<<endl;
    18                  }
    19 };
    Circle.h
     1 //公有继承
     2 #include "Point.h"
     3 const double PI=3.14159;
     4 
     5 class Circle :public Point
     6 {
     7        private:
     8                 double radius;
     9       public:
    10                Circle(double R, int X ,int Y):Point(X,Y)
    11                {
    12                              radius=R;
    13                 }
    14                 double area()
    15                 {
    16                       return PI*radius*radius;
    17                }
    18                void ShowCircle()
    19                {
    20                     cout<<"Center of circle:";
    21                     ShowXY();
    22                     cout<<"radius:"<<radius<<endl;
    23                }
    24 };
    公有继承 main.cpp
     1 #include "Circle.h"
     2 using namespace std;
     3 
     4 int main()
     5 {
     6      Circle Cir1(10,100,200);
     7      Cir1.ShowCircle();
     8      cout<<"area is:"<<Cir1.area()<<endl;
     9      Cir1.move(10,20);
    10     Cir1.ShowXY();
    11     system("pause");
    12     return 0; 
    13 }

    二: 私有继承

      注意点:基类的公有成员和保护成员被继承后作为派生类的私有成员;基类的私有成员在派生类中不能被直接访问;私有继承之后,基类的成员再也无法在以后的派生类中发挥作用,实际是相当于终止了基类的继续派生。

    Point.h
     1 #include <iostream>
     2 using namespace std;
     3 class Point{
     4       private:
     5                 int X,Y;
     6       public:
     7                Point(int X=0,int Y=0)
     8                {
     9                          this->X=X,this->Y=Y;
    10                  }
    11                  void move(int OffX, int OffY)
    12                  {
    13                     X+=OffX,Y+=OffY;
    14                  }
    15                  void ShowXY()
    16                  {
    17                     cout<<"("<<X<<","<<Y<<")"<<endl;
    18                  }
    19 };
    Circle2.h
     1 //私有继承
     2 #include "Point.h"
     3 const double PI=3.14159;
     4 
     5 class Circle : private Point
     6 {
     7        private:
     8                 double radius;
     9       public:
    10                Circle(double R,int X,int Y):Point(X,Y)
    11                {
    12                              radius=R;
    13                 }
    14                double area()
    15                {
    16                       return PI*radius*radius;
    17                }
    18                void ShowCircle()
    19                {
    20                     cout<<"Center of circle:";
    21                     ShowXY();
    22                     cout<<"radius:"<<radius<<endl;
    23                }
    24              void move(int OffX, int OffY)    //同名覆盖 
    25              {
    26                     Point::move(OffX,OffY);
    27                 } 
    28 }; 
    私有继承 main.cpp
     1 #include "Circle2.h"
     2 
     3 using namespace std;
     4 
     5 int main()
     6 {
     7      Circle Cir1(100,200,10);
     8      Cir1.ShowCircle();
     9      cout<<"area is:"<<Cir1.area()<<endl;
    10      Cir1.move(10,20);
    11      //Cir1.ShowXY()      //错误,ShowXY继承为私有成员函数
    12      system("pause");
    13      return 0; 
    14 }

    三:保护继承

      注意点:基类的公有成员和保护成员被继承后作为派生类的保护成员。

    private继承和protected继承异同:

        同:基类所有成员在派生类中的访问属性都是完全相同的。

        异:继承而来的派生类再次作为基类派生时,private派生而来的类的成员和对象都不能访问间接从原基类中继承来的成员;而protected派生而来的类可以,它可以沿着继承树继续向下传播。

    Point3.h
     1 //保护继承
     2 //Point.h
     3 
     4 #include <iostream> 
     5 using namespace std;
     6 
     7 class Point{
     8       protected:
     9                   int X,Y;
    10       public: 
    11                Point(int X=0,int Y=0)
    12                {
    13                          this->X=X;
    14                          this->Y=Y;
    15                  }
    16                  void move(int OffX,int OffY)
    17                  {
    18                     x+=OffX;
    19                     y+=OffY;
    20                  }
    21                  void ShowXY()
    22                  {
    23                     cout<<"("<<X<<","<<Y<<")"<<endl;
    24                  }
    25 };
    保护继承 main.cpp
     1 //保护继承
     2 // Cylinder 是圆柱体
     3 #include <"Point3.h"> 
     4 const double PI=3.14159;
     5 class Circle:protected Point
     6 { 
     7        protected:
     8                   double radius;
     9       public:
    10                Circle(double R,int X,int Y):Point(X,Y)
    11                {
    12                              radius=R;
    13                 }
    14                 double area()
    15                 {
    16                       return PI*radius*radius;
    17                }
    18                void ShowCircle()
    19                {
    20                     cout<<"Center of circle:";
    21                     ShowXY();
    22                     cout<<"radius:"<<radius<<endl;
    23                }
    24 };
    25 
    26 class Cylinder:protected Circle
    27 {
    28        private:
    29                 double height;
    30       public:
    31                Cylinder(intX,intY,double R,double H):Circle(R,X,Y)
    32                {
    33                    height=H;
    34                 }
    35                 double area()
    36                 {
    37                       return 2*Circle::area()+2*PI*radius*height;
    38                }
    39                double volume()
    40                {
    41                       return Circle::area()*height;
    42                }
    43                void ShowCylinder()
    44                 {
    45                     ShowCircle();
    46                     cout<<"height of cylinder:"<<height<<endl;
    47              } 
    48 };
    49 
    50 int main()
    51 {
    52      Cylinder CY(100,200,10,50);
    53      CY.ShowCylinder();
    54      cout<<"total area:"<<CY.area()<<endl;
    55      cout<<"volume:"<<CY.volume();
    56      system("pause");
    57      return 0;
    58 }
  • 相关阅读:
    Hibernate save, saveOrUpdate, persist, merge, update 区别
    Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目
    Neo4j批量插入(Batch Insertion)
    嵌入式(Embedded)Neo4j数据库访问方法
    Neo4j 查询已经创建的索引与约束
    Neo4j 两种索引Legacy Index与Schema Index区别
    spring data jpa hibernate jpa 三者之间的关系
    maven web project打包为war包,目录结构的变化
    创建一个maven web project
    Linux下部署solrCloud
  • 原文地址:https://www.cnblogs.com/shenshuyang/p/2640905.html
Copyright © 2020-2023  润新知