zoukankan      html  css  js  c++  java
  • 一到关于c++继承和多态的题

     1class base
     2{
     3public:
     4        base()
     5        {
     6                cout <<" base::val "<<endl;
     7                val = 12345;
     8                print();
     9        }

    10        base(int x)
    11        {
    12                val = x;
    13                cout << " "<<endl;
    14        }

    15        virtual void print()
    16        {
    17                cout << " base:print(), val = "<<val<<endl;
    18        }

    19        base & operator = (int x)
    20        {
    21                val = x;
    22                cout <<" base::operator, val = "<<x<<endl;
    23                return *this;
    24        }

    25        void relase()
    26        {
    27                cout << " base::relase" <<endl;
    28        }

    29         ~base()
    30        {
    31                cout << " ~base::val = "<< val <<endl;
    32        }

    33protected:
    34        int  val;
    35}
    ;
    36
    37class deiver:public base
    38{
    39public:
    40        deiver()
    41        {
    42                cout << " deiver::val "<<endl;
    43        }

    44
    45        deiver(int x)
    46        {
    47                val = x;
    48                print();
    49        }

    50        void print()
    51        {
    52                cout << " deiver::print(), val =  "<< val <<endl;
    53        }

    54        void relase()
    55        {
    56                cout << " deiver::relase, val = "<<val<<endl;
    57        }

    58        deiver & operator = (int x)
    59        {
    60                val = x;
    61                cout <<" deiver::operator, val = "<<x<<endl;
    62                return *this;
    63        }

    64        ~deiver()
    65        {
    66                cout << " deiver::~deiver "<<endl;
    67        }

    68}
    ;
    69void des(base * b)
    70{
    71        b->print();
    72        b->relase();
    73}

    74deiver  de = 98765;
    75int main()
    76{
    77        deiver  * dee = new deiver;
    78        *(base *)dee = 100;
    79        des(dee);
    80        delete dee;
    81        return 0;
    82}

    83
    84
  • 相关阅读:
    技术普及帖:你刚才在淘宝上买了一件东西
    centos 安装yum
    eclipse 中java 项目红色感叹号终极解决方案
    centos x8664位版本 想安装qq for linux
    甲骨文放弃对RHEL的ASMLib支持
    amoeba连接mysqlERROR 2006 (HY000): MySQL server has gone away
    让Fedora 15/CentOS 6显示详细启动信息,不显示进度条
    Oracle10g RAC安装手册
    如何控制oracle RAC 进行并行运算
    利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
  • 原文地址:https://www.cnblogs.com/ziyuan/p/1316282.html
Copyright © 2011-2022 走看看