标题:各位大哥,帮我看个题!!!谢谢啦
只看楼主
ylmk96521
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-14
 问题点数:0 回复次数:1 
各位大哥,帮我看个题!!!谢谢啦
帮我看看这个程序的运行结果是什么?
#include <iostream.h>
#include <string.h>
class A{
    public:
     A(int i=0){a=i;}
     virtual void print()
        { cout<<a<<endl;}
     vitrual ~A()
        { cout<<"A free!"<<endl;}
    protected:
      int a;};
 class B: public A {
    public:
      B(char *,int);
      ~B( ){
        cout<<"B free!"<<endl; delete [ ]message;}
      virtual void print( );
     private:
       char *message;
       int len; };
   B::B(char *s,int i):A(i) {
     len=strlen(s)+1;
     message=new char[len];
     strcpy(message,s); }
   void B::print( ) {
      A::print( );
      cout<<message<<endl; }
 void main( )
   {   A *pa=new B("this",10);
       pa->print( );
       delete pa; }
搜索更多相关主题的帖子: class free message private include 
2010-08-14 12:47
QQ_ABCD
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2010-10-20
得分:0 
#include <iostream>
#include <string>
using namespace std;
class A
{
   public:
     A(int i=0){a=i;}
     virtual void print()
        { cout<<a<<endl;}
     virtual ~A()
        { cout<<"A free!"<<endl;}
    protected:
      int a;
};
class B: public A
{
   public:
      B(char *,int);
      ~B( ){
        cout<<"B free!"<<endl;
        delete [ ]message;}
      virtual void print( );
    private:
       char *message;
       int len;
};
   B::B(char *s,int i):A(i) {
     len=strlen(s)+1;
     message=new char[len];
     strcpy(message,s); }
   void B::print( ) {
      A::print( );
      cout<<message<<endl; }
void main( )
   {   A *pa=new B("this",10);
       pa->print( );
       delete pa; }
运行结果:10
          this
          B free!
          A free!
         Press any key to continue
2010-10-21 01:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-315671-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.484902 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved