0% found this document useful (0 votes)
1 views6 pages

OOP-Weekend Lecture 06

The document describes three tasks involving static data members in C++. Task 1 involves creating an Employee class that assigns unique IDs to employees, Task 2 focuses on a Book class that tracks borrowed books, and Task 3 outlines a Student class that counts registered students. Each task includes specific requirements for class structure and methods to manage static variables effectively.

Uploaded by

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

OOP-Weekend Lecture 06

The document describes three tasks involving static data members in C++. Task 1 involves creating an Employee class that assigns unique IDs to employees, Task 2 focuses on a Book class that tracks borrowed books, and Task 3 outlines a Student class that counts registered students. Each task includes specific requirements for class structure and methods to manage static variables effectively.

Uploaded by

ayesha.widemedia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Static Data Member

Static data member is a member variable of a class that is associated with class not
objects. Suppose if you create 3 objects of a class there will be only 1 copy of static data
member created.

Task 1

In a company, each employee needs a unique ID number that increments every time a
new employee is created. Write a class Employee that automatically assigns an ID to
each employee starting from 1001 and increments the ID with each new employee.
Display the current employee's ID and the next available ID.

Tasks to Do:

1. Create a static member variable nextId to hold the next available ID (starting from
1001).
2. Create a constructor that assigns the current nextId value to each Employee object
and increments nextId.
3. Add a non-static member function showId() to display the employee's ID.
4. Add a static member function showNextId() to display the next available ID.
5. In main(), create at least 3 employees and display their IDs, as well as the next
available ID.

#include <iostream>

using namespace std;

class Employee {

int empId;

static int nextId;

public:

Employee() {

empId = nextId++;
}

void showId() {

cout << "Employee ID: " << empId << endl;

static void showNextId() {

cout << "Next available ID: " << nextId << endl;

};

int Employee::nextId = 1001;

int main() {

Employee e1, e2, e3;

e1.showId();

e2.showId();

e3.showId();

Employee::showNextId();

return 0;

}
Task 2

Create a class Book that tracks how many books have been borrowed from a library.
Each time a book is borrowed, increment a static counter and display the total number of
borrowed books.

🔧 Tasks to Do:

1. Create a static variable borrowedCount to track the total number of borrowed


books.
2. Create a constructor that accepts the book title and initializes the book object.
3. Implement a method borrowBook() that increments borrowedCount and displays
which book was borrowed.
4. Add a static function showBorrowedCount() to display the total number of books
borrowed.
5. In main(), create two Book objects, borrow them, and then show the total
borrowed books.

#include <iostream>
using namespace std;

class Book {
string title;
static int borrowedCount;

public:
Book(string t) : title(t) {}

void borrowBook() {
borrowedCount++;
cout << title << " borrowed." << endl;
}
static void showBorrowedCount() {
cout << "Total books borrowed: " << borrowedCount << endl;
}
};

int Book::borrowedCount = 0;

int main() {
Book b1("1984");
Book b2("Brave New World");

b1.borrowBook();
b2.borrowBook();

Book::showBorrowedCount();
return 0;
}
Task 3

Design a system to track the total number of students that have registered in a university.
Each time a student object is created, the counter should increment. Display the total
number of students registered.

Tasks to Do:

1. Define a static variable registrationCount in the Student class to track the total
number of students.
2. Increment registrationCount in the constructor whenever a new student is created.
3. Implement a static method showRegistrations() to display the total number of
students.
4. In main(), create at least 3 student objects and call showRegistrations() to display
the total count.

#include <iostream>

using namespace std;

class Student {

static int registrationCount;

public:

Student() {

registrationCount++;

static void showRegistrations() {

cout << "Total registrations: " << registrationCount << endl;


}

};

int Student::registrationCount = 0;

int main() {

Student s1, s2, s3;

Student::showRegistrations();

return 0;

You might also like