0% found this document useful (0 votes)
10 views4 pages

C++ Student Management System Code

This C++ program defines two classes - Student and StudData. The Student class stores student details like name, roll number etc. and StudData stores contact details. The main function takes student details from the user using the getData method and stores it using dynamic memory allocation. It then displays the collected details using the dispData method. The program allows adding multiple student records and finally displays the total count of students added.

Uploaded by

jagruti mahajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

C++ Student Management System Code

This C++ program defines two classes - Student and StudData. The Student class stores student details like name, roll number etc. and StudData stores contact details. The main function takes student details from the user using the getData method and stores it using dynamic memory allocation. It then displays the collected details using the dispData method. The program allows adding multiple student records and finally displays the total count of students added.

Uploaded by

jagruti mahajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical

#include<iostream
>
#include<string.h>
using namespace std;
class StudData;
class Student{
string name;
int roll_no;
string cls;
char* division;
string dob;
char* bloodgroup;
static int count;
public:
Student() // Default Constructor
{
name="";
roll_no=0;
cls="";
division=new char;
dob="dd/mm/yyyy";
bloodgroup=new char[4];
}
~Student()
{
delete division;
delete[] bloodgroup;
}
static int getCount()
{
return count;
}
void getData(StudData*);
void dispData(StudData*);
};
class StudData{
string caddress;
long int* telno;
long int* dlno;
friend class Student;
public:
StudData()
{
caddress="";
telno=new long;
dlno=new long;
}
~StudData()
{
delete telno;
delete dlno;
}
void getStudData()
{
cout<<"Enter Contact Address : ";
[Link]();
getline(cin,caddress);
cout<<"Enter Telephone Number : ";
cin>>*telno;
cout<<"Enter Driving License Number : ";
cin>>*dlno;
}
void dispStudData()
{
cout<<"Contact Address : "<<caddress<<endl;
cout<<"Telephone Number : "<<*telno<<endl;
cout<<"Driving License Number : "<<*dlno<<endl;
}
};
inline void Student::getData(StudData* st)
{
cout<<"Enter Student Name : ";
getline(cin,name);
cout<<"Enter Roll Number : ";
cin>>roll_no;
cout<<"Enter Class : ";
[Link]();
getline(cin,cls);
cout<<"Enter Division : ";
cin>>division;
cout<<"Enter Date of Birth : ";
[Link]();
getline(cin,dob);
cout<<"Enter Blood Group : ";
cin>>bloodgroup;
st->getStudData();
count++;
}
inline void Student::dispData(StudData* st1)
{
cout<<"Student Name : "<<name<<endl;
cout<<"Roll Number : "<<roll_no<<endl;
cout<<"Class : "<<cls<<endl;
cout<<"Division : "<<division<<endl;
cout<<"Date of Birth : "<<dob<<endl;
cout<<"Blood Group : "<<bloodgroup<<endl;
st1->dispStudData();
}
int Student::count;
int main()
{
Student* stud1[100];
StudData* stud2[100];
int n=0;
char ch;
do
{
stud1[n]=new Student;
stud2[n]=new StudData;
stud1[n]->getData(stud2[n]);
n++;
cout<<"Do you want to add another student (y/n) : ";
cin>>ch;
[Link]();
} while (ch=='y' || ch=='Y');
for(int i=0;i<n;i++)
{

cout<<"---------------------------------------------------------------
"<<endl;
stud1[i]->dispData(stud2[i]);
}

cout<<"---------------------------------------------------------------
"<<endl;
cout<<"Total Students : "<<Student::getCount();

cout<<endl<<"---------------------------------------------------------
------"<<endl;
for(int i=0;i<n;i++)
{
delete stud1[i];
delete stud2[i];
}
return 0;
}

Common questions

Powered by AI

