欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C++继承介绍

时间:2022-02-19 09:39:28|栏目:C代码|点击:

然后是各个成员函数选项可以是virtual或non-virtual或pure virtual。本文仅仅作出一些关键点的验证。

  public继承,例如下:

复制代码 代码如下:

 class base
 {...}
 class derived:public base
 {...}  

如果这样写,编译器会理解成类型为derived的对象同时也是类型为base的对象,但类型为base的对象不是类型为derived的对象。这点很重要。那么函数形参为base类型适用于derived,形参为derived不适用于base。下面是验证代码,一个参数为base的函数,传入derived应该成功执行,相反,一个参数为derived的函数
复制代码 代码如下:

 #include <iostream>
  #include <stdio.h>

  class base
  {
      public:
      base()
      :baseName(""),baseData(0)
      {}

     base(std::string bn,int bd)
     :baseName(bn),baseData(bd)
     {}

     std::string getBaseName() const
     {
         return baseName;
     }

     int getBaseData()const
     {
         return baseData;
     }

     private:
         std::string baseName;
         int baseData;
 };

 class derived:public base
 {
     public:
         derived():base(),derivedName("")
         {}
         derived(std::string bn,int bd,std::string dn)
         :base(bn,bd),derivedName(dn)
         {}
         std::string getDerivedName() const
         {
             return derivedName;
         }
     private:
         std::string derivedName;
 };

 void show(std::string& info,const base& b)
 {
     info.append("Name is ");
     info.append(b.getBaseName());
     info.append(", baseData is ");
     char buffer[10];
     sprintf(buffer,"%d",b.getBaseData());
         info.append(buffer);
 }

 int main(int argc,char* argv[])
 {
     base b("test",10);
     std::string s;
     show(s,b);
     std::cout<<s<<std::endl;
     derived d("btest",5,"dtest");
     std::string ss;
     show(ss,d);
     std::cout<<ss<<std::endl;
     return 0;
 }

运行结果为:

base:baseName is test, baseData is 10
base:baseName is btest, baseData is 5

下面改改代码,将函数参数变为derived

复制代码 代码如下:

void show2(std::string& info,const derived& d)
{
    info.append("Name is ");
    info.append(d.getBaseName());
    info.append(", baseData is ");
    char buffer[10];
    sprintf(buffer,"%d",d.getBaseData());
    info.append(buffer);
}

调用show(ss,d);编译器报错
复制代码 代码如下:

 derived_class.cpp: In function `int main(int, char**)':
 derived_class.cpp:84: error: invalid initialization of reference of type 'const derived&' from expression of type 'base'
 derived_class.cpp:70: error: in passing argument 2 of `void show2(std::string&, const derived&)'第二点对各种形式的继承作出验证,首先给出表格

继承方式\成员类型 public protected private
public public protected 无法继承
protected protected protected 无法继承
private private private 无法继承

这里解释一下,这里仅仅表达基类的成员,被public,protected,private三种方式继承后,在原基类为public,protectedc,private的成员在继承类里类型为表格里内容

复制代码 代码如下:

class base
{
    public:
        std::string testPublic()
        {
            return std::string("this is public base");
        }
    protected:
        std::string testProtected()
       {
           return std::string("this is protected base");
       }
   private:
       std::string testPrivate()
       {
           return std::string("this is private base");
       }
};
class derivedPublic:public base
{
   public:
       std::string testPubPublic()
       {
           return testPublic()+= "in derived";
       }
       std::string testProPublic()
       {   
           return testProtected()+= "in derived";
       }
       std::string testPriPublic()                  
       {   
           return testPrivate()+= "in derived";
       }
};
int main(int argc,char* argv[])

   derivedPublic dpub;
   std::cout << dpub.testPublic() << std::endl;


