这个是根据别人的帖子改的
#include <iostream>
using namespace std;
class Sample
{
protected:
int x;
public:
Sample() { x=0; }
Sample(int val) { x=val; }
Sample(Sample& st)
{
x = st.x;
cout<< "Sample引用" <<endl;
}
void operator++(int) { x++; }
void operator=(Sample st1)
{
x = st1.x;
cout<< "sample=" <<endl;
}
virtual void disp() const
{
cout<<"x="<< x << endl;
}
friend ostream& operator<<(ostream& os, const Sample& kk)
{
os<< kk.x;
return os;
}
};
class Derived:public Sample
{
int y;
public:
Derived():Sample(){ y=0; }
Derived(int val1,int val2):Sample(val1){ y=val2; }
Derived(Derived& de):Sample(de)
{
y = de.y;
cout<< "deribed&" <<endl;
}
void operator--(int){ x--;y--;}
void operator=(Derived de1)
{
Sample::operator=(de1);
y = de1.y;
cout<< "deribed=" <<endl;
}
friend ostream& operator<<(ostream& os, const Derived& k)
{
os<< k.x << " " <<k.y;
return os;
}
void aaa(Derived de2)
{
cout<< "传值" <<endl;
}
void aaaa(Derived& de3)
{
cout<< "引用" <<endl;
}
virtual void disp() const
{
cout<<"x="<< x <<" y=" << y << endl;
}
};
int main ()
{
Derived d(3,5);
Derived a(d);
Derived c(6,10);
Derived e = c;
Sample aa(100);
d = c;
d.disp();
cout<< "****************************" <<endl;
d.aaa(c);
cout<< "****************************" <<endl;
d.aaaa(c);
cout<< "****************************" <<endl;
cout<< d <<endl;
cout<< "****************************" <<endl;
//Sample&p = aa;
//cout<< p <<endl;
Sample&p = c;
p.disp();
/*d++;
d.disp ();
d--;
d--;
d.disp(); */
return 0;
}