The concept of inheritance is very similar to the real world. Just like a son inherits the properties (characteristics and behavior) of his father and father himself inherits the properties of the son's grandfather. In programming norms, inheritance occurs when one class inherits the properties of another class(base).
As the name defines, it is the hierarchy of classes. There is a single base class and multiple derived classes. Furthermore, the derived classes are also inherited by some other classes. Thus a tree-like structure is formed of hierarchy.
Syntax:
Class Parent
{
statement(s);
};
Class Derived1: public Parent
{
statement(s);
};
Class Derived2: public Parent
{
statement(s);
};
class newderived1: public Derived1
{
statement(s);
};
class newderived2: public Derived2
{
statement(s);
};
Student Attendance System
Derived Classes:
RegularStudent (tracks attendance for regular students)
OnlineStudent (tracks login sessions instead of physical attendance)
Example
#include <iostream>
using namespace std;
// Base Class: Student
class Student {
protected:
string name;
int rollNumber;
public:
// Constructor
Student(string n, int roll) {
name = n;
rollNumber = roll;
}
// Display Student Details
void displayStudentInfo() {
cout << "\nStudent Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
}
};
// Derived Class: RegularStudent (inherits from Student)
class RegularStudent : public Student {
private:
int totalClasses;
int attendedClasses;
public:
// Constructor
RegularStudent(string n, int roll, int total) : Student(n, roll) {
totalClasses = total;
attendedClasses = 0;
}
// Mark Attendance
void markAttendance() {
if (attendedClasses < totalClasses) {
attendedClasses++;
cout << "Attendance Marked! Total Attended: " << attendedClasses << "/" << totalClasses << endl;
} else {
cout << "All classes attended!" << endl;
}
}
// Display Attendance Details
void displayAttendance() {
displayStudentInfo();
cout << "Total Classes: " << totalClasses << endl;
cout << "Attended Classes: " << attendedClasses << endl;
double percentage = (double)attendedClasses / totalClasses * 100;
cout << "Attendance Percentage: " << percentage << "%" << endl;
}
};
// Derived Class: OnlineStudent (inherits from Student)
class OnlineStudent : public Student {
private:
int totalSessions;
int attendedSessions;
public:
// Constructor
OnlineStudent(string n, int roll, int sessions) : Student(n, roll) {
totalSessions = sessions;
attendedSessions = 0;
}
// Mark Session Attendance
void markSession() {
if (attendedSessions < totalSessions) {
attendedSessions++;
cout << "Session Marked! Total Attended: " << attendedSessions << "/" << totalSessions << endl;
} else {
cout << "All sessions attended!" << endl;
}
}
// Display Session Attendance
void displaySessionAttendance() {
displayStudentInfo();
cout << "Total Sessions: " << totalSessions << endl;
cout << "Attended Sessions: " << attendedSessions << endl;
double percentage = (double)attendedSessions / totalSessions * 100;
cout << "Session Attendance Percentage: " << percentage << "%" << endl;
}
};
// Main Function
int main() {
string name;
int roll, total, choice, markChoice;
cout << "Choose Student Type:\n1. Regular Student\n2. Online Student\nEnter choice: ";
cin >> choice;
cout << "Enter Student Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Roll Number: ";
cin >> roll;
if (choice == 1) {
// Regular Student
cout << "Enter Total Classes: ";
cin >> total;
RegularStudent rs(name, roll, total);
rs.displayAttendance();
while (true) {
cout << "\n1. Mark Attendance\n2. Show Attendance\n3. Exit\nEnter choice: ";
cin >> markChoice;
if (markChoice == 1)
rs.markAttendance();
else if (markChoice == 2)
rs.displayAttendance();
else
break;
}
} else if (choice == 2) {
// Online Student
cout << "Enter Total Sessions: ";
cin >> total;
OnlineStudent os(name, roll, total);
os.displaySessionAttendance();
while (true) {
cout << "\n1. Mark Session Attendance\n2. Show Attendance\n3. Exit\nEnter choice: ";
cin >> markChoice;
if (markChoice == 1)
os.markSession();
else if (markChoice == 2)
os.displaySessionAttendance();
else
break;
}
} else {
cout << "Invalid choice!" << endl;
}
return 0;
}
Choose Student Type:
1. Regular Student
2. Online Student
Enter choice: 2
Enter Student Name: Anjali
Enter Roll Number: 722035
Enter Total Sessions: 40
Student Name: Anjali
Roll Number: 722035
Total Sessions: 40
Attended Sessions: 0
Session Attendance Percentage: 0%
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 1/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 2/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 3/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 4/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 2
Student Name: Anjali
Roll Number: 722035
Total Sessions: 40
Attended Sessions: 4
Session Attendance Percentage: 10%
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 5/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 6/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 7/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 8/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 9/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 10/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 11/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 12/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 13/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 2
Student Name: Anjali
Roll Number: 722035
Total Sessions: 40
Attended Sessions: 13
Session Attendance Percentage: 32.5%
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 14/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 15/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 16/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 17/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 18/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 19/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 20/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 21/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 22/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 23/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 24/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 25/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 26/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 27/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 28/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 29/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 30/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 31/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 32/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 33/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Session Marked! Total Attended: 34/40
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 2
Student Name: Anjali
Roll Number: 722035
Total Sessions: 40
Attended Sessions: 34
Session Attendance Percentage: 85%
1. Mark Session Attendance
2. Show Attendance
3. Exit
Enter choice: 3
Process returned 0 (0x0) execution time : 267.737 s
Press any key to continue.
Choose Student Type:
1. Regular Student
2. Online Student
Enter choice: 1
Enter Student Name: Kirti
Enter Roll Number: 822045
Enter Total Classes: 20
Student Name: Kirti
Roll Number: 822045
Total Classes: 20
Attended Classes: 0
Attendance Percentage: 0%
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 1/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 2/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 3/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 4/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 5/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 6/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 7/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 8/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 9/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 10/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 11/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 12/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 13/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 14/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 15/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 16/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 17/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 18/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 19/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 1
Attendance Marked! Total Attended: 20/20
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 2
Student Name: Kirti
Roll Number: 822045
Total Classes: 20
Attended Classes: 20
Attendance Percentage: 100%
1. Mark Attendance
2. Show Attendance
3. Exit
Enter choice: 3
Process returned 0 (0x0) execution time : 105.801 s
Press any key to continue.
No comments:
Post a Comment