报下面错误,说明testPrivate()不是derived私有函数而是base的私有函数
derived11.cpp:16: error: `std::string base::testPrivate()' is private
derived11.cpp:36: error: within this context这样验证private类型成员无法被继承(public,private,protected)注:private,protected略去不做证明
下面只要验证 testProtected 能被第三层继承类继承,但是无法被第三层类直接调用就说明是public继承后继承类型为protected,而基类为Public类型成员则即可被继承又可以直接调用。
复制代码 代码如下:

 #include <iostream>
 #include <string>

 class base
 {
     public:
         std::string testPublic()
         {
             return std::string("this is public base");
        }
    protected:
        std::string testProtected()
        {
            return std::string("this is protected base");
        }
    private:
        std::string testPrivate()
        {
            return std::string("this is private base");
        }
};

class derivedPublic:public base
{
    public:
        std::string testPubPublic()
        {
            return testPublic()+= "in derived";
        }

        std::string testProPublic()
        {   
            return testProtected()+= "in derived";
        }

//        std::string testPriPublic()                  
//        {   
//            return testPrivate()+= "in derived";
//        }
};

class deepDerived:public derivedPublic
{
    public:
        std::string deepProtected()
        {
            return testProtected() +="in deep";
        }

        std::string deepPublic()
        {
            return testPublic() +="indeep";
        }
};

int main(int argc,char* argv[])
{
    derivedPublic dpub;
    std::cout << dpub.testProtected() << std::endl;
    deepDerived deepdpub;
    std::cout<<deepdpub.testPublic() <<std::endl;
    std::cout<<deepdpub.testProtected() <<std::endl;
    std::cout<<deepdpub.deepProtected() <<std::endl;
    std::cout<<deepdpub.deepPublic() <<std::endl;
}


这里服务器报错

derived12.cpp:13: error: `std::string base::testProtected()' is protected
derived12.cpp:62: error: within this context这样就验证了一个是public,一个是protected,protected是不能直接调用的,但是被继承后是可以被public成员调用的。
下面的已经证明,详细步骤就略去如果对该部分验证感兴趣,可以看下面代码。

复制代码 代码如下:

 #include <iostream>
 2 #include <string>
 3 class base
 4 {
 5     public:
 6         std::string testPublic()
 7         {
 8             return std::string("this is public base");
 9         }
     protected:
         std::string testProtected()
         {
             return std::string("this is protected base");
         }
     private:
         std::string testPrivate()
         {
             return std::string("this is private base");
         }
 };

 class derivedPublic:public base
 {
     public:
         std::string testPubPublic()
         {
             return testPublic()+= "in derived";
         }

         std::string testProPublic()
         {   
             return testProtected()+= "in derived";
         }

 //        std::string testPriPublic()                   //私有成员并没有被继承下来
 //        {   
 //            return testPrivate()+= "in derived";
 //        }
 };

 class deepDerived:public derivedPublic
 {
     public:
         std::string test()
         {
             return testPublic() +="in 3";
         }
 };

 class derivedProtected:protected base
 {
     public:
         std::string testPubProtected()
         {
             return testPublic()+= "in derived";
         }

         std::string testProProtected()
         {   
             return testProtected()+= "in derived";
         }
 };

 class deepDerived2:public derivedProtected
 {
     public:
         std::string test()
         {
             return testPublic() +="in 3";
         }
 };

 class derivedPrivate:private base
 {
     public:
         std::string testPubPirvate()
         {
             return testPublic()+= "in derived";
         }

         std::string testProPrivate()
         {   
             return testProtected()+= "in derived";
         }

 };

 //class deepDerived3:public derivedPrivate
 //{
 //    public:
 //        std::string test()
 //        {
 //            return testPublic() +="in 3";
 //        }
 //};

 int main(int argc,char* argv[])
 {
     derivedPublic dpub;
    //derivedProtected dpro;
    //derivedPrivate dpri;
    std::cout<<dpub.testPublic()<<std::endl;       //
    //std::cout<<dpub.testProtected()<<std::endl;    //用户被继承也是无法使用
    //cout<<dpub.testPrivate()<<std::endl;         //基类都是私有函数
    std::cout<<dpub.testPubPublic()<<std::endl;
    std::cout<<dpub.testProPublic()<<std::endl;
    //std::cout<<dpub.testPriPrivate()<<std::endl; //没有被继承

    deepDerived dd;
    std::cout<<dd.test()<<std::endl;

    derivedProtected dpro;
    //std::cout<<dpro.testPublic()<<std::endl;        //变成protected类型
    std::cout<<dpro.testPubProtected()<<std::endl;
    std::cout<<dpro.testProProtected()<<std::endl;

    deepDerived2 dd2;
    std::cout<<dd2.test()<<std::endl;

    derivedPrivate dpri;
    std::cout<<dpri.testPubPirvate()<<std::endl;
    std::cout<<dpri.testProPrivate()<<std::endl;

//    deepDerived3 dd3;
//    std::cout<<dd3.test()<<std::endl;

上一篇:C语言中冒泡排序算法详解

栏    目:C代码

下一篇:C++中关于Crt的内存泄漏检测的分析介绍

本文标题:C++继承介绍

本文地址:http://www.codeinn.net/misctech/193881.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有