Object assignment : instance object « Class « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Class » instance object 
9.32.6.Object assignment
#include <iostream> 
using namespace std; 
 
class MyClass 
  int a, b; 
public
  void setAB(int i, int j) { a = i, b = j; 
  void display() { 
    cout << "\n a is " << a << '\n'
    cout << "\n b is " << b << '\n'
  
}
 
int main() 

  MyClass ob1, ob2; 
 
  ob1.setAB(1020)
  ob2.setAB(00)
  cout << "ob1 before assignment:"
  ob1.display()
  cout << "ob2 before assignment:"
  ob2.display()
 
  ob2 = ob1; // assign ob1 to ob2 
 
  cout << "ob1 after assignment:"
  ob1.display()
  cout << "ob2 after assignment:"
  ob2.display()
 
  ob1.setAB(-1, -1)// change ob1 
 
  cout << "ob1 after changing ob1:"
  ob1.display()
  cout << "ob2 after changing ob1:"
  ob2.display()
 
  return 0
}
ob1 before assignment:
 a is 10

 b is 20
ob2 before assignment:
 a is 0

 b is 0
ob1 after assignment:
 a is 10

 b is 20
ob2 after assignment:
 a is 10

 b is 20
ob1 after changing ob1:
 a is -1

 b is -1
ob2 after changing ob1:
 a is 10

 b is 20
9.32.instance object
9.32.1.Using an empty initializer
9.32.2.Automatically converted into MyClass(4): MyClass ob = 4;
9.32.3.MyClass ob(4)
9.32.4.MyClass ob = MyClass(4);
9.32.5.Creates two objects.
9.32.6.Object assignment
9.32.7.Creating objects on the heap using new
9.32.8.Accessing members of objects on the heap
9.32.9.Call class constructor without new operator
9.32.10.Assign class object
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.