Using raw pointers for memory management, as seen in this program, presents several drawbacks. Raw pointers lack automatic memory management, making the program susceptible to memory leaks if 'delete' is not appropriately called for every 'new' allocation. They also pose risks of undefined behavior, such as dangling pointers and double deletions if mishandled. Furthermore, raw pointers complicate exception safety since thrown exceptions prior to deallocation might leave allocated memory stranded. Replacing raw pointers with smart pointers like std::unique_ptr or std::shared_ptr enhances safety, providing automated and exception-safe deallocation and reducing overhead associated with manual memory management .

The Student class maintains the count of the number of student objects created using a static member variable called 'count'. The value of 'count' is incremented each time the getData method is called for a new student object creation. The static nature of the 'count' variable ensures that it is shared among all instances of the Student class, thereby accurately reflecting the total number of student objects created .

The program utilizes a static member function, getCount, within the Student class. Its role is to provide access to the static member variable 'count', which tracks the number of Student objects instantiated. Since static member functions do not operate on specific instance variables, they can be called without an object and are associated with the class itself. This function enhances program functionality by allowing external access to the cumulative count of created students, offering a global perspective on data handled across the application .

The Student and StudData classes exhibit a friend relationship where StudData is declared as a friend inside the Student class. This relationship allows the Student class to access private data members of StudData, such as 'caddress', 'telno', and 'dlno'. The interaction occurs in the getData and dispData methods of the Student class, where detailed student data is gathered or displayed using methods from both classes. The Student class accesses and manipulates the data within StudData objects, showcasing a tight coupling facilitated by the friend function mechanism .

To handle incorrect input, the program could be modified to include input validation and error checking mechanisms. For instance, when reading numeric inputs such as roll number and telephone number, the program could check if the input is a valid number using cin.fail() and throw an error message before prompting for input again. Additionally, the use of regular expressions could help validate the format of the date of birth and division strings. Implementing try-catch blocks could also handle exceptions that arise from invalid user inputs more gracefully .

The separation of concerns between personal and additional student data into distinct classes, Student and StudData, exemplifies a modular design approach that promotes maintainability and clarity. By compartmentalizing related responsibilities, each class can evolve independently for its respective concern, reducing complexity. This decouples personal information and contact or license details, adhering to single responsibility principles. However, this design also leads to increased interdependence with friend relationships, which could affect maintainability negatively if not managed carefully. Protecting class invariants becomes harder with such tight coupling which may require further refactoring .

In the main function, Student and StudData objects are dynamically allocated using the 'new' operator when handling student entries. The objects are stored in arrays (stud1[] and stud2[]), indexed to keep track of each entry incrementally. A do-while loop manages the creation of these objects, continuing based on user input. Deallocation occurs after data display, where the 'delete' operator is used within a loop to free each allocated object, thus preventing memory leaks. This dynamic handling allows elastic resource management suited for varied numbers of student entries without static memory constraints .

Inline member functions, like getData and dispData in the Student class, are used to optimize performance by reducing the function call overhead. When these functions are declared as inline, the compiler attempts to expand them at the call site, potentially improving execution speed for small, frequently called functions. This can be beneficial in scenarios where such functions involve simple operations like setting and displaying member variables. However, it's crucial to note that the use of the 'inline' keyword is merely a request to the compiler, which may or may not inline the function based on its internal optimization strategies .

Data encapsulation in the Student and StudData classes is achieved by making class members private, controlling access to these members through public methods, such as getData and dispData. This encapsulation ensures that the internal state of an object is protected, and data is modulated through tightly controlled interfaces. Abstraction is provided by these classes, as they expose operations relevant to managing student data and additional details, hiding underlying implementation details from a user perspective. This design allows users to interact with student data structures without needing to manage or understand their internal mechanics .

Memory management is primarily handled through dynamic allocation and deallocation using new and delete operators. In the Student class, dynamic memory is allocated for the 'division' and 'bloodgroup' members, and in the StudData class, for the 'telno' and 'dlno' members. To prevent memory leaks, destructors are defined in both classes to release allocated memory when objects go out of scope: ~Student deletes the memory allocated for 'division' and 'bloodgroup', while ~StudData deletes the memory allocated for 'telno' and 'dlno'. This ensures all dynamically allocated resources are properly deallocated .

You might also like