LAB REP 6
LAB REP 6
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
OOP
Lab Number 6
o SUBMITTED BY:
M.RAHIM JAMIL
M RAHIM JAMIL
CE - 45 SYN B
o SUBMITTED TO:
LE EMAN FATIMA
o Related Topic/Chapter in theory class:
COMPOSITION.
o Hardware/Software required:
Hardware: PC
Software Tool: MICROSOFT VISUAL CODE 2022
T1:
Write a program that demonstrates the concept of
composition by creating two classes Author and Book.
The Author class should include attributes such as the
author's name and nationality. The Book class should
have attributes for the book's title, year of publication, and
an Author object. Implement member functions in both
classes to display the details of the book and its author.
Test your program by creating multiple book objects with
different authors and displaying the complete details of
each book along with its respective author. The test plan is
that the composition between the Book and Author
classes is properly demonstrated and displayed .
CODE:
#include <iostream>
#include <string>
M RAHIM JAMIL
CE - 45 SYN B
using namespace std;
class Author {
private:
string name;
string nationality;
public:
Author() : name("Unknown"), nationality("Unknown") {}
Author(string authorName, string authorNationality)
: name(authorName), nationality(authorNationality) {}
void displayAuthor() const {
cout << "Author Name: " << name << "\nNationality: " << n
ationality << endl;
}
};
class Book {
private:
string title;
int publicationYear;
Author author;
public:
Book() : title("Unknown"), publicationYear(0) {}
Book(string bookTitle, int year, Author bookAuthor)
: title(bookTitle), publicationYear(year), author(bookAuth
or) {}
void displayBook() const {
cout << "Book Title: " << title << "\nPublication Year: "
<< publicationYear << endl;
author.displayAuthor();
}
};
int main() {
Author author1("ALI", "PAKISTANI");
M RAHIM JAMIL
CE - 45 SYN B
Author author2("JOHN", "BRITISH");
Book book1("SCIENCE AND TECHNOLOGY", 2022, author1);
Book book2("LIFE LESSONS", 2015, author2);
cout << "Book 1 Details:\n";
book1.displayBook();
cout<<endl;
cout << "Book 2 Details:\n";
book2.displayBook();
return 0;
}
OUTPUT:
T2:
Create a class LINE include appropriate data members. A line
segment includes the endpoints, i.e. the
points that it joins.
M RAHIM JAMIL
CE - 45 SYN B
Class should also provide a function to find the length of
the Line.
Two lines can be compared to determine which line is
shorter and vice versa. Provide function to compare two
Lines.
Provide appropriate constructors for initialization and
destructor.
Make appropriate members constant.
CODE:
#include <iostream>
#include <cmath> // Include for sqrt function
using namespace std;
class Point {
public:
double x, y;
// Constructor to initialize point coordinates
Point(double xCoord = 0, double yCoord = 0)
: x(xCoord), y(yCoord) {} // Use initialization list
};
// Class representing a line
class Line {
private:
const Point start; // Start point of the line
const Point end; // End point of the line
// Calculate the distance between two points
double calculateDistance() const {
double deltaX = end.x - start.x;
double deltaY = end.y - start.y;
M RAHIM JAMIL
CE - 45 SYN B
return sqrt(deltaX * deltaX + deltaY * deltaY); // Pyt
hagoras theorem
}
public:
// Default constructor (line from (0, 0) to (0, 0))
Line() : start(Point()), end(Point()) {}
// Parameterized constructor to create a line from start to
end
Line(const Point& startPoint, const Point& endPoint)
: start(startPoint), end(endPoint) {}
// Function to get the length of the line
double getLineLength() const {
return calculateDistance(); // Call private function to
get length
}
// Compare the lengths of two lines
void compareLineLength(const Line& otherLine) const {
double thisLength = getLineLength();
double otherLength = otherLine.getLineLength();
// Output the comparison result
if (thisLength < otherLength) {
cout << "The current line is shorter than the compare
d line." << endl;
} else if (thisLength > otherLength) {
cout << "The current line is longer than the compared
line." << endl;
} else {
cout << "Both lines are of equal length." << endl;
}
}
};
int main() {
// Create two lines using specific points
Line line1(Point(5, 6), Point(3, 2));
Line line2(Point(0, 0), Point(9, 5));
M RAHIM JAMIL
CE - 45 SYN B
// Display the length of both lines
cout << "Length of the first line: " << line1.getLineLength()
<< endl;
cout << "Length of the second line: " << line2.getLineLength()
<< endl;
// Compare the two lines
line1.compareLineLength(line2);
return 0;
}
OUTPUT:
T3:
Write a program that demonstrates multi-level composition
by creating three classes Student, Department, and
University. The Student class should include attributes for
the student's name, student ID, and major. The
Department class should have an attribute for the
department name and an array of Student objects. The
University class should contain a name attribute and an
array of Department objects. Implement member functions
to add departments to the university and to add students to
each department. Test the composition by creating a
M RAHIM JAMIL
CE - 45 SYN B
university with multiple departments and adding students to
each department, then display the details of the university,
its departments, and the students within each department
CODE:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int studentID;
string major;
Student(const string& studentName, int id, const string& stude
ntMajor)
: name(studentName), studentID(id), major(studentMajor) {}
void showStudentInfo() const {
cout << "Student Name: " << name
<< ", Student ID: " << studentID
<< ", Student Major: " << major << endl;
}
};
class Department {
public:
string departmentName;
Student* students[10]{};
int totalStudents;
Department(const string& deptName)
: departmentName(deptName), totalStudents(0) {}
void enrollStudent(Student* student) {
if (totalStudents < 10) {
M RAHIM JAMIL
CE - 45 SYN B
students[totalStudents++] = student;
} else {
cout << "Cannot enroll more students in "
<< departmentName << " department." << endl;
}
}
void showDepartmentInfo() const {
cout << "Department: " << departmentName << endl;
for (int i = 0; i < totalStudents; i++) {
students[i]->showStudentInfo();
}
}
};
class University {
public:
string universityName;
Department* departments[10]{};
int totalDepartments;
University(const string& uniName)
: universityName(uniName), totalDepartments(0) {}
void addNewDepartment(Department* department) {
if (totalDepartments < 10) {
departments[totalDepartments++] = department;
} else {
cout << "Cannot add more departments to "
<< universityName << " university." << endl;
}
}
void showUniversityInfo() const {
cout << "University: " << universityName << endl;
for (int i = 0; i < totalDepartments; i++) {
departments[i]->showDepartmentInfo();
}
}
};
M RAHIM JAMIL
CE - 45 SYN B
int main() {
University university("NUST CEME");
Department csDepartment("Computer Science");
Department meDepartment("Mechanical Engineering");
university.addNewDepartment(&csDepartment);
university.addNewDepartment(&meDepartment);
Student student1("ALI", 123456, "Computer Science");
Student student2("OMAR", 654321, "Computer Science");
Student student3("SARA", 789123, "Mechanical Engineering");
csDepartment.enrollStudent(&student1);
csDepartment.enrollStudent(&student2);
meDepartment.enrollStudent(&student3);
university.showUniversityInfo();
return 0;
}
OUTPUT:
M RAHIM JAMIL
CE - 45 SYN B
M RAHIM JAMIL
CE - 45 SYN B