Lab Report 5
Lab Report 5
LAB REPORT - 05
Course Instructor: Lecturer Anum Abdul Salam
Degree/ Syndicate: 45 / B
Total 10
Total Marks = O𝒃𝒕𝒂𝒊𝒏𝒆𝒅 𝑴𝒂𝒓𝒌𝒔 (∑6𝟏 𝑹𝒊 ∗ 𝑹7)
Lab Objective:
The main purpose of this lab is to understand the principles of separating
header and implementation files in object-oriented programming to enhance
code organization, reusability and maintainability. Additionally, the lab aims
to explore the concept of dynamic memory allocation, allowing for efficient
memory management during runtime, particularly when dealing with data
structures of variable size.
LAB TASKS:
Task No. 01
Imagine you're building digital circuit simulation software that can model
various logical operations. As part of this project, you need to create a class
representing an N-input AND gate. This gate will serve as a fundamental
building block in your simulation, allowing users to configure multiple inputs
and see how the AND logic behaves based on those inputs.
The N-input AND gate outputs true only when all its inputs are true. If any
input is false, the output should be false. This exercise will not only test your
understanding of object-oriented programming principles but will also allow
you to practice memory management and logical operations in C++.
#include <iostream>
using namespace std;
class NInputANDGate {
private:
bool* inputs; // Dynamic array to hold the input values
int inputCount; // Number of inputs
public:
// Constructor
NInputANDGate(int n) {
inputCount = n;
inputs = new bool[inputCount]; // Allocate memory for
inputs
for (int i = 0; i < inputCount; i++) {
inputs[i] = false; // Initialize all inputs to false
}
}
// Destructor
~NInputANDGate() {
delete[] inputs; // Clean up dynamically allocated memory
}
int main() {
int n;
cout << "Enter the number of inputs for the AND gate: ";
cin >> n;
// Display the inputs and the result for the first AND gate
cout << "First AND Gate:" << endl;
andGate1.display();
// Display the inputs and the result for the second AND gate
cout << "Copied AND Gate:" << endl;
andGate2.display();
return 0;
}
Output:
In this exercise, you will explore the representation of polynomials and their
coefficients. A polynomial is an expression consisting of variables (often
represented as x) raised to non-negative integer powers, combined with
coefficients, which can be real or complex numbers.
Source Code:
#include<iostream>
#include<cmath> // For pow() function
using namespace std;
class polynomial {
private:
int degree;
float* coefficients;
public:
//Constructor
polynomial(int deg) {
degree = deg;
coefficients = new float[degree + 1];
}
////////////Setters///////////
void set_degree(int d) {
degree = d;
}
void set_coefficients(float* c) {
for (int i = 0; i <= degree; i++) {
coefficients[i] = c[i];
}
}
////////////Getters/////////////
int get_degree() const {
return degree;
}
float* get_coefficients() const {
return coefficients;
}
/////////MEMBER FUNCTIONS///////////
//Displaying polynomial
void display() const {
for (int j = degree; j >= 0; j--) {
}
cout << endl;
}
//Polynomial addition
void add_poly(const polynomial& p) {
for (int k = 0; k <= degree; k++) {
coefficients[k] += p.coefficients[k];
}
}
//Polynomial subtraction
void subtract_poly(const polynomial& p) {
for (int k = 0; k <= degree; k++) {
coefficients[k] -= p.coefficients[k];
}
}
//Polynomial multiplication
void multiply_poly(const polynomial& p) {
for (int k = 0; k <= degree; k++) {
coefficients[k] *= p.coefficients[k];
}
}
~polynomial() {
delete[] coefficients;
}
};
int main() {
int degree;
//read degree from user
cout << "Enter the degree for the polynomials: ";
cin >> degree;
return 0;
}
Output: