Download this source file


// Slice1.cpp - Slicing copy example.

#include <iostream>

using namespace std;

class Base
{
public:
	Base (const int v1=32, const int v2=64)  { a1 = v1; a2 = v2;}
	Base& operator= (const Base& other)
		{	if (&other == this)  return *this;  // Check for self-assignment.
			a1 = other.a1;  a2 = other.a2;
			return *this;
		}
	void display () const
		{  cout << "Base class values: " << a1
				<< ", " << a2 << endl;
		}
private:
	int a1, a2;
};


class Derrived: public Base
{
public:
	Derrived (int v1=0, int v2=0, int v3=37, int v4=57) : Base(v1, v2)
		{ b1 = v3;  b2 = v4; }
	Derrived& operator= (const Derrived& other)
		{	if (&other == this)  return *this;  // Check for self-assignment.
			b1 = other.b1;  b2 = other.b2;
			return *this;
		}
	void display () const
		{	Base::display();
			cout << "Derrived class values: " << b1
				 << ", " << b2 << endl;
		}
private:
	int b1, b2;
};


int main ()
{
	Derrived obj1;
	Derrived obj2(1, 2, 3, 4);

	obj1 = obj2;
	obj1.display();
	return 0;
}