C++ | Making array of class

Note:- If you are looking for simple program of gross salary(not using class array), please see this.

C++ | Gross salary

else continue here……

Introduction

This tutorial is to make arrays of class. The program is given below to calculate gross salary using array of class. The program is extendable. Go enjoy the program. Lets begin……….

Program to find gross salary using class array

#include<iostream.h>
#include<conio.h>
//declare the class
class gsal
{
private:
int empno,sal,da,hra,gsal;
char name[40];
public:
void getdata(void)
{
cout<<“Enter the emp no:”;
cin>>empno;
cout<<“Enter the name:”;
cin>>name;
cout<<“Enter the salary:”;
cin>>sal;
}
void gross(void)
{
da=0.4*sal;
hra=0.2*sal;
gsal=da+hra+sal;
}
void print(void)
{
cout<<empno<<“\t”<<name<<“\t”<<gsal<<endl;
}
};
void main()
{
//clear the screen.
clrscr();
//declare x as array of gsal.
gsal x[3];
int i;
//input loop
for(i=0;i<3;i++)
{
x[i].getdata();
x[i].gross();
}
cout<<“empno \t name \t gsal”<<endl;
//output loop
for(i=0;i<3;i++)
x[i].print();
//get character
getch();
}

Output

Enter the empno:1
Enter the name:abc
Enter the salary:1000
Enter the empno:2
Enter the name:def
Enter the salary:1000
Enter the empno:2
Enter the name:ghi
Enter the salary:1000
empno     name     gsal
1               abc         1600
2               def          1600
3               ghi           1600

Extending it

The program can be extended by using the concept in any program. Like for ex. you can create a same program for mark sheet of students,etc. Go ahead extend it……

  • Remember this is a basic part of c++ which can be used in any program….

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Declare class.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare the x as your class name in arrays.
  6. Add input loop and call two functions.
  7. Add output loop and call print function.

At the end

You learnt creating the C++ program for Making array of class in c++. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s