Pract 2
Pract 2
2
Input : -
#include <iostream> this->dlno = new long;
#include <string> }
#include <limits> // For std::numeric_limits ~StudData() {
using namespace std; delete this->telno;
//Edited by Ashtrick delete this->dlno;
class StudData; // Forward declaration }
class Student { void getStudData() {
string name; cout << "Enter Contact Address: ";
int roll_no; cin.ignore(); // Clear the input buffer
string cls; getline(cin, this->caddress);
string division; // Changed to std::string cout << "Enter Telephone Number: ";
string dob; cin >> *this->telno;
string bloodgroup; // Changed to std::string while (cin.fail()) { // Check for fail state
static int count; cin.clear(); // Clear the fail state
public: cin.ignore(numeric_limits<streamsize>::max(),
Student() { // Default Constructor '\n'); // Ignore invalid input
this->name = ""; cout << "Invalid input. Please enter a numeric
this->roll_no = 0; telephone number: ";
this->cls = ""; cin >> *this->telno;
this->division = ""; }
this->dob = "dd/mm/yyyy"; cout << "Enter Driving License Number: ";
this->bloodgroup = ""; cin >> *this->dlno;
} while (cin.fail()) { // Check for fail state
Student(const Student& other) { // Copy cin.clear(); // Clear the fail state
Constructor cin.ignore(numeric_limits<streamsize>::max(),
this->name = other.name; '\n'); // Ignore invalid input
this->roll_no = other.roll_no; cout << "Invalid input. Please enter a numeric
this->cls = other.cls; driving license number: ";
this->division = other.division; cin >> *this->dlno;
this->dob = other.dob; }
this->bloodgroup = other.bloodgroup; }
} void dispStudData() {
static int getCount() { cout << "Contact Address: " << this->caddress
return count; << endl;
} cout << "Telephone Number: " << *this->telno
void getData(StudData*); << endl;
void dispData(StudData*); cout << "Driving License Number: " << *this-
}; >dlno << endl;
class StudData { }
string caddress; };
long int* telno; inline void Student::getData(StudData* st) {
long int* dlno; cout << "Enter Student Name: ";
friend class Student; getline(cin, this->name); // No need for
public: cin.ignore() before this
StudData() { cout << "Enter Roll Number: ";
this->caddress = ""; cin >> this->roll_no;
this->telno = new long;
cin.ignore(); // Clear the input buffer after StudData* stud2[100];
reading an integer int n = 0;
cout << "Enter Class: "; char ch;
getline(cin, this->cls); // No need for do {
cin.ignore() before this stud1[n] = new Student;
cout << "Enter Division: "; stud2[n] = new StudData;
cin >> this->division; // Use std::string directly
cin.ignore(); // Clear the input buffer after stud1[n]->getData(stud2[n]);
reading a string n++;
cout << "Enter Date of Birth: ";
getline(cin, this->dob); // No need for cout << "Do you want to add another student
cin.ignore() before this (y/n): ";
cout << "Enter Blood Group: "; cin >> ch;
cin >> this->bloodgroup; // Use std::string cin.ignore(); // Clear the input buffer
directly } while (ch == 'y' || ch == 'Y');
st->getStudData(); for (int i = 0; i < n; i++) {
this->count++; cout << "----------------------------------------------
} -----------------" << endl;
inline void Student::dispData(StudData* st1) { stud1[i]->dispData(stud2[i]);
cout << "Student Name: " << this->name << }
endl; cout << "----------------------------------------------
cout << "Roll Number: " << this->roll_no << -----------------" << endl;
endl; cout << "Total Students: " <<
cout << "Class: " << this->cls << endl; Student::getCount() << endl;
cout << "Division: " << this->division << endl; cout << "----------------------------------------------
cout << "Date of Birth: " << this->dob << endl; -----------------" << endl;
cout << "Blood Group: " << this->bloodgroup
<< endl; for (int i = 0; i < n; i++) {
st1->dispStudData(); delete stud1[i];
} delete stud2[i];
int Student::count = 0; // Initialize static count }
variable return 0;
int main() { }
Student* stud1[100];
Output : -
Enter Student Name: Ketan Kishor Belekar Student Name: Pratik Gaikwad
Enter Roll Number: 46 Roll Number: 46
Enter Class: SE DSE Class: SE DSE
Enter Division: C Division: C
Enter Date of Birth: 02/09/2005 Date of Birth: 02/09/2005
Enter Blood Group: O+ Blood Group: O+
Enter Contact Address: 3536676984 Contact Address: 3536676984
Enter Telephone Number: 65341676754 Telephone Number: 65341676754
Enter Driving License Number: Driving License Number: 5253657689779
5253657689779
Do you want to add another student (y/n): n Total Students: 1