0% found this document useful (0 votes)
24 views15 pages

Anus Babar (231499) OOP Assigment # 01

Uploaded by

231499
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)
24 views15 pages

Anus Babar (231499) OOP Assigment # 01

Uploaded by

231499
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/ 15

Name: Anus Babar

Roll No: 231499


Section: 3-A BEEE
Assignment No: 1
Subject: OOP
Submitted To: Ma’am Saba Mumtaz
Date: 06 - Oct - 2024
Question 1: Defining classes

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:

• string name: Stores the student's name.


• int roll Number: Stores the student's roll number.
• char grade: Stores the student's grade.

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:

• set Student Details (): This


is a setter method that takes three parameters: a string for the
student's name, an integer for the student's roll number, and a character for the student's
grade. It assigns the values passed to this method to the private attributes (name, roll
Number, and grade).
• const string& student Name: The const ensures that the input string isn't modified, and the &
ensures that the string is passed by reference (to avoid unnecessary copying).
• Display Student Details () const: This is a getter method that prints the details of the student to
the console. The const keyword indicates that this method will not modify any attributes
of the class.

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'.

• Displaying Student Details:

• 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:

First Student (student1):

• 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.

Second Student (student2):

• 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:

• title: Holds the title of the book (type: string).


• author: Holds the author's name (type: string).
• price: Stores the price of the book (type: float).
• Year Published: Stores the year the book was published (type: int).

• 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:

• title = "unknown": Sets the title to "unknown" by default.


• author = "unknown": Sets the author to " unknown" by default.
• price = 0.0: Sets the price to 0.0 by default.
• Year Published = 0: Sets the year published to 0 by default

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:

• string t: A string for the title.


• string a: A string for the author.
• float p: A float for the price.
• int y: An integer for the year published.

• Initialization:

• The constructor assigns the provided values to the class attributes:


o title = t: The title is set to the value passed as t.
o author = a: The author is set to the value passed as a.
o price = p: The price is set to the value passed as p.
o year Published = y: The year published is set to the value passed as y.

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:

• get Title () const: Returns the title attribute.


• Get Author () const: Returns the author attribute.
• Get Price () const: Returns the price attribute.
• Get Year Published () const: Returns the year Published attribute.
Display Method:

• 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.

When you call book1.displayDetails(), it will output the details of book1

Main Function:

• Purpose: The main () function is where the program starts. It contains the logic for creating and
testing the Book objects.

• First Book (Default Constructor):

• 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.

• Second Book (Parameterized Constructor):

• 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:

• Title: unknown: Prints the value of title, which is "unknown".


• Author: unknown: Prints the value of author, which is "unknown".
• Price: 0.0: Prints the value of price, formatted to two decimal places (0.0).
• Year Published: 0: Prints the value of year Published, which is 0.
• Title: Gumman: Prints the value of title, which is "Gumman".
• Author: John Elia: Prints the value of author, which is "John Elia".
• Price: 1000.0: Prints the value of price, formatted to two decimal places (1000.0).
• Year Published: 2004: Prints the value of year Published, which is 2004.

Question 3: Design a Library Management System that tracks books and members, allowing

members to borrow and return books.


Including Header Files:

• The program begins by including the necessary header files:


• iostream: This library allows input/output operations such as cout and cin.
• string: This library is used for handling strings in C++.

2. Book Class:

The Book class represents a book in the library system. It contains:

• Private Attributes:

o title: A string to store the name of the book.


o Borrower ID: An int to track if the book is borrowed and by which member. If it's set to -1, the
book is not borrowed.
• Public Methods:

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.

Borrow Book (int member ID):

• This function checks if the book is already borrowed.


• If borrower ID == -1 (not borrowed), it sets borrower ID to the member's ID and prints the
borrowing message.
• If the book is already borrowed, it prints an error message.
• It uses is Borrowed () to check and print the borrowing status.

Return Book ():


• This function checks if the book is currently borrowed.
• If the book is borrowed, it resets borrower ID to -1 (indicating the book is returned) and
prints the return message.
• If the book is not borrowed, it prints an error message.

Is Borrowed ():

• This method simply checks if the book is borrowed by returning whether borrower ID is not
equal to -1.

3. Member Class:

The Member class represents a member in the library. It contains:

• Private Attributes:

o Member ID: An int to store the unique ID of the member.


o name: A string to store the name of the member.

• 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:

• The constructor initializes the member's ID and name.


• It also increments the static variable total Members every time a member is created.

Get ID ():

• This method returns the member's ID.

Display Member ():

• This method displays the member's name and 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:

o Member "Anus" returns the book "Gumman".


o The system prints the return message and resets the borrowerID.

Output:
• Total Books: 2:

• Two books ("Gumman" and "Shayad") are added to the system.

• 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy