Answer to Question #232866 in C++ for Arup

Question #232866

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. 


1
Expert's answer
2021-09-03T15:09:09-0400
#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;


}

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