Arrays as Class Data Member

Submitted by: 

Arrays as Class Data Member

In this tutorial you will learn:
1. How to make Arrays as Class Data Member?
2. Why to use arrays as attributes of the class?
3. How to make array of objects?
4. C++ syntax

Arrays can also be the data members of the class, just like integer, float and other data type integers we can also define an array as the data members. Arrays are used as data members because if the user requires that the object contain a lot of information which is to be saved in the array then he will use array as the data member of the class. It must be noted here that whenever we define array as the data member of the class we must define its size dynamically and if we don’t do so then the we have to define the array dynamically using ‘new’ keyword.
Arrays as the data member of the class can be defined as follow
Class myclass
int a[5];
// here we write remaining part of the class
We can also make array of objects, but this is done in main of the program. It allows us to make a lot of objects and store them in an array. By making an array of objects we can set the values of all the objects using the smaller piece of code.
We define an array of objects and initialzed the value using ‘setDist’ function in the main as follow
void main()
Distance dist[100];
for (int i=0; i<100; i++) dist[i].setDist();

In the code below we have made stack using the Array data member. It also has the two functions which are necessary for a stack, Push and Pop. Push function pushes in the integer at the top of stack while the pop function remove the integer from the top of the stack.

strong>Array as Data Member
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These are the basic header files which are required for C++ programming. Using namespace std is used at the top of the program because if its not written then we have to write ‘std::’ with every line of the program. #include includes the C++ built in libraries and iostream is used for input and output of the program. Conio.h is used to show output on the screen, if it is not present , the screen flashes and disappears hence not showing output.

  1. const int MAX=10;
  2. class Stack {
  3. int st[MAX];
  4. int top;
  5. public:
  6. Stack() : top(-1) {}
  7. void push(int var) {st[++top]=var;}
  8. int pop() {return st[top--];}
  9. };

First of all we defined the size of constant integer ‘MAX’, it is the size of the Array which is the data member of the class. Then we made a class named stack. We defined two data members, one is the top and other is the array ‘st’. Next we have a constructor in which we have initialized the value of top equal to -1. Next we have a push function which takes an integer as argument and puts that argument at the top of the stack. Then we have a pop function which removes the top element of the stack and returns the value.

  1. void main()
  2. {
  3. Stack s1;
  4. int a,b;
  5. cout<<"Please Enter the First number you want to push in Array Data Member:";
  6. cin>>a;
  7. s1.push(a);
  8. cout<<"\nPlease Enter the Second number you want to push in Array Data Member:";
  9. cin>>b;
  10. s1.push(b);
  11. cout<<"---------------------Popping out integers from Array------------------------\n";
  12. cout<<"\nAfter popping out first integer from Array:"<<s1.pop()<<endl;
  13. cout<<"\nAfter popping out second integer from Array:"<<s1.pop()<<endl;
  14. }

In the above code we have made an object “s1”of the class stack, then we take 2 inputs from the user and push the input value(integer) at the top of the stack one after the other. Then in the next step the elements that we pushed into the stack are popped out and displayed on the screen.
strong>Constant Data Members
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
Execute > compile
then Execute > run

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.