The document defines a C++ class called Student with data members for age, roll number, and marks in three subjects. It includes methods to get student information from the user, calculate percentage and grade from the marks, and output a summary. The main function creates two Student objects, gets their data, calls the methods to find percentages and grades, and prints a summary for each student.
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
LAB 2 Report.docx
1. 1 | P a g e
CS-212 Object Oriented Programming
National University of Sciences and Technology
School of Electrical Engineering and Computer Science
CS-212 Object Oriented Programming
LAB No. 02
Section: BEE 12-C
Submitted to: Ms. Shakeela Bibi
Instructor: Dr. Khuram Shahzad
Name CMS ID
Ahamed Musharaf 356481
2. 2 | P a g e
CS-212 Object Oriented Programming
LAB TASKS
Task
Develop a program that implements a C++ class “Student” with following data members
Age
Roll Number
markSubject1
marksSubject2
marksSubject3
The class should define a method/function to calculate percentage from marks obtained in three subjects.
Finally, you’ll create objects of Student class, initialize their data members through set and get functions,
calculate percentages, determine grades and print summary in the end.
Code:
//The given code defines a class Student
//The function getinfo() accepts the age,student roll no, and marks of three subjects
from the user and putdata() display this information.
#include <iostream>
#include <stdio.h>
using namespace std;
//Class which contains the attributes of Students
class student
{
private:
int age, rollno;
int m1, m2, m3;
public:
void getinfo() //getting info from user about the student
{
cout << "Enter Student Age: ";
cin >> age;
cout << "Enter Student Roll No: ";
cin >> rollno;
cout << "Enter the marks obtained in Subject1: ";
cin >> m1;
cout << "Enter the marks obtained in Subject2: ";
3. 3 | P a g e
CS-212 Object Oriented Programming
cin >> m2;
cout << "Enter the marks obtained in Subject3: ";
cin >> m3;
}
//calculating percentage
float getPercentage()
{
float percentage;
//converting the integer percentage calculated into float percentage using
static_cast function
percentage = static_cast <float>(m1 + m2 + m3) / 3;
return percentage;
}
//calculating the Grade obtained by the student
char getGrade()
{
char grade;
float percentage = getPercentage();
if (percentage < 40) {
return 'F';
}
else if (percentage < 50) {
return 'D';
}
else if (percentage < 60) {
return 'C';
}
else if (percentage < 75) {
return 'B';
}
else {
return 'A';
}
}
void putdata() // to get the summary
{
cout << "Age: " << age << endl;
cout << "Roll number: " << rollno << endl;
cout << "Marks obtained in Subject1: " << m1 << endl;
cout << "Marks obtained in Subject2: " << m2 << endl;
cout << "Marks obtained in Subject3: " << m3 << endl;
cout << "Percentage: " << getPercentage() << endl;
cout << "Grade obtained: " << getGrade() << endl;
}
};
int main() {
//student object s1 and s2 created in main
4. 4 | P a g e
CS-212 Object Oriented Programming
//The age, roll no, and marks are ontained from the user by using the class
student
student s1;
student s2;
cout << "Enter below the details of the student Ahamed," << endl;
s1.getinfo();
cout << "n";
cout << "Enter below the details of the student Musharaf," << endl;
s2.getinfo();
cout << "n";
//displaying the percentage and grade of both the students using the created
getPercentage and getGrade functions
cout << "Ahamed's percentage is: " << s1.getPercentage() << endl;
cout << "Musharaf's percentage is: " << s2.getPercentage() << endl;
cout << "n";
cout << "Ahamed's grade is: " << s1.getGrade() << endl;
cout << "Musharaf's grade is: " << s2.getGrade() << endl;
cout << "n";
//Display of summary of both the students
cout << "Ahamed's summary: " << endl;
s1.putdata();
cout << "n";
cout << "Musharaf's summary: " << endl;
s2.putdata();
return 0;
}