← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 16 of 17

C++ का Mini Project — Student Management

यह mini project students add, find, update और remove करने के लिए एक simple student record system बनाता है, एक digital class register की तरह। यह classes और collections साथ practice करता है।

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक student के लिए enter किया input validate न करना, जैसे एक negative या non-numeric grade।
  2. search करते वक्त अलग letter case वाले names compare करना, इसलिए एक valid record नहीं मिलता।
  3. decimals चाहने वाली grades के लिए int इस्तेमाल करना, जो average truncate कर देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.