Assign object1 to object2 : Class Basics « Class « C++






Assign object1 to object2

Assign object1 to object2

#include <iostream>
using namespace std;

class BaseClass {
  int a;
public:
  void load_a(int n) { 
    a = n; 
  }
  int get_a() { 
    return a; 
  }
};

class DerivedClass : public BaseClass {
  int b;
public:
  void load_b(int n) { 
     b = n; 
  }
  int get_b() { 
     return b; 
  }
};

int main()
{
  DerivedClass object1, object2;

  object1.load_a(5);
  object1.load_b(10);

  
  object2 = object1;

  cout << "Here is object1's a and b: ";
  cout << object1.get_a() << ' ' << object1.get_b() << endl;

  cout << "Here is object2's a and b: ";
  cout << object2.get_a() << ' ' << object2.get_b() << endl;

  return 0;
}


           
       








Related examples in the same category

1.Class forward declarationClass forward declaration
2.Simplest class definitionSimplest class definition
3.A simple class with member variable, constructor, destructorA simple class with member variable, constructor, destructor
4.Address class: class definition and implementationAddress class: class definition and implementation
5.Declare class instanceDeclare class instance
6.Constructor: different parameter typeConstructor: different parameter type
7.Declare Class instance and use themDeclare Class instance and use them
8.Init Object arrayInit Object array