0% found this document useful (0 votes)
30 views20 pages

CSC-101L-Object Oriented Programming Assignment # 01

Uploaded by

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

CSC-101L-Object Oriented Programming Assignment # 01

Uploaded by

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

Object Oriented Programming Assignment 01

Namal University, Mianwali


Department of Computer Science
CSC-101L- Object Oriented Programming

Assignment # 01

Student’s Name Fatima Marayam

Roll No. NUM-BSCS-2023-27

Marks Obtained

Course Instructor:
Dr. Naureen Shaukat

Question 01:
1|Page
Object Oriented Programming Assignment 01

Code:
#include <iostream>
#include <string>
using namespace std;

// Structure to represent an address


struct Address {
int houseNumber;
string street;
string city;
};

// Structure to represent an employee


struct Employee {
string name;
int code;
Address address;
};

int main() {
// Array of 5 employees
Employee employees[5];

// Input information for 5 employees


for (int i = 0; i < 5; ++i) {
cout << "Enter details for Employee " << i + 1 << ":\n";

cout << "Name: ";


cin>>employees[i].name;

2|Page
Object Oriented Programming Assignment 01

cout << "Code: ";


cin >> employees[i].code;

cout << "House Number: ";


cin >> employees[i].address.houseNumber;

cout << "Street: ";


cin>>employees[i].address.street;

cout << "City: ";


cin>>employees[i].address.city;

cout << endl;


}

// Display information of 5 employees


cout << "Employee Details:\n";
for (int i = 0; i < 5; ++i) {
cout << "\nEmployee " << i + 1 << ":\n";
cout << "Name: " << employees[i].name << endl;
cout << "Code: " << employees[i].code << endl;
cout << "Address: "
<< employees[i].address.houseNumber << ", "
<< employees[i].address.street << ", "
<< employees[i].address.city << endl;
}

return 0;
}
Result Output:

3|Page
Object Oriented Programming Assignment 01

Explanation:

In this program i uses nested structures to manage and display information about employees
and their residential addresses. The Address structure stores house number, street, and city,
while the Employee structure includes name, code, and an embedded Address. It declares an
array of five employees to store details for multiple individuals. Using a loop, the program
collects user input for employee details and stores them in the array. Finally, it displays each
employee's name, code, and complete address in a clear, structured format.

4|Page
Object Oriented Programming Assignment 01

Question 02:
Code:
#include <iostream>
#include <string>
using namespace std;

class Book {
private:
string title;
string author;
string ISBN;
bool isAvailable; // true for available, false for issued

public:
// Constructor to initialize book details
Book(string t = "", string a = "", string i = "")
: title(t), author(a), ISBN(i), isAvailable(true) {}

// Function to enter book details


void enterDetails() {
cout << "Enter Title: ";
cin.ignore();
cin>>title;
cout << "Enter Author: ";
cin>>author;
cout << "Enter ISBN: ";
cin>>ISBN;
isAvailable = true; // New books are available by default
cout << "Book details entered successfully!\n";
}

// Function to display book details


void displayDetails() const {

5|Page
Object Oriented Programming Assignment 01

cout << "Title: " << title << endl;


cout << "Author: " << author << endl;
cout << "ISBN: " << ISBN << endl;
cout << "Availability: " << (isAvailable ? "Available" : "Issued") << endl;
}

// Static function to display details of all books


static void showAllBooks(Book books[], int size) {
for (int i = 0; i < size; ++i) {
cout << "\nBook " << i + 1 << " Details:\n";
books[i].displayDetails();
}
}

// Function to issue a book


void issueBook() {
if (isAvailable) {
isAvailable = false;
cout << "Book \"" << title << "\" issued successfully.\n";
} else {
cout << "Book \"" << title << "\" is already issued.\n";
}
}

// Function to return a book


void returnBook() {
if (!isAvailable) {
isAvailable = true;
cout << "Book \"" << title << "\" returned successfully.\n";
} else {
cout << "Book \"" << title << "\" was not issued.\n";
}
}

6|Page
Object Oriented Programming Assignment 01

};

