Write a program to create a class that stores name and subject marks of two students. Using dynamic constructor allocate name and subject marks. Display the details along with average mark.
#include<iostream>
using namespace std;
class Student {
private:
char* name;
int* subjectMarks;
public:
Student(){
this->name= new char[30];
this->subjectMarks= new int[5];
}
void get(){
cout<<"Enter name: ";
gets(this->name);
for(int i=0;i<5;i++){
cout<<"Enter subject mark "<<(i+1)<<": ";
cin>>this->subjectMarks[i];
}
}
void display(){
cout<<"Name: "<<this->name<<"\n";
cout<<"Subject marks: ";
float sum=0;
for(int i=0;i<5;i++){
cout<<this->subjectMarks[i]<<" ";
sum+=this->subjectMarks[i];
}
cout<<"\n";
float averageMark=sum/5.0;
cout<<"Average mark: "<< averageMark<<"\n";
}
~Student(){
delete[] subjectMarks;
delete[] name;
}
};
int main(){
Student* student1=new Student();
student1->get();
student1->display();
Student* student2=new Student();
cin.ignore();
student2->get();
student2->display();
delete student1;
delete student2;
int k;
cin>>k;
return 0;
}
Comments
Leave a comment