Answer to Question #220124 in C++ for Srikanth Kodamasim

Question #220124
Create a class student with attributes name, register number, department, an array for storing 5 subject marks, total. In main(), find the student who has scored the highest total using the '>' operator (friend) to compare the objects and display the details of the student. Include necessary member functions and constructors.
1
Expert's answer
2021-07-24T07:07:21-0400
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>//Input output stream
#include <stdio.h>//standart inp-out(C)
#include <stdlib.h>//Standart Library
#include <string.h>//Library for string function
//define and implement class Student
using namespace std;
class Student
{
	//Private section
private:
	char name[30];//Name
	int regNum;//Register number
	int mark[5];//total mark 5 subject
public:
	//
	Student()//default constructor
	{
		//defaulting
		strcpy(this->name, "");//empty name
		this->regNum = 0;
		for (int i = 0; i < 5; i++)
			this->mark[i] = 0;//0 mark
	}
	//Parametrazed constructor  with name and register number
	Student(const char* _name, const int reg)
	{
		strcpy(this->name, _name);
		this->regNum = reg;
		for (int i = 0; i < 5; i++)
			this->mark[i] = 0;
	}
	//getter and setter
	void setName(const char* s)
	{
		strcpy(this->name, s);
	}
	void setRegNum(const int a)
	{
		this->regNum = a;//set register number
	}
	void setMark(int a[5])
	{
		for (int i = 0; i < 5; i++)
			this->mark[i] = a[i];
	}
	const char* getName()const
	{
		return this->name;
	}
	int getReg()const
	{
		return this->regNum;
	}
	//method which return sum all mark
	int sumMark()
	{
		int sum = 0;
		for (int i = 0; i < 5; i++)
			sum += this->mark[i];
		return sum;
	}
	//Input console
	void InputDate()
	{
		cout << "Please enter date of student\n";
		cout << "\tName:";
		cin >> this->name;
		cout << "\tRegister number: ";
		cin >> this->regNum;
		cout << "Total mark for 5 subject\n";
		for (int i = 0; i < 5; i++)
			cin >> this->mark[i];
	}
	//Out console 
	void Output()
	{
		cout << "Name: " << this->getName() << "\t" << "Reg Number:  "<<this->getReg() << endl;
		cout << "Total mark: ";
		for (int i = 0; i < 5; i++)
			printf("%3d", this->mark[i]);
	}
	//overload operator >(maximum sum total mark
	friend bool operator>(const Student& a, const Student& b);
	
};
bool operator>( Student& a, Student& b)
{
	return a.sumMark() > b.sumMark();
}
int main()
{
	int n;//count Student
	cout << "Please enter count student: ";
	cin >> n;
	Student* arS = new Student[n];//alloc memory
	//Input 
	for (int i = 0; i < n; i++)
	{
		arS[i].InputDate();
	}
	//find student scrored higgest
	int mxNum = 0;
	for (int i = 0; i < n; i++)
	{
		if (arS[i] > arS[mxNum])
			mxNum = i;
	}
	//output date 
	cout << "=============Student scored the higgest total=============\n";
	arS[mxNum].Output();
	delete[]arS;//free alocate memory
	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