C++ का Mini Project — Student Management
In this page:
Student Model Define करना
एक student को ID, name, और grades के एक collection जैसे fields वाली एक class या struct की तरह model करना आपको store, pass around, और operate करने के लिए एक well-defined unit देता है, names, IDs, और grades के parallel arrays track करने की बजाय जो sync से बाहर drift हो सकते थे।
उदाहरण: Defining the Student Model
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
std::string name;
};
// Program execution starts in main()
int main() {
Student s{1, "Alex"};
// Print to the console with cout
std::cout << s.id << " " << s.name << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Student Collection Manager
एक std::vector<Student> memory में students records का पूरा collection hold करता है, आपको vector operations का पूरा toolkit -- iterating, searching, sorting -- free में देते हुए, manual bounds tracking वाला एक fixed-size array manage करने की बजाय।
उदाहरण: Student Collection Manager
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <string>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
std::string name;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1, "Alex"}, {2, "Sam"}};
// Print to the console with cout
std::cout << students.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Student Records Search करना
ID से search करना typically vector में loop करके हर student की ID field को target से compare करता है, क्योंकि IDs unique होने की उम्मीद है; name से search करने में शायद same name share करने वाले कई students handle करने और सिर्फ पहला नहीं बल्कि हर match return करने की ज़रूरत हो।
उदाहरण: Searching Student Records
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <string>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
std::string name;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1, "Alex"}, {2, "Sam"}};
// Range-based for loop: visit every element of students
for (const auto &s : students) {
if (s.id == 2) std::cout << s.name << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Student Grades Update करना
एक grade update करने का मतलब है पहले collection में सही Student object locate करना (आमतौर पर ID से), फिर सीधे इसकी grade field modify करना -- क्योंकि एक vector actual objects hold करता है कहीं और copies नहीं, यह change तुरंत हर जगह reflect होता है जहाँ वह student reference होता है।
उदाहरण: Updating Student Grades
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
int grade;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1, 80}, {2, 90}};
// Range-based for loop: visit every element of students
for (auto &s : students) {
if (s.id == 1) s.grade = 95;
}
// Print to the console with cout
std::cout << students[0].grade << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Student Profiles Remove करना
std::vector::erase(iterator) एक दी गई position पर एक single element remove करता है, gap भरने के लिए हर बाद का element नीचे shift करते हुए -- पहले target student का iterator locate करने वाली एक search के साथ combine करके, यह एक specific record delete करने का standard pattern है।
उदाहरण: Removing Student Profiles
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1}, {2}, {3}};
auto it = std::find_if(students.begin(), students.end(), [](Student &s) { return s.id == 2; });
if (it != students.end()) students.erase(it);
// Print to the console with cout
std::cout << students.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक student के लिए enter किया input validate न करना, जैसे एक negative या non-numeric grade।
- search करते वक्त अलग letter case वाले names compare करना, इसलिए एक valid record नहीं मिलता।
- decimals चाहने वाली grades के लिए
intइस्तेमाल करना, जो average truncate कर देता है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced