Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lab Report 1

Uploaded by

Fabeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Report 1

Uploaded by

Fabeha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CS-212 Object Oriented Programming

LAB MANUAL - 01
Course Instructor: Lecturer Anum Abdul Salam

Lab Engineer: Kashaf Raheem

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

What is Procedural Programming? [Definition]

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.

Difference between Procedural Programming and Object-Oriented Programming:

PROCEDURAL ORIENTED OBJECT ORIENTED


PROGRAMMING PROGRAMMING
In procedural programming, program is divided In object-oriented programming, program is
into small parts called functions. divided into small parts called objects.
Procedural programming follows top-down Object oriented programming
approach. follows bottom-up approach.
There is no access specifier in procedural Object oriented programming have access
programming. specifiers like private, public, protected etc.
Adding new data and function is not easy. Adding new data and function is easy.
Procedural programming does not have any Object oriented programming provides data
proper way for hiding data so it is less secure. hiding so it is more secure.
In procedural programming, overloading is not Overloading is possible in object-oriented
possible. programming.
In procedural programming, function is more In object-oriented programming, data is
important than data. more important than function.
Procedural programming is based on unreal Object oriented programming is based
world. on real world.
Examples: C, FORTRAN, Pascal, Basic etc. Examples: C++, Java, Python, C# etc.
LAB TASKS:

1. Write a program to calculate area of circumference of circle, take radius as an


input from user and write the following function:
 GetInput
 CalculateArea
 CalculateCircumference
 Display

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

//Public access specifier for public members of class


public:
//setter
float set_radius(float r) {
if (r > 0) {
radius = r;
}
else {
radius = 5; // Default value if invalid input is
given
cout << "Invalid radius. Setting radius to default
value of 5 cm." << endl;
}
return 0;
}

// Getter used for accessing private attribute radius


float GetRadius() {
return radius;
}

// Function to calculate area of circle


float CalculateArea() {
return PI * radius * radius;
}

//Function to calculate circumference of circle


float CalculateCircumference() {
return 2 * PI * radius;
}

//Function to display the area and circumference of circle


void Display() {
cout << "Area of circle of radius " << GetRadius() << " cm =
" << CalculateArea();

cout << "\nCircumference of circle of radius " <<


GetRadius() << " cm = " << CalculateCircumference();
}
};

//main fucntion
int main(){
Circle C1; //Object instantiation

float radius_main; // variable declaration

//Reading value of radius from user


cout << "Enter radius (cm) of the circle whose area and
circumference you want to calculate: ";
cin >> radius_main;

//Calling the setter function for radius value


C1.set_radius(radius_main);

//Calling the display function to display value


C1.Display();
Circle c2;

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;

//public access specifier


public:

//////////////////////////////////Constructors///////////////////////////
//

//Default Constructor
ComplexNumber() {
real = 0;
imag = 0;
}

//Parameterized Constructor
ComplexNumber(float r, float i) {
set_real(r);
set_imag(i);
}

/////////////////////////Setter & Getter


Functions/////////////////////////

//Setter function for attribute 'real'


void set_real(float r) {
real = r;
}

//Setter function for attribute 'imag'


void set_imag(float i) {
imag = i;
}

//Getter function for attribute 'real'


float get_real() {
return real;
}

//Getter function for attribute 'imag'


float get_imag() {
return imag;
}

///////////////////////////////More Member
Functions//////////////////////////////////////

//Input function to read real and imaginary parts of complex number


from user
void Input() {
cout << "\nEnter real part : ";
cin >> real;

cout << "Enter imaginary part : ";


cin >> imag;
}

//Display function to display complex number


void Display() {
if (imag < 0) {
cout << real << " - " << -imag << "i"; // Corrected to
remove extra negative sign
}
else {
cout << real << " + " << imag << "i";
}
}

//Addition function used to add two complex numbers


ComplexNumber Add(ComplexNumber& C) {
ComplexNumber result;
result.real = C.real + real;
result.imag = C.imag + imag;
return result;
}

//Subtraction function used to subtract two numbers


ComplexNumber Subtract(ComplexNumber& C) {
ComplexNumber result;
result.real = real - C.real; // Corrected subtraction formula
result.imag = imag - C.imag; // Corrected subtraction formula
return result;
}
};
int main() {

//Objects instantiation
ComplexNumber num1, num2, result;

//Read two complex numbers from user


cout << "Enter the first complex number: ";
num1.Input();

cout << "Enter the second complex number: ";


num2.Input();

//Display both numbers


cout << "\nFirst Complex Number: ";
num1.Display();

cout << "\nSecond Complex Number: ";


num2.Display(); // Corrected: added missing parentheses

//Add the complex numbers and displaying their sum


result = num1.Add(num2);
cout << "\n\nSum = (";
num1.Display();
cout << ") + (";
num2.Display();
cout << ") = ";
result.Display();

//Subtract the complex numbers and displaying their difference


result = num1.Subtract(num2);
cout << "\n\nDifference = (";
num1.Display();
cout << ") - (";
num2.Display();
cout << ") = ";
result.Display();

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

Source Code for ‘n’ number of components:

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

//public access specifier


public:

//////////////////Constructors/////////////////////
//Default constructor
Circuit() {
isCapacitance = true; //default circuit to capacitive
}

//parameterized constructor
Circuit(const vector<float>& comps, bool type) {
components = comps;
isCapacitance = type;
}

////////////Setter and Getter Functions///////////////

// Setter function to specify component values (resistance or


capacitance)
void set_components(const vector<float>& comps) {
components = comps;
}

