0% found this document useful (0 votes)
9 views

Lab Report 5

Uploaded by

Fabeha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab Report 5

Uploaded by

Fabeha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CS-212 Object Oriented Programming

LAB REPORT - 05
Course Instructor: Lecturer Anum Abdul Salam

Lab Engineer: Eman Fatima

Student Name: Fabeha Zahid Mahmood

Registration no. : 480057

Degree/ Syndicate: 45 / B

Trait Obtained Maximum


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

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++.

Class Design Requirements

1. Class Name: NInputANDGate


2. Attributes:
o bool* inputs: A dynamic array to hold the input values, which can
be true (high) or false (low).
o int inputCount: The number of inputs that the AND gate can
accept, allowing for flexible configurations.
3. Methods:
o Constructor: A constructor that initializes the input array and
sets the number of inputs, ensuring the inputs are initially set to
false.
o Copy constructor: A constructor to ensure deep copy
o Destructor: A destructor that cleans up the dynamically
allocated memory for the input array, preventing memory leaks.
o Set Input Method: A method to set the value of an input at a
specific index, with error checking to ensure the index is valid.
o Calculate Output Method: A method that computes the output
of the AND gate based on the current input values, reflecting the
logic of an AND operation.
o Display Method: A method to print the current state of the
inputs and the resulting output in a user-friendly format.
Source Code:

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

// Copy Constructor (Deep Copy)


NInputANDGate(const NInputANDGate& other) {
inputCount = other.inputCount;
inputs = new bool[inputCount]; // Allocate new memory for
deep copy
for (int i = 0; i < inputCount; i++) {
inputs[i] = other.inputs[i];
}
cout << "Copy constructor called!" << endl; // Print to
show copy constructor is invoked
}

// Destructor
~NInputANDGate() {
delete[] inputs; // Clean up dynamically allocated memory
}

// Set input method


void setInput(int index, bool value) {
if (index >= 0 && index < inputCount) {
inputs[index] = value;
}
else {
cout << "Error: Invalid input index!" << endl;
}
}

// Calculate output method (AND operation)


bool calculateOutput() const {
bool result = true; // AND gate only outputs true if all
inputs are true
for (int i = 0; i < inputCount; i++) {
result = result && inputs[i]; // Apply AND logic
if (!result) break; // If any input is false, output
is false
}
return result;
}

// Display the current state of inputs and the output


void display() const {
cout << "Inputs: ";
for (int i = 0; i < inputCount; i++) {
cout << (inputs[i] ? "true" : "false") << " ";
}
cout << endl;
cout << "AND Gate Output: " << (calculateOutput() ? "true" :
"false") << endl;
}
};

int main() {
int n;
cout << "Enter the number of inputs for the AND gate: ";
cin >> n;

NInputANDGate andGate1(n); // Create AND gate with 'n' inputs

// Set the input values for the first AND gate


for (int i = 0; i < n; i++) {
bool value;
cout << "Enter value for input " << i + 1 << " (0 for false,
1 for true): ";
cin >> value;
andGate1.setInput(i, value); // Set input value
}

// Display the inputs and the result for the first AND gate
cout << "First AND Gate:" << endl;
andGate1.display();

// Create a second AND gate using the copy constructor


NInputANDGate andGate2 = andGate1; // Copy constructor is called
here

// Display the inputs and the result for the second AND gate
cout << "Copied AND Gate:" << endl;
andGate2.display();

return 0;
}
Output:

Task No. 02:

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.

For instance, a polynomial of degree n can be expressed in the general form:

 P(x) is the polynomial.


 an, an-1,…,a1,a0 are the coefficients.
 N is the highest power of the variable xxx, known as the degree of the
polynomial.
Objective: You will design a polynomial and identify its coefficients.

1. Choose a Polynomial: Select or create a polynomial of your choice.


You can use any combination of coefficients and degrees but ensure
the polynomial has at least three terms.
2. Identify Coefficients: Write down the polynomial in its standard form
and explicitly identify the coefficients associated with each term.
3. Degree Determination: Determine the degree of your polynomial
and explain its significance.
4. Evaluate Polynomial: To evaluate a polynomial at a specific value of
x, substitute that value into the polynomial's expression and compute
the result. The value of x should be a float/fraction.

5. Multiply, add, subtract Polynomial: Functions to add, subtract and


multiply two polynomials

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

//Input function for reading coefficient


void get_input() {
for (int i = degree; i >= 0; i--) {
cout << "Enter The Value For The Coefficient of x^" <<
i << ": ";
cin >> coefficients[i];
}
}

//Displaying polynomial
void display() const {
for (int j = degree; j >= 0; j--) {

cout << coefficients[j] << "x^" << j;


if (j != 0) {
cout << " + ";
}

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

// Evaluate polynomial at a specific value of x


float evaluate_poly(float x) const {
float result = 0;
for (int i = 0; i <= degree; i++) {
result += coefficients[i] * pow(x, i); // Calculate
each term and add to result
}
return result;
}

~polynomial() {
delete[] coefficients;
}
};

int main() {
int degree;
//read degree from user
cout << "Enter the degree for the polynomials: ";
cin >> degree;

//create first polynomial (object instantiation)


polynomial poly1(degree);

//read coefficients for first polynomial and display it


cout << "Enter coefficients for the first polynomial: " << endl;
poly1.get_input();
cout << "\nFirst Polynomial = P1(x) = ";
poly1.display();

// Evaluate the polynomial at a specific value of x


float x;
cout << "\nEnter the value of x to evaluate the polynomial: ";
cin >> x;
float result1 = poly1.evaluate_poly(x);
cout << "\nThe value of the polynomial at x = " << x << " is: " <<
result1 << endl;

//Create second polynomial (object instantiation)


polynomial poly2(degree);
//read coefficients for second polynomial and display it
cout << "\nEnter coefficients for the second polynomial: " << endl;
poly2.get_input();
cout << "\nSecond Polynomial = P2(x) = ";
poly2.display();

// Evaluate the polynomial at a specific value of x


float y;
cout << "\nEnter the value of x to evaluate the polynomial: ";
cin >> y;
float result2 = poly2.evaluate_poly(y);
cout << "\nThe value of the polynomial at x = " << y << " is: " <<
result2 << endl;

//Add the polynomials


poly1.add_poly(poly2);
cout << "\nThe sum of the polynomials is: ";
poly1.display();

//Subtract the polynomials


poly1.subtract_poly(poly2);
cout << "\nThe difference of the polynomials is: ";
poly1.display();

//Multiply the polynomials


poly1.multiply_poly(poly2);
cout << "\nThe product of the polynomials is: ";
poly1.display();

return 0;
}

(output on next page)

Output:

You might also like