OOP Lab 06 Report
OOP Lab 06 Report
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
LAB REPORT – 06
Total 10
1
LAB # 06: Composition
Lab Objective:
The objective of this lab is to introduce students to the concept of Composition to understand
how to design classes that consist of other classes as their member objects, how to create a class
composed of other classes.
Tool Used: Visual Studio Code
Lab Tasks:
1. 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.
SOURCE CODE:
#include <iostream>
#include <string>
using namespace std;
// Author Class
class Author {
private:
string name;
string nationality;
public:
// Default Constructor
Author() : name("Unknown"), nationality("Unknown") {}
// Parameterized Constructor
Author(string name, string nationality) : name(name),
nationality(nationality) {}
// Copy Constructor
Author(const Author& other) {
name = other.name;
nationality = other.nationality;
}
// Destructor
~Author() {
// Setters
void setName(string name) { this->name = name; }
2
void setNationality(string nationality) { this->nationality =
nationality; }
// Getters
string getName() const { return name; }
string getNationality() const { return nationality; }
public:
// Default Constructor
Book() : title("Unknown"), year(0), author(Author()) {}
// Parameterized Constructor
Book(string title, int year, Author author) : title(title), year(year),
author(author) {}
// Copy Constructor
Book(const Book& other) {
title = other.title;
year = other.year;
author = other.author; // Author copy constructor will be called
}
// Destructor
~Book() {
// Setters
void setTitle(string title) { this->title = title; }
void setYear(int year) { this->year = year; }
void setAuthor(Author author) { this->author = author; }
// Getters
string getTitle() const { return title; }
int getYear() const { return year; }
Author getAuthor() const { return author; }
3
// Main Function to test the program
int main() {
// Create Author objects
Author author1("Khaled Hosseini", "Afghan");
Author author2("George Orwell", "British");
return 0;
}
OUTPUT:
2. Create a class LINE include appropriate data members. A line segment includes the
endpoints, i.e. the points that it joins.
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.
4
SOURCE CODE:
#include<iostream>
#include<cmath> // For sqrt and pow
using namespace std;
class Point
{
float x, y;
public:
// Parameterized constructor for Point with cout statement
Point(float x = 0.0, float y = 0.0)
{
this->x = x;
this->y = y;
cout << "Point constructor called" << endl; // Cout for constructor
}
void moveUp()
{
this->y++;
}
void moveDown()
{
this->y--;
}
void moveLeft()
{
this->x--;
}
void moveRight()
{
this->x++;
}
5
{
return this->y;
}
class Line
{
private:
Point point1;
Point point2;
public:
// Default constructor for Line
Line() : point1(0.0, 0.0), point2(0.0, 0.0)
{
cout << "Line default constructor called" << endl; // Cout for
default constructor
}
6
return point1.lengthFromOrigin();
}
int main()
{
// Using the default constructor
Line defaultLine;
// Outputting the lengths of points from origin for the parameterized line
cout << "Length of point1 from origin: " <<
paramLine.lengthFromOriginPoint1() << endl;
cout << "Length of point2 from origin: " <<
paramLine.lengthFromOriginPoint2() << endl;
return 0;
}
7
3. 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 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.
SOURCE CODE:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
private:
string name;
string studentID;
public:
// Constructors to initialize student details
Student() : name("Unknown"), studentID("Unknown") {}
Student(string name, string studentID) : name(name), studentID(studentID)
{}
class Department {
private:
string departmentName;
vector<Student> students; // List of students in the department
public:
// Constructors to initialize department name
Department() : departmentName("Unknown") {}
Department(string departmentName) : departmentName(departmentName) {}
8
// Add a student to the department
void addStudent(const Student& student) {
students.push_back(student);
}
class University {
private:
string universityName;
vector<Department> departments; // List of departments in the university
public:
// Constructors to initialize university name
University() : universityName("Unknown University") {}
University(string universityName) : universityName(universityName) {}
int main() {
// Create a university
University uni("Tech University");
// Create departments
Department csDept("Computer Science");
Department eeDept("Electrical Engineering");
9
csDept.addStudent(student2);
eeDept.addStudent(student3);
eeDept.addStudent(student4);
return 0;
}
10