Answer to Question #236025 in C++ for SOM

Question #236025

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.

1
Expert's answer
2021-09-11T14:25:06-0400
#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;


} 





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS