Initialize an array of objects by referencing the constructor directly : object array « 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++ Tutorial » Class » object array 




#include <iostream> 
using namespace std; 
 
class MyClass 
  int x; 
public
  MyClass(int i) { x = i; 
  int getX() { return x; 
}
 
int main() 

  MyClass obs[4MyClass(-1), MyClass (-2)
                   MyClass (-3), MyClass (-4) };
  int i; 
 
  for(i=0; i < 4; i++
    cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n"
 
  return 0
}
obs[0].getX(): -1
obs[1].getX(): -2
obs[2].getX(): -3
obs[3].getX(): -4














9.33.object array
9.33.1.Create an array of objects
9.33.2.An array of objects: call its method
9.33.3.Initialize an array of objects without referencing the constructor directly
9.33.4.Initialize an array of objects by referencing the constructor directly
9.33.5.Object array of derived classes
9.33.6.Allocate an array of objects using new operator
9.33.7.An array of pointers to objects
9.33.8.An array on the heap
9.33.9.Delete an array of objects
9.33.10.allocates and frees an object and an array of objects of type loc.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.