220: Construct a class stack with member functions isEmpty, isFull, push, pop. Then make a class Human with data members name, father name and age. Then derive a class from it Student with data members rollNumber, department. Make a static count variable to check the total number of students anytime without calling object. Overload ++ operator that will increase the age of student by one. Your stack class should hold the record of every student.(array in stack class will be of student type)
// Construct a class stack with member functions isEmpty, isFull, push, pop. Then make a class Human with data members name, father name
// and age. Then derive a class from it Student with data members rollNumber, department. Make a static count variable to check the
// total number of students anytime without calling object. Overload ++ operator that will increase the age of student by one.
// Your stack class should hold the record of every student.(array in stack class will be of student type)
#include <iostream>
using namespace std;
const int stackSize = 10;
class Human
{
private:
string name, fatherName;
int age;
public:
Human(string n = "", string f = "", int a = 0) { name = n, fatherName = f, age = a; }
void setName(string n) { name = n; }
void setFather(string f) { fatherName = f; }
void setAge(int a) { age = a; }
string getName() { return name; }
string getFather() { return fatherName; }
int getAge() { return age; }
void display() { cout << "Name: " << name << "\nFather name: " << fatherName << "\nAge: " << age << endl; }
};
class Student : public Human
{
private:
int rollNumber;
string department;
static int count;
public:
Student() : Human() { rollNumber = 0, department = ""; }
Student(string n, string f, int a, int r, string d) : Human(n, f, a) { rollNumber = r, department = d, count++; }
void setRoll(int r) { rollNumber = r; }
void setDepartment(string d) { department = d; }
int getRoll() { return rollNumber; }
string getDepartment() { return department; }
void display()
{
Human::display();
cout << "Roll Number: " << rollNumber << "\nDepartment: " << department << endl;
}
void operator++(int)
{
int temp = getAge();
temp++;
setAge(temp);
}
static int displayCount() { return count; }
};
int Student::count = 0;
class Stack
{
private:
Student arr[stackSize];
int pointer;
public:
Stack() { pointer = -1; }
bool isEmpty(int x)
{
if (x == -1)
{
return true;
}
else
{
return false;
}
}
bool isFull(int x)
{
if (x == 9)
{
return true;
}
else
{
return false;
}
}
void push(Student x)
{
if (!(isFull(pointer)))
{
arr[++pointer] = x;
}
else
{
cout << "Error! Stack is full\n";
return;
}
}
Student pop()
{
if (!(isEmpty(pointer)))
{
return arr[pointer--];
}
else
{
cout << "Error! Stack is Empty\n";
return Student();
}
}
};
int main()
{
Stack stack1;
stack1.pop(); // Just for the test if it gives error or not
cout << endl;
Student s1("Mohib", "Muhammad Ali", 20, 003, "BSCS");
s1.display();
cout << endl;
Student s2("Abdullah", "Nadeem Aziz", 20, 003, "BSCS");
s2++; // increasing age by 1
s2++;
s2.display();
cout << endl;
// pushing s1 and s2 students in stack;
stack1.push(s1);
stack1.push(s2);
// popping students from stack1
Student s3 = stack1.pop();
cout << "Popped for the first time:\n";
s3.display();
cout << endl;
cout << "Popped for the second time:\n";
Student s4 = stack1.pop();
s4.display();
cout << endl;
// printing total number of students (not contains student that are popped from stack)
cout << "/////////////////////////////////////////\n";
cout << "////// Total number of students: " << Student::displayCount() << " /////" << endl;
cout << "///////////////////////////////////////// \n";
return 0;
}
Comments
Post a Comment