You are on page 1of 7

Department of Computing

CS 212: Object Oriented Programming

Class: BEE-14D
Fall 2023

Lab08

Date: 20th November, 2023

Instructor: Mehreen Tahir


Lab Engineer: Mehwish Kiran

Name: Rehan Ahmad Iftikhar


CMSID: 405427

.
Lab Tasks:
Task 1:
NoteBook is the base class that holds
data fields named manufacturerID (of integer type) and manufacturerName (of string type)
A two-argument constructor to initialize data-fields with user-defined values
Appropriate accessor and mutator functions to set and get values of data fields
Derive two classes from the NoteBook class:
ENotebook, which contains an attribute size of integer type and a setter member function to set size
value
PaperNoteBook, which contains an instance variable named totalPages along with a member
function to set total number of pages in a certain paper notebook object
Both derived classes have function display to show all data field values (including values inherited
from the base class).
Also add appropriate destructor in both base and derived classes.
In the main() function, create objects of ENoteBook and PaperNoteBook classes and show the
advantages of using protected access specifier.
Display all data and see the behavior and order for constructors and destructors.

Code:
Notebook.h:
#pragma once
#include <string>

class NoteBook {
protected:
int manufacturerID;
std::string manufacturerName;

public:
NoteBook(int id, const std::string& name);

void setManufacturerID(int id);


int getManufacturerID() const;

void setManufacturerName(const std::string& name);


std::string getManufacturerName() const;

virtual void display() const;

virtual ~NoteBook();
};

Notebook.cpp:
#include "NoteBook.h"
#include <iostream>
using namespace std;

NoteBook::NoteBook(int id, const string& name) : manufacturerID(id), manufacturerName(name) {}

void NoteBook::setManufacturerID(int id) {


manufacturerID = id;
}

int NoteBook::getManufacturerID() const {


return manufacturerID;
}

void NoteBook::setManufacturerName(const string& name) {


manufacturerName = name;
}

string NoteBook::getManufacturerName() const {


return manufacturerName;
}

void NoteBook::display() const {


cout << "Manufacturer ID: " << manufacturerID << endl;
cout << "Manufacturer Name: " << manufacturerName << endl;
}

NoteBook::~NoteBook() {
cout << "Base class destructor called" << endl;
}

ENotebook.h:
#pragma once
#include "NoteBook.h"

class ENoteBook : public NoteBook {


private:
int size;

public:
ENoteBook(int id, const std::string& name, int notebookSize);

void setSize(int notebookSize);


int getSize() const;

void display() const override;

~ENoteBook();
};
ENotebook.cpp:
#include "ENoteBook.h"
#include <iostream>
using namespace std;

ENoteBook::ENoteBook(int id, const string& name, int notebookSize)


: NoteBook(id, name), size(notebookSize) {}

void ENoteBook::setSize(int notebookSize) {


size = notebookSize;
}

int ENoteBook::getSize() const {


return size;
}

void ENoteBook::display() const {


NoteBook::display();
cout << "Notebook Size: " << size << " inches" << endl;
}

ENoteBook::~ENoteBook() {
cout << "ENoteBook class destructor called" << endl;
}

PaperNotebook.h:
#pragma once
#include "NoteBook.h"

class PaperNoteBook : public NoteBook {


private:
int totalPages;

public:
PaperNoteBook(int id, const std::string& name, int pages);

void setTotalPages(int pages);


int getTotalPages() const;

void display() const override;

~PaperNoteBook();
};

PaperNotebook.cpp:
#include "PaperNoteBook.h"
#include <iostream>
using namespace std;
PaperNoteBook::PaperNoteBook(int id, const string& name, int pages)
: NoteBook(id, name), totalPages(pages) {}

void PaperNoteBook::setTotalPages(int pages) {


totalPages = pages;
}

int PaperNoteBook::getTotalPages() const {


return totalPages;
}

void PaperNoteBook::display() const {


NoteBook::display();
cout << "Total Pages: " << totalPages << endl;
}

PaperNoteBook::~PaperNoteBook() {
cout << "PaperNoteBook class destructor called" << endl;
}

Test.cpp:
#include "PaperNoteBook.h"
#include <iostream>
using namespace std;

PaperNoteBook::PaperNoteBook(int id, const string& name, int pages)


: NoteBook(id, name), totalPages(pages) {}

void PaperNoteBook::setTotalPages(int pages) {


totalPages = pages;
}

int PaperNoteBook::getTotalPages() const {


return totalPages;
}

void PaperNoteBook::display() const {


NoteBook::display();
cout << "Total Pages: " << totalPages << endl;
}

PaperNoteBook::~PaperNoteBook() {
cout << "PaperNoteBook class destructor called" << endl;
}
Output:

Task 2:
a. Provide a meaningful implementation for class course and section.
b. Write a main program that declares an array of 7 objects of type section and set their values to:
courseNumber: 117 for all sections
creditHours: 3 for all sections
sectionNumber: give each section a unique number from 1-7

Code:
Section.h:
#pragma once

#include "Course.h"

class Section {
private:
int secNumber;
Course c; // Composition

public:
Section();
Section(int secNum, const Course& course);

int getSectionNumber() const;


Course getCourse() const;
};

Section.cpp:
#include "Section.h"

Section::Section() : secNumber(0), c(0, 0) {}


Section::Section(int secNum, const Course& course) : secNumber(secNum), c(course) {}

int Section::getSectionNumber() const {


return secNumber;
}

Course Section::getCourse() const {


return c;
}

Test.cpp:
#include <iostream>
#include "Section.h"
using namespace std;

int main() {
Section sections[7];

for (int i = 0; i < 7; ++i) {


sections[i] = Section(i + 1, Course(117, 3));
}

for (const auto& section : sections) {


cout << "Section " << section.getSectionNumber() << " - Course Number: "
<< section.getCourse().getCourseNumber() << ", Credit Hours: "
<< section.getCourse().getCreditHours() << endl;
}

return 0;
}

Output:

You might also like