Let's Think About This For a Second
In this project, we'll combine everything you've learned so far — variables, functions, and loops from the Basic chapter; arrays, pointers, and references from the Intermediate chapter; and classes, objects, and access specifiers from the Advanced chapter — to build a small console application called Student Record Manager. In Part 1, we'll design the Student class that forms the program's foundation, protect its private data members through encapsulation, and arrange things so they're only accessible through public methods. We'll use vector<Student> to keep the entire student list dynamically in memory. info In Part 2, we'll add the add/search/display features, and in Part 3 we'll wrap up the project with file save/load and error handling.
Let's Build It
Build the Student class with 3 private members: name (string), roll (int), and marks (double). Add a constructor along with getter methods like getName(), getRoll(), and getMarks() under public. Declare vector<Student> students; inside main(), and create a menu loop with while(true) — show the user choices: 1) Add Student 2) Display All 3) Exit. Lay out a skeleton with a switch statement so it can call the right function based on the user's choice — we'll fill in the logic in Part 2.
Example Code
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
private:
string name;
int roll;
double marks;
public:
Student(string n, int r, double m) {
name = n;
roll = r;
marks = m;
}
string getName() const { return name; }
int getRoll() const { return roll; }
double getMarks() const { return marks; }
};
int main() {
vector<Student> students;
int choice;
while (true) {
cout << "\n--- Student Manager ---\n";
cout << "1. Add Student\n";
cout << "2. Display All\n";
cout << "3. Exit\n";
cout << "Choice: ";
cin >> choice;
switch (choice) {
case 1:
// Part 2 မှာ ဖြည့်ပါမယ်
break;
case 2:
// Part 2 မှာ ဖြည့်ပါမယ်
break;
case 3:
cout << "Goodbye!\n";
return 0;
default:
cout << "Invalid choice!\n";
}
}
}When the program runs, it shows a menu with 3 options; choosing option 3 prints "Goodbye!" and the program ends.5-Minute Try It
Try adding a setter method called setMarks(double newMarks) to the Student class so you can update marks.
A Quick Word of Caution
Getting encapsulation right from the start makes it much smoother to add more code in Parts 2 and 3. Stick with this getter/setter pattern consistently.