// Setter fucntion to specify if the circuit is capacitive or


resistive
void set_type(bool type) {
isCapacitance = type;
}

// Getter fucntion to retrieve components


vector<float> get_components() const {
return components;
}

// Getter to check if it's a capacitive or resistive circuit


bool get_type() const {
return isCapacitance;
}

//////////////////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: ";
}

// Read the input value


cin >> value;

// Break the loop if the user enters -1 (sentinel value)


if (value == -1) {
break;
}

// Add the entered value to the components vector


components.push_back(value);
}
}

//Function to compute equivalent resistances and capacitance when


they're connected in series
float Series() {
float total = 0;
if (isCapacitance == true) { // For capacitances in series
for (int i = 0; i < components.size(); ++i) {
total += 1 / components[i];
}
return 1 / total;
}
else { // For resistances in series
for (int i = 0; i < components.size(); ++i) {
total += components[i];
}
return total;
}
}

//Function to compute equivalent resistances and capacitance when


they're connected in parallel
float Parallel() {
float total = 0;
if (isCapacitance == true) { // For capacitances in parallel
for (int i = 0; i < components.size(); ++i) {
total += components[i];
}
return total;
}
else { // For resistances in parallel
for (int i = 0; i < components.size(); ++i) {
total += 1 / components[i];
}
return 1 / total;
}
}

//Display function for displaying the components of circuit


void display() {
cout << "Circuit components: ";
for (int i = 0; i < components.size(); ++i) {
cout << components[i] << " ";
}
if (isCapacitance)
cout << "Farads" << endl;
else
cout << "Ohms" << endl;
}

};

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

resistanceCircuit.display(); // Display the circuit components


cout << "Equivalent resistance in series: " <<
resistanceCircuit.Series() << " Ohms" << endl;
cout << "Equivalent resistance in parallel: " <<
resistanceCircuit.Parallel() << " Ohms" << endl;

// object instantiation : capacitive circuit


Circuit capacitanceCircuit;
capacitanceCircuit.set_type(true); // Set the circuit type to
capacitive
capacitanceCircuit.Input(); // Take input from the user for
capacitances

capacitanceCircuit.display(); // Display the circuit components


cout << "Equivalent capacitance in series: " <<
capacitanceCircuit.Series() << " Farads" << endl;
cout << "Equivalent capacitance in parallel: " <<
capacitanceCircuit.Parallel() << " Farads" << endl;

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.

