c++继承问题

来源:百度知道 编辑:UC知道 时间:2024/05/28 22:25:34
高手能告诉我怎样修才能运行吗?谢谢了
#include <iostream>
using namespace std ;
class B
{
public :
B();
B(int i);
~B();
void print () const ;
private :
int b ;
};
B::B()
{
b=0;
cout<<"1"<<endl;
}
B::B(int i)
{
b=i;
cout<<"2"<<endl;
}
B::~B()
{
cout<<"3"<<endl;
}
void B::print()const
{
cout<<b<<endl;
}
class C :public B
{
public:
C();//自己定义构造函数
C(int i,j);//带参数函数
~C();//析构函数
void print () const;//覆盖父类函数
private:
int c;

C::C()
{
c=0;
cout<<"4"<<endl;
}
C::C(int i,int j):B(i)
{
c=j;
cout<<"5"<<endl;
}
C::~C()
{

//可以运行的呀
#include <iostream.h>
class B
{
public :
B();
B(int i);
~B();
void print () const ;
private :
int b ;
};
B::B()
{
b=0;
cout<<"1"<<endl;
}
B::B(int i)
{
b=i;
cout<<"2"<<endl;
}
B::~B()
{cout<<"3"<<endl;
}
void B::print()const
{
cout<<b<<endl;
}
class C : public B //写错了不是::而是:
{
public: //在类中不用写::直接写{}里边的内容就行了
C()//自己定义构造函数
{
c=0; //写成大写了.改为小写c
cout<<"4"<<endl;
} C(int i,int j):B(i)//带参数函数 ====>变量j少了类型int
{
c=j;
cout<<"5"<<endl;
}
~C()//析构函数
{
cout<<"6"<<endl;
}
void print () const//覆盖父类函数
{
B::print ();
cout<