Object as Array in C++

Here we are going to create multiple instances of the class using Object as Array Technique.

Source code :

#include<iostream>

#define _SIZE_ 2

using namespace std;

class student{

int mark1,mark2,total;
float avg;
char name[20];

public:
void get(){
    
	cout<<"\nEnter the name\t";
	cin>>name;
	cout<<"Enter the Marks\t";
	cin>>mark1>>mark2;
	
}

void calculate(){
    total=mark1+mark2;
	avg=total/2;
	
}

void display(){
    
	cout<<"\nYour Name is\t"<<name<<"\n";
	cout<<"Total\t"<<total<<"\nAverage\t"<<avg;
	
}

};

int main(){


student obj[_SIZE_];

for(int i=0;i<_SIZE_;i++)
obj[i].get();


for(int i=0;i<_SIZE_;i++){
    
	obj[i].calculate();
	obj[i].display();
	
}

return 0;
}

Output :

Enter the name  Arun
Enter the Marks 10
20

Enter the name  Karan
Enter the Marks 30
15
Your Name is    Arun
Total   30
Average 15

Your Name is  Karan
Total   45
Average 22

Output :


                

Comments :