Init Object array : Class Basics « Class « C++






Init Object array

Init Object array
#include <iostream>
using namespace std;

class CharClass {
  char ch;
public:
  CharClass(char c) { 
     ch = c; 
  }
  char get_ch() { 
     return ch; 
  }
};

int main()
{
  CharClass ob[10] = { 
    CharClass('a'), 
    CharClass('b'), 
    CharClass('c'), 
    CharClass('d'), 
    CharClass('e'), 
    CharClass('f'), 
    CharClass('g'), 
    CharClass('h'), 
    CharClass('i'), 
    CharClass('j')
  };

  int i;

  for(i = 0; i <10; i++) 
    cout << ob[ i ].get_ch() << ' ';

  cout << 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.Assign object1 to object2Assign object1 to object2