Source Code for maximum three components:


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

//public access specifier


public:

//////////////////Constructors/////////////////////
//Default constructor
Circuit() {
isCapacitance = true; //default circuit to capacitive
}

//parameterized constructor
Circuit(const vector<float>& comps, bool type) {
components = comps;
isCapacitance = type;
}

////////////Setter and Getter Functions///////////////

// Setter function to specify component values (resistance or


capacitance)
void set_components(const vector<float>& comps) {
components = comps;
}

// Setter fucntion to specify if the circuit is capacitive or resistive


void set_type(bool type) {
isCapacitance = type;
}

// Getter fucntion to retrieve components


vector<float> get_components() const {
return components;
}

// Getter to check if it's a capacitive or resistive circuit


bool get_type() const {
return isCapacitance;
}
//////////////////Member functions//////////////////////////
// Function to take input for circuit type and components
void Input() {
float value;
cout << "Enter up to 3 component values (enter -1 to stop early):\
n";

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


// Prompt user based on whether the circuit is for
capacitance or resistance
if (isCapacitance) {
cout << "Enter capacitance in Farads: ";
}
else {
cout << "Enter resistance in Ohms: ";
}

// Read the input value


cin >> value;

// Break the loop if the user enters -1 (sentinel value)


if (value == -1) {
break;
}

// Add the entered value to the components vector


components.push_back(value);
}

if (components.size() == 0) {
cout << "No components were added.\n";
}
}

//Function to compute equivalent resistances and capacitance when they're


connected in series
float Series() {
float total = 0;
if (isCapacitance == true) { // For capacitances in series
for (int i = 0; i < components.size(); ++i) {
total += 1 / components[i];
}
return 1 / total;
}
else { // For resistances in series
for (int i = 0; i < components.size(); ++i) {
total += components[i];
}
return total;
}
}

//Function to compute equivalent resistances and capacitance when they're


connected in parallel
float Parallel() {
float total = 0;
if (isCapacitance == true) { // For capacitances in parallel
for (int i = 0; i < components.size(); ++i) {
total += components[i];
}
return total;
}
else { // For resistances in parallel
for (int i = 0; i < components.size(); ++i) {
total += 1 / components[i];
}
return 1 / total;
}
}

//Display function for displaying the components of circuit


void display() {
cout << "Circuit components: ";
for (int i = 0; i < components.size(); ++i) {
cout << components[i] << " ";
}
if (isCapacitance)
cout << "Farads" << endl;
else
cout << "Ohms" << endl;
}

};

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

resistanceCircuit.display(); // Display the circuit components


cout << "Equivalent resistance in series: " << resistanceCircuit.Series()
<< " Ohms" << endl;
cout << "Equivalent resistance in parallel: " <<
resistanceCircuit.Parallel() << " Ohms" << endl;

// object instantiation : capacitive circuit


Circuit capacitanceCircuit;
capacitanceCircuit.set_type(true); // Set the circuit type to capacitive
capacitanceCircuit.Input(); // Take input from the user for capacitances

capacitanceCircuit.display(); // Display the circuit components


cout << "Equivalent capacitance in series: " <<
capacitanceCircuit.Series() << " Farads" << endl;
cout << "Equivalent capacitance in parallel: " <<
capacitanceCircuit.Parallel() << " Farads" << endl;

return 0;
}

Output:
THINK

1. What are the advantages of Procedural Programming?


Procedural Programming is easier to understand for beginner programmers as it
follows a step-by-step approach. It’s suitable for performing smaller tasks. In this
approach, a program is divided into functions which allows reuse without
repetition. It is effective in smaller projects whose flow control is clear. However
this method cannot handle complex real world problems. It lacks the ability to
organize the program into objects which makes code maintenance harder for
larger projects.

2. Name some of the languages other than mentioned in Lab manual that
supports procedural programming?

a. Ada
b. Python
c. Perl
d. COBOL

3. What are the overheads of Procedural Programming?

Some of the overheads of Procedural Programming are:

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.

You might also like