Write a program in C++ to create a class to take student subject marks and name (dynamically as per users input) for two students at the time of creation of objects. Display the details of two students along with the average mark by calling member function.Also deallocate the memory using destructor function.
#include<iostream>
using namespace std;
class Student{
private:
string name;
int subject[2];
public:
Student(){
}
Student(string na){
name = na;
}
~Student(){
cout<<"Destruction of the class\n";
}
void input(){
for(int i=0; i<2; i++){
cout<<"Subject "<<(i+1)<<endl;
cin>>subject[i];
}
}
int getTotal(){
int total = 0.0;
for(int i=0; i<2; i++){
total += subject[i];
}
return total;
}
void display(){
int total = 0.0;
cout<<"Name: "<<name<<endl;
for(int i=0; i<2; i++){
cout<<"Subject "<<(i+1)<<" ";
cout<<subject[i]<<endl;
}
cout<<"Total marks: "<<getTotal()<<endl;
}
};
int main(){
Student stud[2];
int total[2];
for(int i=0; i<2; i++){
cout<<"Student "<<(i+1)<<endl;
string name;
cout<<"Enter the student name\n";
cin>>name;
Student j(name);
j.input();
stud[i] = j;
total[i] = j.getTotal();
}
double totalSum = 0.0;
for(int i=0; i<2;i++){
totalSum += total[i];
}
for(int i=0; i<2; i++){
cout<<"Student "<<(i+1)<<endl;
stud[i].display();
}
cout<<"The average is: "<<totalSum/2;
}
Comments
Leave a comment