int main() {
const int numBooks = 4;
Book library[numBooks];

// Initialize books with sample data


library[0] = Book("The Mystery Island", "F. Scott", "1234");
library[1] = Book("Freedom Mockingbird", "Harper Lee", "9870");
library[2] = Book("1984", "George Orwell", "123498765");
library[3] = Book("Pride and Prejudice", "Jane Austen", "567890123");

// Display initial details of all books


cout << "Initial Library Details:\n";
Book::showAllBooks(library, numBooks);

// Perform operations
cout << "\nIssuing the first book...\n";
library[0].issueBook();

cout << "\nTrying to issue the first book again...\n";


library[0].issueBook();

cout << "\nReturning the first book...\n";


library[0].returnBook();

cout << "\nTrying to return the first book again...\n";


library[0].returnBook();

// Display updated details of all books


cout << "\nUpdated Library Details:\n";
Book::showAllBooks(library, numBooks);

7|Page
Object Oriented Programming Assignment 01

// Allow entering details for a new book


cout << "\nAdding a new book to the library:\n";
Book newBook;
newBook.enterDetails();

// Display the new book's details


cout << "\nNew Book Details:\n";
newBook.displayDetails();

return 0;
}

Output:

8|Page
Object Oriented Programming Assignment 01

Question 03:
Code:
#include <iostream>
#include <string>
using namespace std;

class Book {

9|Page
Object Oriented Programming Assignment 01

private:
string title;
string author;
string ISBN;
int availabilityStatus; // 1 for available, 0 for issued
public:
// Constructor to initialize book details
Book(string t , string a , string i ) : title(t), author(a), ISBN(i), availabilityStatus(1)
{}
// Function to enter book details (not used in this version)
void enterDetails() {
cout << "Enter Title: ";
cin>>title;
cout << "Enter Author: ";
cin>>author;
cout << "Enter ISBN: ";
cin>>ISBN;
availabilityStatus = 1; // New books are available by default
}
// Function to display book details
void displayDetails() const {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "ISBN: " << ISBN << endl;
cout << "Availability: " << (availabilityStatus == 1 ? "Available" : "Issued") <<
endl;
}
// Function to issue a book
void issueBook() {
if (availabilityStatus == 1) {
availabilityStatus = 0;
cout << "Book issued successfully." << endl;
} else {

10 | P a g e
Object Oriented Programming Assignment 01

cout << "Book is already issued." << endl;


}
}
// Function to return a book
void returnBook() {
if (availabilityStatus == 0) {
availabilityStatus = 1;
cout << "Book returned successfully." << endl;
} else {
cout << "Book was not issued." << endl;
}
}
};
int main() {
const int numBooks = 4;
Book library[numBooks] = {
Book("Book1", "Author1", "ISBN1"),
Book("Book2", "Author2", "ISBN2"),
Book("Book3", "Author3", "ISBN3"),
Book("Book4", "Author4", "ISBN4")
};
// Display details of all books
for (int i = 0; i < numBooks; ++i) {
cout << "\nBook " << i + 1 << " Details:\n";
library[i].displayDetails();
}
// Perform operations on the books
cout << "\nIssuing the first book...\n";
library[2].issueBook();
cout << "\nTrying to issue the first book again...\n";
library[2].issueBook();
cout << "\nReturning the first book...\n";
library[2].returnBook();

11 | P a g e
Object Oriented Programming Assignment 01

// Display details of all books again


for (int i = 0; i < numBooks; ++i) {
cout << "\nBook " << i + 1 << " Details:\n";
library[i].displayDetails();
}
return 0;
}
Output:

Question 04:
Code:
#include <iostream>
12 | P a g e
Object Oriented Programming Assignment 01

using namespace std;

class Number {
private:
int value; // Data item to compare

public:
// Constructor
Number(int val = 0) : value(val) {}

// Getter for the private value


int getValue() const {
return value;
}

// Display the number


void display() const {
cout << "Value: " << value << endl;
}
};

// Overloading '<' operator


bool operator<(const Number& lhs, const Number& rhs) {
return lhs.getValue() < rhs.getValue();
}

// Overloading '>' operator


bool operator>(const Number& lhs, const Number& rhs) {
return lhs.getValue() > rhs.getValue();
}

// Overloading '==' operator


bool operator==(const Number& lhs, const Number& rhs) {

13 | P a g e
Object Oriented Programming Assignment 01

return lhs.getValue() == rhs.getValue();


}

