Lab Report 1
Lab Report 1
LAB MANUAL - 01
Course Instructor: Lecturer Anum Abdul Salam
Degree/ Syndicate: 45 / B
Total 10
Total Marks = O𝒃𝒕𝒂𝒊𝒏𝒆𝒅 𝑴𝒂𝒓𝒌𝒔 (∑6𝟏 𝑹𝒊 ∗ 𝑹7)
Lab # 01: Procedural Programming
Lab Objective:
The main purpose of this lab to revise procedural programming concepts and to understand the
difference between procedural programming and object-oriented programing.
Lab Description:
Procedural Programming may be the first programming paradigm that a new developer will
learn. Fundamentally, the procedural code is the one that directly instructs a device on how to
finish a task in logical steps. This paradigm uses a linear top-down approach and treats data and
procedures as two different entities. Based on the concept of a procedure call, Procedural
Programming divides the program into procedures, which are also known as routines or
functions, simply containing a series of steps to be carried out.
Source Code:
#include<iostream>
#define PI 3.14
using namespace std;
class Circle {
//private access specifier for private members of class
private:
float radius; //attribute declaration: radius
//main fucntion
int main(){
Circle C1; //Object instantiation
return 0;
}
Output:
2. Design a program for complex numbers. Complex number consists of real and
imaginary part. You are required to set real and imaginary input values from user.
Provide following member functions:
i. Take input complex number from user (real and imaginary part)
ii. Display Complex number in form of a+bi
iii. Addition of two complex numbers
iv. Subtraction of two complex numbers
Source Code:
#include<iostream>
using namespace std;
class ComplexNumber {
//private access specifier
private:
float real;
float imag;
//////////////////////////////////Constructors///////////////////////////
//
//Default Constructor
ComplexNumber() {
real = 0;
imag = 0;
}
//Parameterized Constructor
ComplexNumber(float r, float i) {
set_real(r);
set_imag(i);
}
///////////////////////////////More Member
Functions//////////////////////////////////////
//Objects instantiation
ComplexNumber num1, num2, result;
return 0;
}
Output:
3. Design a program in C++ capable to represent a circuit. Circuit may have either at
most three resistances OR Capacitances, provide following functionalities to your
Program.
a. A function that takes resistance OR Capacitance value of all components.
b. A function that computes equivalent resistance OR equivalent capacitance
accordingly
#include<iostream>
#include<vector>
using namespace std;
class Circuit {
//private access specifier
private:
vector<float> components; //vector to store capacitances and
resistances of circuit
bool isCapacitance; /*boolean variable to check circuit
type i.e. capacitive or resistive.
True if circuit is
capacitive, false if circuit is resistive*/
//////////////////Constructors/////////////////////
//Default constructor
Circuit() {
isCapacitance = true; //default circuit to capacitive
}
//parameterized constructor
Circuit(const vector<float>& comps, bool type) {
components = comps;
isCapacitance = type;
}
//////////////////Member functions//////////////////////////
// Function to take input for circuit type and components
void Input() {
float value;
cout << "Enter component values (enter -1 to stop):\n";
while (true) {
// Prompt user based on whether the circuit is for
capacitance or resistance
if (isCapacitance == true) {
cout << "Enter capacitance in Farads: ";
}
else {
cout << "Enter resistance in Ohms: ";
}
};
int main() {
//Both resistive and capacitive circuits are included here for
testing (this can be modified according to needs)
//object instantiation : resistive circuit
Circuit resistanceCircuit;
resistanceCircuit.set_type(false); // Set the circuit type to
resistive
resistanceCircuit.Input(); // Take input from the user for
resistances
return 0;
}
Output:
The above program caters to any number of circuit components i.e. n number of components
making it more flexible and user friendly. However, as specified for utmost three components,
a few changes in Input() function can be made by adding a for loop that limits the user to enter
only three values.
class Circuit {
//private access specifier
private:
vector<float> components; //vector to store capacitances and
resistances of circuit
bool isCapacitance; /*boolean variable to check circuit type
i.e. capacitive or resistive.
True if circuit is
capacitive, false if circuit is resistive*/
//////////////////Constructors/////////////////////
//Default constructor
Circuit() {
isCapacitance = true; //default circuit to capacitive
}
//parameterized constructor
Circuit(const vector<float>& comps, bool type) {
components = comps;
isCapacitance = type;
}
if (components.size() == 0) {
cout << "No components were added.\n";
}
}
};
int main() {
//Both resistive and capacitive circuits are included here for testing
(this can be modified according to needs)
//object instantiation : resistive circuit
Circuit resistanceCircuit;
resistanceCircuit.set_type(false); // Set the circuit type to resistive
resistanceCircuit.Input(); // Take input from the user for resistances
return 0;
}
Output:
THINK
2. Name some of the languages other than mentioned in Lab manual that
supports procedural programming?
a. Ada
b. Python
c. Perl
d. COBOL
a. Code Redundancy: Repeated code can occur as there's limited support for code
reuse.
b. Difficult Maintenance: As programs grow larger, procedural code becomes
harder to maintain and modify due to a lack of encapsulation.
c. Poor Data Security: Data is often exposed and can be modified freely, increasing
the risk of unintended changes.
d. Less Flexibility: Limited support for flexibility in handling different data types or
operations.