Answer to Question #332728 in C++ for king

Question #332728

Construct a C++ program having base class Student consisting protected member’s student name, student reg, student branch and employee rno. Define a member function input() having void return type for initializing the above said data members. Use this class to create new class Result having data member subject name ,subject marks, total marks and grade that calculate the grade for each course by using marks range given below and print the grade of each course by defining member function output() with void return type. Total marks Grade Calcultion 95-100 ‘O’ 90-94 ‘A+’ 85-89 ‘A’ 80-84 ‘B+’ 70-79 ‘B’ 60-69 ‘C’ 40-59 ‘D’ <40 ‘E'


1
Expert's answer
2022-04-23T18:13:42-0400


#include <iostream>
#include <vector>
#include <string.h>
#include <iomanip>
#include <string>
using namespace std;
class Student
{
protected:
	char name[25];//Name student
	char reg[7];//In different country different show
	char branch[25];
	char emplNum[10];//You can change it in the future
public:
	Student()
	{
		//Default constructor
		strcpy(reg, "AA12345");//for example 
	}
	Student(const char* name, const char* rg, const char* br, const char* em)
	{
		strcpy(this->name, name);
		strcpy(reg, rg);
		strcpy(branch, br);
		strcpy(emplNum, em);
	}
	//sets
	void setName(const char* nm)
	{
		strcpy(this->name, nm);
	}
	void setReg(const char* rg)
	{
		strcpy(this->reg, rg);
	}
	void setBranch(const char* br)
	{
		strcpy(this->branch, br);
	}
	void setEmplNum(const char* e)
	{
		strcpy(this->branch, e);
	}
	//Input
	void input()
	{
		cout << "Name: ";
		cin >> name;
		cout << "Registration num: ";
		cin >> reg;
		cout << "Branch: ";
		cin >> branch;
		cout << "Employee number: ";
		cin >> emplNum;
	}
	//define information method wich print all information to standart output stream
	void output()
	{
		cout << "|" << setw(20) << name << "|" << setw(7) << reg << "|" << setw(20) << branch << "|" << setw(10) << emplNum << "|\n";
	}
};
//Derived from Student class
class Results:public Student
{
public:
	//define struct subject
	struct  Subject
	{
		char nameSub[25];//Subject name
		unsigned marks[10];//marks
		Subject()
		{


		}
		Subject(const char* nm)
		{
			strcpy(this->nameSub, nm);
		}
		void setMarks(unsigned ind,unsigned value)
		{
			if (ind >= 0 && ind < 10)
				marks[ind] = value;
		}
		unsigned getSumm()//Summ all mark
		{
			unsigned ans = 0;
			for (unsigned i = 0; i < 10; i++)
				ans += marks[i];
			return ans;
		}
		
		string getTotaMark()
		{
			unsigned ans = getSumm();
			if (ans >= 95)
				return "O";
			else if (ans >= 90)
				return "A+";
			else if (ans >= 85)
				return "A";
			else if (ans >= 80)
				return "B+";
			else if (ans >= 70)
				return "B";
			else if (ans >= 60)
				return "C";
			else if (ans >= 40)
				return "D";
			return "E";
		}
	};
	Results(int cntSub=10):Student()
	{
		this->sub = new Subject[cntSub];
		sizeSub = cntSub;
	}
	Results(const char* name, const char* rg, const char* br, const char* em,int cn):Student(name,rg,br,em)
	{
		this->sub = new Subject[cn];
		sizeSub = cn;
	}
	void InputData()
	{
		for (int i = 0; i < sizeSub; i++)
		{
			cout << "Subject name: ";
			cin >> sub[i].nameSub;
			cout << "Please input marks(1-10 point)\n";
			for (int j = 0; j < 10; j++)
				cin >> sub[i].marks[j];
		}
	}
	//Out information
	void ReportResults()
	{
		output();
		for (int i = 0; i < sizeSub; i++)
		{
			cout << "Subject : " << sub[i].nameSub << "  Total Grade: " << sub[i].getTotaMark() << endl;
		}
	}
	~Results()
	{
		delete[]sub;
	}
private:
	Subject* sub;
	unsigned sizeSub;
	string total;
};
int main()
{
    //Example 
	Results res("Marry", "AA1452", "BSBRANCH", "111111A", 3);
	res.InputData();
	res.ReportResults();
	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