Question 1
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int quiz1;
int quiz2;
int midtermExam;
int finalExam;
public:
Student() : name(""), quiz1(0), quiz2(0), midtermExam(0),
finalExam(0) {}
void setName(string n) { name = n; }
void setQuiz1(int q) { quiz1 = q; }
void setQuiz2(int q) { quiz2 = q; }
void setMidtermExam(int m) { midtermExam = m; }
void setFinalExam(int f) { finalExam = f; }
string getName() { return name; }
int getQuiz1() { return quiz1; }
int getQuiz2() { return quiz2; }
int getMidtermExam() { return midtermExam; }
int getFinalExam() { return finalExam; }
double calculateAverage() {
double quizAverage = (quiz1*10 + quiz2*10) / 2.0;
return 0.5 * finalExam + 0.25 * midtermExam + 0.25 *
quizAverage;
}
};
int main() {
Student student;
cout << "Enter student name: ";
string name;
getline(cin, name);
student.setName(name);
cout << "Enter quiz 1 score: ";
int quiz1;
cin >> quiz1;
student.setQuiz1(quiz1);
cout << "Enter quiz 2 score: ";
int quiz2;
cin >> quiz2;
, student.setQuiz2(quiz2);
cout << "Enter midterm exam score: ";
int midtermExam;
cin >> midtermExam;
student.setMidtermExam(midtermExam);
cout << "Enter final exam score: ";
int finalExam;
cin >> finalExam;
student.setFinalExam(finalExam);
cout << "Student record for: " << student.getName() << endl;
cout << "Quiz 1: " << student.getQuiz1() << endl;
cout << "Quiz 2: " << student.getQuiz2() << endl;
cout << "Midterm exam: " << student.getMidtermExam() << endl;
cout << "Final exam: " << student.getFinalExam() << endl;
cout << "Average: " << student.calculateAverage() << endl;
return 0;
}
Question 2
(a) The purpose of the keywords public and private in the class
declaration is to define access levels for the class members.
Public members can be accessed from anywhere, while private
members can only be accessed within the class itself or by friends
of the class.
(b) A class is a blueprint or a template that defines the
properties and behavior of an object. An object, on the other
hand, is an instance of a class, which has its own set of
attributes (data) and methods (functions).
(c) To "instantiate" an object means to create an instance of a
class.
(d) The purpose of a constructor is to initialize objects when
they are created. It is a special member function that is called
when an object is instantiated.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int quiz1;
int quiz2;
int midtermExam;
int finalExam;
public:
Student() : name(""), quiz1(0), quiz2(0), midtermExam(0),
finalExam(0) {}
void setName(string n) { name = n; }
void setQuiz1(int q) { quiz1 = q; }
void setQuiz2(int q) { quiz2 = q; }
void setMidtermExam(int m) { midtermExam = m; }
void setFinalExam(int f) { finalExam = f; }
string getName() { return name; }
int getQuiz1() { return quiz1; }
int getQuiz2() { return quiz2; }
int getMidtermExam() { return midtermExam; }
int getFinalExam() { return finalExam; }
double calculateAverage() {
double quizAverage = (quiz1*10 + quiz2*10) / 2.0;
return 0.5 * finalExam + 0.25 * midtermExam + 0.25 *
quizAverage;
}
};
int main() {
Student student;
cout << "Enter student name: ";
string name;
getline(cin, name);
student.setName(name);
cout << "Enter quiz 1 score: ";
int quiz1;
cin >> quiz1;
student.setQuiz1(quiz1);
cout << "Enter quiz 2 score: ";
int quiz2;
cin >> quiz2;
, student.setQuiz2(quiz2);
cout << "Enter midterm exam score: ";
int midtermExam;
cin >> midtermExam;
student.setMidtermExam(midtermExam);
cout << "Enter final exam score: ";
int finalExam;
cin >> finalExam;
student.setFinalExam(finalExam);
cout << "Student record for: " << student.getName() << endl;
cout << "Quiz 1: " << student.getQuiz1() << endl;
cout << "Quiz 2: " << student.getQuiz2() << endl;
cout << "Midterm exam: " << student.getMidtermExam() << endl;
cout << "Final exam: " << student.getFinalExam() << endl;
cout << "Average: " << student.calculateAverage() << endl;
return 0;
}
Question 2
(a) The purpose of the keywords public and private in the class
declaration is to define access levels for the class members.
Public members can be accessed from anywhere, while private
members can only be accessed within the class itself or by friends
of the class.
(b) A class is a blueprint or a template that defines the
properties and behavior of an object. An object, on the other
hand, is an instance of a class, which has its own set of
attributes (data) and methods (functions).
(c) To "instantiate" an object means to create an instance of a
class.
(d) The purpose of a constructor is to initialize objects when
they are created. It is a special member function that is called
when an object is instantiated.