Answer to Question #247969 in C++ for Rajeev

Question #247969

Write a program to create a class called STUDENT with data members Roll

Number, Name and Age. Using inheritance, create the classes

UGSTUDENT and PGSTUDENT having fields a semester, fees and

stipend. Enter the data for at least 5 students. Find the average age for all

UG and PG students separately.


1
Expert's answer
2021-10-08T11:45:33-0400
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student
{
public:
	Student(int rn, string nm, unsigned ag) :
		rollNumber{ rn },
		name{ nm },
		age{ ag }
	{}
	Student() {};
	int  rollNumber;
	string name;
	unsigned age;
};
class UgStudent : public Student
{
public:
	UgStudent(int rn, string nm, unsigned ag, string sm,double st,double fs)
		: Student(rn,nm,ag),
		semestr{sm},
		stipend{st},
		fees{fs}
	{}
	UgStudent() {};
	string semestr;
	double stipend;
	double fees;
};


class PgStudent : public UgStudent
{
public:
	PgStudent(int rn, string nm, unsigned ag, string sm, double st, double fs)
		:UgStudent(rn, nm,  ag,  sm,  st,  fs)
	{}
	PgStudent() {};
};
int main()
{
	vector<Student> stud;
	vector<PgStudent> studPg;
	vector<UgStudent> studUg;
	unsigned choise{ 4 };
	int rn;
	string nm;
	unsigned ag;
	string sm;
	double st;
	double fs;
	cout << "Enter the data of students at least 5 persons" << "\n";
	// Entering student data
	cout << "Selection numbers for data entry: "<<"\n";
	cout << "    1 - Student" << "\n";
	cout << "    2 - Student UG" << "\n";
	cout << "    3 - Student PG" << "\n";
	cout<<  "    Other number - Complete data entry" << "\n";
do{
	cout << "Enter an positive integer to define the type of data entry: ";
	cin >> choise;
	if (choise > 3) 
	{
		cout << "Complete data entry" << "\n"; 
		break ;
	}
	cout << "Enter Roll Number: ";
	cin >> rn;
	cout << "Enter Name: ";
	cin >> nm;
	cout << "Enter Age: ";
	cin >> ag;
	if (choise == 1)
	{
		stud.push_back( Student(rn, nm, ag));
		continue;
	}
	cout << "Enter Semestr: ";
	cin >> sm;
	cout << "Enter Stipende: ";
	cin >> st;
	cout << "Enter Fees: ";
	cin >> fs;
	if (choise == 2)
	{
		studUg.push_back( UgStudent(rn, nm, ag, sm, st, fs));
	}
	if (choise == 3)
	{
		studPg.push_back( PgStudent(rn, nm, ag, sm, st, fs));
	}
	} while (true);
	// Counting average age 
	int ageSum ;
	ageSum = 0;
	if (size(studUg) != 0) 
	{
		for (int i = 0; i < size(studUg); i ++ )
			ageSum += studUg[i].age;
		double avgUg;
		avgUg = ageSum/ (double)size(studUg);
		cout << "Average age Ug Student: " << avgUg << "\n";
	}
	ageSum = 0;
	if (size(studPg) != 0)
	{
		for (int i = 0; i < size(studPg); i ++ )
			ageSum += studPg[i].age;
		double avgPg;
		avgPg = ageSum / (double)size(studPg);
		cout << "Average age Pg Student: " << avgPg << "\n";
	}
	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