Anus Babar (231499) OOP Assigment # 01
Anus Babar (231499) OOP Assigment # 01
Define a class Student with private attributes: name, roll Number, and grade. Provide
public methods to set and get these values. Write a main () function to create two
Student objects, set their values, and display the information of both students.
Explanation:
• #include <iostream>: This library is included to use the input/output stream, particularly
cout for printing to the console.
• #include <string>: This library is included to use the string class for handling text.
• Private Members:
These attributes are private, meaning they cannot be accessed directly outside the class. This
encapsulates the data and ensures it's only modified or accessed through public methods.
• Public Methods:
Purpose: This is a helper function outside the Student class. It is used to create a Student
object, set its details, and return the object.
• The function takes three parameters: the student's name, roll number, and grade. It uses
these parameters to call the set Student Details () method of the Student object.
• The function creates a local Student object, calls the set Student Details () method on it, and
then returns the object to the caller.
Creating Students:
• Two Student objects are created using the create Student () function: student1 and student2.
• Student student1 = create Student ("Anus", 333, 'A’; This calls the create Student () function, passing
"Anus" as the name, 333 as the roll number, and 'A' as the grade. The function creates a
Student object, sets the details, and returns it, which is then assigned to student1.
• Student student2 = create student ("babar", 334, 'B'); Similarly, this creates a Student object for
"babar" with a roll number of 334 and a grade of 'B'.
• student1.display Student Details (); This calls the display Student Details () method for student1,
which prints the details ("Anus", roll number 333, grade 'A').
• student2.display Student Details (); This calls the display Student Details () method for student2,
which prints the details ("babar", roll number 334, grade 'B').
• Return Statement:
• return 0: The main () function returns 0, indicating that the program has executed
successfully.
Output:
• The create Student ("Anus", 333, 'A') function creates a Student object with the name "Anus",
roll number 333, and grade 'A'.
• When student1.display Student Details () is called, the information is printed as shown in the
first output block.
• The create Student ("babar", 334, 'B') function creates a Student object with the name "babar",
roll number 334, and grade 'B'.
• When student2.display Student Details () is called, the information is printed as shown in the
second output block.
Question no 2. Implementing Classes with Constructors in C++
Explanation:
• Purpose: We need the iostream library to handle input/output operations (such as cout
for printing text to the console).
We include the string library to handle the string data type, which will store the title and author
of the books.
• Purpose: We define a class Book to represent a book with four private attributes:
• Private: These attributes are marked as private to enforce encapsulation. This means they
cannot be accessed or modified directly from outside the class. Instead, we'll use methods
(getters and setters) to access and modify these values.
• Purpose: This is the default constructor, a special method used to create an object of the Book
class without passing any values. When you create a Book object without arguments (e.g., Book
book1;), this constructor will be called.
• Initialization:
Parameterized Constructor:
• Purpose: This is a parameterized constructor that allows you to initialize a Book object with
specific values for the attributes when the object is created. When you create a Book object with
arguments (e.g., Book book2("Gumman", "John Elia",1000.0, 2004) ;), this constructor will be called.
• Parameters:
• Initialization:
Set Methods:
• Purpose: Setters are used to update the value of private attributes. Each setter method
allows you to modify a specific attribute of the Book object after it has been created.
• Details:
o set Title (string t): Updates the title attribute to the value passed in t.
o set Author (string a): Updates the author attribute to the value passed in a.
o set Price (float p): Updates the price attribute to the value passed in p.
o set Year Published (int y): Updates the year Published attribute to the value passed in y.
For example, after creating a Book object, you can update the title by calling book1.setTitle("New
Title”;
Get Methods:
• Purpose: Getters are used to retrieve the value of private attributes. Each getter method
allows you to access a specific attribute of the Book object without directly accessing the private
member.
•Details:
• Purpose: This method prints the book's details in a formatted way. It is a public method
that can be called to display the values of the Book object’s attributes.
• Output:
o It prints:
▪ The title of the book.
▪ The author of the book.
▪ The price of the book.
▪ The year the book was published.
Main Function:
• Purpose: The main () function is where the program starts. It contains the logic for creating and
testing the Book objects.
• Book book1; Creates a Book object called book1 using the default constructor.
• book1.displayDetails (); Calls the display Details () method to print the details of book1. Since
the default constructor was used, the details printed will be the default values:
"unknown", 0.0, and 0.
• Book book2("Gumman, "John Elia",1000.0, 2004);: Creates a Book object called book2 using the
parameterized constructor. The details provided are passed to the constructor and
stored in the object.
• book2.displayDetails (); Calls the display Details () method to print the details of book2, which
will show the title, author, price, and year published provided to the constructor.
• Return 0: The main () function returns 0, indicating that the program executed successfully.
Output:
Question 3: Design a Library Management System that tracks books and members, allowing
2. Book Class:
• Private Attributes:
o static int totalBooks: This static variable keeps track of the total number of books in the system.
o Book (const string& t): A constructor that initializes the book with a title and sets borrowerID to -1
(not borrowed).
o Borrow Book (int memberID): A method that allows a member to borrow the book.
o Return Book (): A method that allows a member to return the book.
o Is Borrowed () const: A method that returns true if the book is borrowed, false otherwise.
Constructor:
• The constructor is used to initialize the book's title and set borrowerID to -1 (indicating
the book is not borrowed).
• It also increments the static variable totalBooks every time a book is created.
Is Borrowed ():
• This method simply checks if the book is borrowed by returning whether borrower ID is not
equal to -1.
3. Member Class:
• Private Attributes:
• Public Methods:
o static int total Members: This static variable keeps track of the total number of
members in the system.
o Member (int id, const string& n): A constructor to initialize the member with an ID and
name.
o Get ID () const: A method to get the member's ID.
o Display Member () const: A method to display the member's details.
Constructor:
Get ID ():
4. Static Variables:
int Book::totalBooks = 0;
int Member::totalMembers = 0;
• These static variables are initialized to 0. They track the total number of books and
members.
5. Main Function:
Step-by-Step Breakdown:
1. Creating Books:
o Two Book objects are created: "Gumman" and "Shayad". This increments the
totalBooks static variable to 2.
2. Creating Members:
o Three Member objects are created: "Anus", "Ali", and "Babar". This increments
the totalMembers static variable to 3.
3. Displaying Total Books and Members:
o The total number of books and members is printed
4. Borrowing a Book:
o Member "Anus" borrows the book "Gumman".
o The system prints the borrowing message and updates the borrower ID.
5. Returning a Book:
Output:
• Total Books: 2:
• Total Members: 3:
• Three members ("Anus", "Ali", and "Babar") are registered in the system.
• Borrowing a Book:
• Member "Anus" borrows the book "Gumman". The system confirms the book is now
borrowed.
• Returning a Book:
• Member "Anus" returns the book "Gumman". The system confirms the book is no longer
borrowed.
Conclusion:
The programs demonstrate the use of classes in C++ for modeling real-world entities. The
Student class manages students' details with encapsulation. The Book class represents books in a library
with attributes like title, author, price, and year of publication, using both default and parameterized
constructors. The Library Management System extends this by tracking members, borrowing, and
returning books, while using static variables to maintain total counts. Each program highlights object-
oriented concepts like constructors, encapsulation, and static variables for efficient system
management.