inher_copy.cpp

来自「在进行C++培训时」· C++ 代码 · 共 72 行

CPP
72
字号
#include<iostream>
using namespace std;
class B{
     public:
	       B();
	       B(int i);
		   //B(B& x);
	       ~B();
	       virtual void Print() const;
     private:
	       int b;
};
B::B()
{	b=10;
	cout<<"B's default constructor called."<<endl;
}
B::B(int i)
{	b=i;
    cout<<"B's constructor called." <<endl;
}
//B::B(B& x)
//{
//	b=x.b;
//	cout<<"B' copy constructor called."<<endl;
//}

B::~B()
{	cout<<"B's destructor called."<<endl;  }
void B::Print() const
{	cout<<b<<endl;  }
class C:public B
{
    public:
      C();
	  C(int i,int j);
	  C(C& cc);
	  ~C();
	  virtual void Print() const;
	private:
	  int c;
};
C::C()
{	c=0;
	cout<<"C's default constructor called."<<endl;
}
C::C(int i,int j):B(i)
{	c=j;
	cout<<"C's constructor called."<<endl;
}
C::C(C& cc)//:B(cc)
{
	c=cc.c;
	cout<<"C's copy constructor called."<<endl;
}

C::~C()
{	cout<<"C's destructor called."<<endl; }
void C::Print() const
{	B::Print();	cout<<c<<endl;  }
void fn1(C x)
{
	x.Print();
}
void main()
{	

	C obj(5,6);
	

	fn1(obj);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?