CS304 Assignment 2 Solution 2024

Question

Let suppose there is a well-known government organization with a rich history in education and public services. This prestigious institution has always strived for excellence in every aspect of its operations, and now they are committed to modernizing and streamlining their student records management system. They want to keep track of their student record for obtaining such objective they hired you as an employee. You’ve been assigned the crucial task of creating specialized software for the institution responsible for overseeing and maintaining student records. This institution plays a pivotal role in ensuring the education and administrative needs of students are met with efficiency and precision. The program must be capable of storing and processing essential student information such as the student’s name, course enrollment, and unique student ID. It should also be able to calculate and display the sum of the student’s last three digits. Your goal is to develop a program that meets all of these requirements. In order to achieve these objectives, the following table should be used as a reference guide for understanding the key elements and concepts in the code they are asked to implement. It serves as an educational example to help students grasp these concepts, and it will prepare them for real-world software development. Additionally, this code simulates a simplified “Student Record Management System,” showing how these concepts can be applied to practical scenarios. The table is given as under.

Class Diagram:

Class Diagram
ElementExplanation
PurposeThis code is designed to demonstrate important concepts in C++ programming, including OOP, polymorphism, pure virtual function, and more. It serves as an educational example to help students grasp these concepts.
LibrariesThe code includes the <iostream> header for input/output operations, allowing interaction with the user through the console. Students should be familiar with the role of libraries in C++ programming.
Class DefinitionsThree classes are defined: Base, child1, and child2, illustrating inheritance and polymorphism. Students should understand how class hierarchies work and how to implement polymorphism by extending base class.
PolymorphismThe Base class declares a pure virtual function display (), which is implemented in the derived classes child1 and child2 respectively. In child1 the display () function is going to display student name, while in child2 the same function is going to display course like “CS304P” .This concept exemplifies polymorphism, where different classes provide their own implementation of a common interface.
Function DefinitionsFunctions like showID () and sumLastThreeDigits () are defined outside of these classes, ShowID () is going to manipulate and display student ID and   the function sumLastThreeDigits () is used to calculate sum of the last three digits of student ID. Note:  function  sumLastThreeDigits () checks for errors in the array size  if the size of an array received is less than 3, show the message cannot calculate the sum of the last three digits. Loops and conditional statements are used in both functions showID () and sumLastThreeDigits () to control the flow of the program. Students should understand how loops and conditionals affect program flow.
MainIn the main function, a pointer p of type Base* is used to demonstrate dynamic polymorphism when calling the display function. This helps students grasp the idea that the function to execute is determined at runtime based on the object type.In the main also declare an array which is composed of student ID (only integer part), and calculate its size as well.Create the objects  d1, and d2 of the inherited classes child1 and child2 respectivelyInitialize the string variables S1= “name” and S2= “CS304P”.Assign the object of child1 class to the pointer p and then pass the string variable S1 to display function define in child1 using pointer. Similarly repeat the same procedure for class child2 respectively. Call the showID () function and pass two arguments array and size of an array. And at the end call the,sumLastThreeDigits () function which receive the same parameters. Note: the array contains only the integer/number part of the student ID as shown in the screen short. The array is passed as an argument to both functions namely ShowID () and sumLastThreeDigits (). The former is going to show student ID and the latter is going to calculate and show the sum of the last three digits of student ID.
Output required

Answer

#include <iostream>
#include <string>

// https://universitydistancelearning.com/

using namespace std;

class Person{
	string name;
	char gender;
	int age;
	
	public:
		Person(string n, char g, int ag){
			setName(n);
			setGender(g);
			setAge(ag);
		}
		
		//setters functions
		void setName(string nam){
			name = nam;
		}
		
		void setGender(char g){
			gender = g;
		}
		
		void setAge(int ag){
			age = ag;
		}
		//getters functions
		string getName(){
			return name;
		}
		
		char getGender(){
			return gender;
		}
		
		int getAge(){
			return age;
		}
		
		virtual void displayData() = 0;
		
};

class Student : public Person {
	string studentId;
	string degreeProgram;
	float feePerSem;
	
	public:
		Student(string name, char gender, int age, string id, string degProg, float semFee) :Person(name, gender, age){

			setId(id);
			setDegreeProgram(degProg);
			setSemmesterFee(semFee);
		}
		//setter functions
		void setId(string id){
			studentId = id;
		}
		
		void setDegreeProgram(string program){
			degreeProgram = program;
		}
		
		void setSemmesterFee(float fee){
			feePerSem = fee;
		}
		
	//getter functions
	
	string getId(){
		return studentId;
	}
	
	string getDegreeProgram(){
		return degreeProgram;
	}
	
	float getSemmesterFee(){
		return feePerSem;
	}
	
	void displayData(){
		cout<<"---------------------------------------------"<<endl;
		cout<<"----------------Student Record---------------"<<endl;
		cout<<"---------------------------------------------"<<endl<<endl;
		cout<<"Name: "<<getName()<<endl;
		cout<<"Gender: "<<getGender()<<endl;
		cout<<"Age: "<<getAge()<<endl;
		cout<<"Student Id: "<<getId()<<endl;
		cout<<"Degree Program: "<<getDegreeProgram()<<endl;
		cout<<"Fee Per Semmester: "<<getSemmesterFee()<<endl;
		cout<<"---------------------------------------------"<<endl<<endl;
	}
};

class Teacher : public Person {
	float salary;
	int allocatedStudents;
	
	public:
		Teacher(string name, char gender, int age, int allocStd, float salary): Person(name, gender, age){
	
			setSalary(salary);	
			setAllacatedStudents(allocStd);
		}
		
		//setter functions
		setSalary(float sal){
			salary = sal;
		}
		
		setAllacatedStudents(int stdAlloc){
			allocatedStudents = stdAlloc;
		}
		//getter functions
		getSalary(){
			return salary;
		}
		
		getAllacatedStudents(){
			allocatedStudents;
		}
		
			
	void displayData(){
		cout<<"---------------------------------------------"<<endl;
		cout<<"----------------Teacher Record---------------"<<endl;
		cout<<"---------------------------------------------"<<endl<<endl;
		cout<<"Name: "<<getName()<<endl;
		cout<<"Gender: "<<getGender()<<endl;
		cout<<"No. of Allocated Students: "<<getAllacatedStudents()<<endl;
		cout<<"Salary: "<<getSalary()<<endl;
		cout<<"---------------------------------------------"<<endl;
	}
		
};


int main(){
	int totalPersons = 2;
	Person *p[totalPersons];
	Student std("Samra", 'f', 27, "BC000000", "BSCS", 22000);
	Teacher techr("Zubair", 'm',  32, 22, 30000);
	p[0] = &std;
	p[1] = &techr;
	
	for(int i = 0; i < totalPersons; i++){
		p[i]->displayData();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *