C++程序改错 哪位高手帮忙看看 标明原因

#include<iostream>
#include<string>
using namespace std ;

class CBase
{
protected:
char *ch ;
public:
CBase( char *x )
{
ch = new char[20] ;
strcpy( ch , x ) ;
}
virtual void fun() = 0 ;

virtual void fun1() //虚函数
{
cout << x << endl ;
}
{
cout << ch << endl ;
}
~CBase()
{
delete[]ch ;
cout << "~CBase()" << endl ;
}
};

class CDerived:public CBase
{
protected:
char *ch ;
public:
CDerived ( char *x , char*y ):CBase(y)
{
ch = new char[20] ;
strcpy( ch , x ) ;
}
void fun1()// {cout<<x<<endl;}
{
cout << ch << endl ;
}
virtual ~CDerived()
{
delete[]ch ;
cout << "~CDerived()" << endl ;
}
};
int main()
{
CBase obj1("Hello") , *p ;
p = &obj1 ;
p->fun1() ;
p->fun() ;
p=new CDerived("China","Hello") ;
p->fun1() ;
p->fun() ;
delete p ;
return 0 ;
}

第1个回答  2012-05-23
virtual void fun1() //虚函数
{
cout << x << endl ; // x 没有定义
}
{ // 你这个{是什么意思? 是不是想写个函数忘了写函数名了
cout << ch << endl ;
}

CBase obj1("Hello") , *p ; 纯虚基类不可以声明对象本回答被提问者采纳
第2个回答  2012-05-23
基类CBase中有公共数据成员char *ch,而派生类CDerived中又有一个保护数据成员 char *ch,那么在派生类中该如何区分这两个ch呢?追问

不知道 http://zhidao.baidu.com/question/427555918.html?quesup2&oldq=1

相似回答
大家正在搜