int main() {
// Creating instances of the Number class
Number num1(20), num2(50), num3(30);

// Displaying the numbers


cout << "Numbers:" << endl;
cout << "Number 01 ";num1.display();
cout << "Number 02 ";num2.display();
cout << "Number 03 ";num3.display();
cout << endl;

// Comparing numbers using overloaded operators


if (num1 < num2) {
cout << "num1 is less than num2." << endl;
} else {
cout << "num1 is not less than num2." << endl;
}

if (num2 > num3) {


cout << "num2 is greater than num3." << endl;
} else {
cout << "num2 is not greater than num3." << endl;
}

if (num1 == num3) {
cout << "num1 is equal to num3." << endl;
} else {
cout << "num1 is not equal to num3." << endl;
}

14 | P a g e
Object Oriented Programming Assignment 01

return 0;
}

Output:

Question 05:
Code:
##include <iostream>
#include <string>
using namespace std;
// Base Class
class Faculty {
protected:
string name;
string department;
int employeeID;
public:
// Constructor
Faculty(string n, string d, int id) : name(n), department(d), employeeID(id) {}

15 | P a g e
Object Oriented Programming Assignment 01

// Method to display faculty information


virtual void displayInfo() {
cout << "Name: " << name << endl;
cout << "Department: " << department << endl;
cout << "Employee ID: " << employeeID << endl;
}
};
// Derived Class: Professor
class Professor : public Faculty {
private:
double salary; // Salary of the Professor
public:
Professor(string n, string d, int id, double sal) : Faculty(n, d, id), salary(sal) {}
void displayInfo() override {
cout << "Professor Information:" << endl;
Faculty::displayInfo();
cout << "Salary: $" << salary << endl;
}
};
// Derived Class: AssociateProfessor
class AssociateProfessor : public Faculty {
private:
int yearsOfExperience; // Years of experience
public:
AssociateProfessor(string n, string d, int id, int exp) : Faculty(n, d, id),
yearsOfExperience(exp) {}
void displayInfo() override {
cout << "Assistant Professor Information:" << endl;
Faculty::displayInfo();
cout << "Years of Experience: " << yearsOfExperience << " years" << endl;
}
};

16 | P a g e
Object Oriented Programming Assignment 01

// Derived Class: AssistantProfessor


class AssistantProfessor : public Faculty {
private:
int publications; // Number of publications
public:
AssistantProfessor(string n, string d, int id, int pub) : Faculty(n, d, id),
publications(pub) {}
void displayInfo() override {
cout << "Associate Professor Information:" << endl;
Faculty::displayInfo();
cout << "Publications: " << publications << endl;
}
};

// Derived Class: Lecturer


class Lecturer : public Faculty {
private:
int numberOfProjects; // Number of projects
public:
Lecturer(string n, string d, int id, int proj) : Faculty(n, d, id),
numberOfProjects(proj) {}
void displayInfo() override {
cout << "Lecturer Information:" << endl;
Faculty::displayInfo();
cout << "Number of Projects: " << numberOfProjects << endl;
}
};
// Derived Class: LabEngineer
class LabEngineer : public Faculty {
private:
string projectName; // Current project name
public:
LabEngineer(string n, string d, int id, string proj) : Faculty(n, d, id),

17 | P a g e
Object Oriented Programming Assignment 01

projectName(proj) {}
void displayInfo() override {
cout << "Lab Engineer Information:" << endl;
Faculty::displayInfo();
cout << "Current Project: " << projectName << endl;
}
};
int main() {
// Creating instances of different faculty members
Professor prof("Dr. Ryan", "Computer Science", 20202, 775000);
AssociateProfessor assocProf("Dr Ramzan Shahid", "Mathematics", 10342, 10);
AssistantProfessor assistProf("Ms Naureen ", "EE", 1003, 76);
Lecturer lec("MS.Faiqa ", "Mathematics", 1004, 3);
LabEngineer labEng("Dawood Khan", "CS", 1005, "Supercomputing");
// Displaying information of each faculty member
cout << endl;
prof.displayInfo();
cout << endl;
assocProf.displayInfo();
cout << endl;
assistProf.displayInfo();
cout << endl;
lec.displayInfo();
cout << endl;
labEng.displayInfo();
cout << endl;
return 0;
}

Output:

18 | P a g e
Object Oriented Programming Assignment 01

19 | P a g e
Object Oriented Programming Assignment 01

20 | P a g e

You might also like