Define and init class array: two int type parameters in constructor : Array Object « Data Structure « C++






Define and init class array: two int type parameters in constructor

Define and init class array: two int type parameters in constructor
 
#include <iostream>
using namespace std;

class squares {
  int num, sqr;
public:
  squares(int a, int b) { 
     num = a; 
     sqr = b; 
  }
  void show() {
     cout << num << ' ' << sqr << endl; 
  }
};

int main()
{
  squares ob[10] = {
    squares(1, 1),
    squares(2, 4),
    squares(3, 9),
    squares(4, 16),
    squares(5, 25),
    squares(6, 36),
    squares(7, 49),
    squares(8, 64),
    squares(9, 81),
    squares(10, 100)
  };
  int i;

  for(i = 0; i <10; i++) 
     ob[ i ].show();

  return 0;
}


           
         
  








Related examples in the same category

1.Init class array: char typeInit class array: char type
2.Init class array: int constructor parameterInit class array: int constructor parameter
3.Arrays of ObjectsArrays of Objects
4.Initialize each object in an array by specifying an initialization list
5.A simple bounded 2-d array example.A simple bounded 2-d array example.
6.Two dimension object array: reference by indexTwo dimension object array: reference by index
7.Two dimension object array: reference by pointerTwo dimension object array: reference by pointer
8.Incrementing an object pointer