[go: up one dir, main page]

0% found this document useful (0 votes)
20 views10 pages

OOP Lab 06 Report

Uploaded by

Fabeha
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)
20 views10 pages

OOP Lab 06 Report

Uploaded by

Fabeha
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/ 10

DEPARTMENT OF COMPUTER & SOFTWARE

ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI

EC-201 Object Oriented Programming

LAB REPORT – 06

Course Instructor: Anum Abdul Salam

Lab Instructor: Engr. Eman Fatima

Student Name: Fabeha Zahid Mahmood

Degree/ Syndicate: 45/B

Trait Obtained Maximum


Marks Marks
R1 Application Functionality 3
and Specification
30%
R2 Readability 1
10%
R3 Reusability 1
10%
R4 Object Oriented Design 3
30%
R5 Efficiency 1
10%
R6 Delivery 1
10%
R7 Plagiarism below 70% 1

Total 10

Total Marks = O𝒃𝒕𝒂𝒊𝒏𝒆𝒅 𝑴𝒂𝒓𝒌𝒔 (∑6𝟏 𝑹𝒊 ∗ 𝑹7)

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; }

// Display Author details


void displayAuthorDetails() const {
cout << "Author: " << name << ", Nationality: " << nationality <<
endl;
}
};

// Book Class (Composition: Book "has an" Author)


class Book {
private:
string title;
int year;
Author author; // Composition: Author object

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; }

// Display Book details (along with Author details)


void displayBookDetails() const {
cout << "Book: " << title << ", Year: " << year << endl;
author.displayAuthorDetails(); // Display associated author details
}
};

3
// Main Function to test the program
int main() {
// Create Author objects
Author author1("Khaled Hosseini", "Afghan");
Author author2("George Orwell", "British");

// Create Book objects using parameterized constructor


Book book1("The Kite Runner", 2003, author1);
Book book2("1984", 1949, author2);

// Display details of the books and their respective authors


cout << "Book 1 Details:" << endl;
book1.displayBookDetails();

cout << "\nBook 2 Details:" << endl;


book2.displayBookDetails();

// Create a copy of book1


Book book3 = book1;
cout << "\nBook 3 (Copy of Book 1) Details:" << endl;
book3.displayBookDetails();

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
}

// Destructor with cout statement


~Point()
{
cout << "Point destructor called" << endl; // Cout for destructor
}

void moveUp()
{
this->y++;
}
void moveDown()
{
this->y--;
}
void moveLeft()
{
this->x--;
}
void moveRight()
{
this->x++;
}

void setX(float xloc)


{
this->x = xloc;
}

void setY(float yloc)


{
this->y = yloc;
}

float getX() const


{
return this->x;
}

float getY() const

5
{
return this->y;
}

// Function to calculate distance between two points


float calcdistance(const Point& other)
{
float dx = x - other.getX();
float dy = y - other.getY();
return sqrt(pow(dx, 2) + pow(dy, 2));
}

// Function to calculate distance from the origin (0, 0)


float lengthFromOrigin()
{
return sqrt(pow(x, 2) + pow(y, 2));
}
};

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
}

// Parameterized constructor for Line


Line(Point p1, Point p2) : point1(p1), point2(p2)
{
cout << "Line parameterized constructor called" << endl; // Cout for
parameterized constructor
}

// Destructor with cout statement


~Line()
{
cout << "Line destructor called" << endl; // Cout for destructor
}

// Getters for points


Point getpoint1() const {
return point1;
}

Point getpoint2() const {


return point2;
}

// Function to calculate the length of point1 from the origin


float lengthFromOriginPoint1()
{

6
return point1.lengthFromOrigin();
}

// Function to calculate the length of point2 from the origin


float lengthFromOriginPoint2()
{
return point2.lengthFromOrigin();
}
};

int main()
{
// Using the default constructor
Line defaultLine;

// Creating two Point objects


Point p1(3, 4);
Point p2(6, 8);

// Creating a Line object using the parameterized constructor


Line paramLine(p1, p2);

// 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)
{}

// Setters for student attributes


void setName(string name) { this->name = name; }
void setStudentID(string studentID) { this->studentID = studentID; }

// Getters for student attributes


string getName() const { return name; }
string getStudentID() const { return studentID; }

// Display student details


void displayStudentDetails() const {
cout << "Student Name: " << name << ", ID: " << studentID << endl;
}
};

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) {}

// Setter for department name


void setDepartmentName(string name) { this->departmentName = name; }

8
// Add a student to the department
void addStudent(const Student& student) {
students.push_back(student);
}

// Display department details along with its students


void displayDepartmentDetails() const {
cout << "Department: " << departmentName << endl;
for (const auto& student : students) {
student.displayStudentDetails();
}
}
};

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) {}

// Setter for university name


void setUniversityName(string name) { this->universityName = name; }

// Add a department to the university


void addDepartment(const Department& department) {
departments.push_back(department);
}

// Display university details along with its departments


void displayUniversityDetails() const {
cout << "University: " << universityName << endl;
for (const auto& department : departments) {
department.displayDepartmentDetails();
cout << endl;
}
}
};

int main() {
// Create a university
University uni("Tech University");

// Create departments
Department csDept("Computer Science");
Department eeDept("Electrical Engineering");

// Create students and assign them to departments


Student student1("Saad", "CS101");
Student student2("Talha", "CS102");
Student student3("Momina", "EE101");
Student student4("Aliza", "EE102");

// Add students to departments


csDept.addStudent(student1);

9
csDept.addStudent(student2);
eeDept.addStudent(student3);
eeDept.addStudent(student4);

// Add departments to the university


uni.addDepartment(csDept);
uni.addDepartment(eeDept);

// Display the entire university's details


uni.displayUniversityDetails();

return 0;
}

10

You might also like