Professional Documents
Culture Documents
Rehan Ahmad Iftikhar - 405427 - Lab08
Rehan Ahmad Iftikhar - 405427 - Lab08
Class: BEE-14D
Fall 2023
Lab08
.
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);
virtual ~NoteBook();
};
Notebook.cpp:
#include "NoteBook.h"
#include <iostream>
using namespace std;
NoteBook::~NoteBook() {
cout << "Base class destructor called" << endl;
}
ENotebook.h:
#pragma once
#include "NoteBook.h"
public:
ENoteBook(int id, const std::string& name, int notebookSize);
~ENoteBook();
};
ENotebook.cpp:
#include "ENoteBook.h"
#include <iostream>
using namespace std;
ENoteBook::~ENoteBook() {
cout << "ENoteBook class destructor called" << endl;
}
PaperNotebook.h:
#pragma once
#include "NoteBook.h"
public:
PaperNoteBook(int id, const std::string& name, int pages);
~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) {}
PaperNoteBook::~PaperNoteBook() {
cout << "PaperNoteBook class destructor called" << endl;
}
Test.cpp:
#include "PaperNoteBook.h"
#include <iostream>
using namespace std;
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);
Section.cpp:
#include "Section.h"
Test.cpp:
#include <iostream>
#include "Section.h"
using namespace std;
int main() {
Section sections[7];
return 0;
}
Output: