0% found this document useful (0 votes)
4 views4 pages

Oop Lab As1

The document discusses various programming paradigms including Procedural Programming, Object-Oriented Programming (OOP), and Functional Programming, outlining their definitions, pros, and cons. It provides C++ code examples to compare procedural and object-oriented approaches, highlighting the advantages of encapsulation and reusability in OOP. Additionally, it explains the concepts of classes, methods, access modifiers, structures, and arrays in C and C++.

Uploaded by

imahanoor123
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)
4 views4 pages

Oop Lab As1

The document discusses various programming paradigms including Procedural Programming, Object-Oriented Programming (OOP), and Functional Programming, outlining their definitions, pros, and cons. It provides C++ code examples to compare procedural and object-oriented approaches, highlighting the advantages of encapsulation and reusability in OOP. Additionally, it explains the concepts of classes, methods, access modifiers, structures, and arrays in C and C++.

Uploaded by

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

Assignment 01

OBJECT ORIENTED
PROGRAMMING-LAB
By

NAME: Mahnoor Iqbal

(CMS-ID):2382-2022

Program BSCS-3A

Course Instructor
SIR UZAIR

Faculty of Engineering Sciences and Technology


Hamdard University, Karachi, Pakistan
Q1: Types of Programming (definition, pros & cons.):

Procedural Programming: This programming paradigm revolves around procedures or functions to


perform tasks. The primary focus of procedural programming is on the process, rather than the objects.
Pros:
Simplicity: Procedural programming is straightforward and easy to understand.
Readability: The code is easy to read and understand.
Cons:
Limited reusability: In larger programs, reusing code becomes challenging.
Difficulty in managing large codebases: As the program grows in complexity, it becomes difficult to
maintain and debug.
Object-Oriented Programming (OOP): OOP is a programming paradigm that organizes code
around objects, offering modularity and reusability. The central theme of OOP is the object, which can
contain data and methods.
Pros:
Code reusability: Once an object is created, it can be reused in multiple places, leading to code
reduction.
Easier maintenance: Since the code is organized into objects, it is easier to find and fix bugs.
Cons:
Added complexity: OOP can introduce complexity, especially for beginners.
Functional Programming: Functional programming (FP) treats computation as the evaluation of
mathematical functions and avoids changing state and mutable data.
Pros:
Immutability: The data in functional programming is immutable, reducing the chances of bugs.
Ease of testing: The functions in functional programming are easy to test due to their purity.
Cons:
Steep learning curve: For developers unfamiliar with functional programming, it can be a challenging
concept to grasp.
Lower performance: Some operations in functional programming may be slower compared to
procedural or object-oriented programming.

Q2: Code in C++ and its comparison (procedure vs oops):

PROCEDURAL PROGRAMMING

#include <iostream>
#include <string>

void getName(std::string &name) {


std::cout << "Enter name: ";
std::cin >> name;
}

void getAge(int &age) {


std::cout << "Enter age: ";
std::cin >> age;
}

void displayDetails(std::string name, int age) {


std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}

int main() {
std::string name;
int age;

getName(name);
getAge(age);
displayDetails(name, age);

return 0;
}

Vs
OBJECT ORIENTED PROGRAMMING
#include <iostream>
#include <string>

class Person {
private:
std::string name;
int age;

public:
void getName() {
std::cout << "Enter name: ";
std::cin >> name;
}

void getAge() {
std::cout << "Enter age: ";
std::cin >> age;
}

void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
int main() {
Person person;

person.getName();
person.getAge();
person.displayDetails();

return 0;
}

Comparison:
Procedural (C-style) code example focuses on a linear flow of control using functions. Each function has
a specific purpose and the flow of control moves from one function to another. This style of coding can
lead to better organization in certain scenarios.

Object-Oriented Programming (OOP) example, on the other hand, uses a class to define an object with
attributes (name and age) and methods (getName, getAge, and displayDetails). This style of coding
provides better encapsulation, allowing the object

Q3: Class, methods, access modifier:


Class: In object-oriented programming, a class is a blueprint for creating objects. It defines the
structure and behavior of objects.

Methods: Methods are functions defined within a class that represent the behavior or actions that
objects of that class can perform.

Access Modifier: Access modifiers in OOP control the visibility and accessibility of class members.
Common access modifiers include public (accessible from anywhere), private (only accessible within the
class), and protected (accessible within the class and its subclasses).

Q4: Structure & array:

Structure:
In C and C++, a structure is a composite data type that allows you to group variables of different data
types under a single name. It’s used to create a user-defined data type that can represent a record or
data structure with various fields, each of which can have its own data type. Structures are often used to
encapsulate related data, making it easier to organize and work with complex data. The fields (also
known as members) of a structure are accessed using the dot (.) operator.

Array:
An array is a data structure in C and C++ that stores a fixed-size, sequential collection of elements of the
same data type. Arrays provide efficient data access because elements are stored in contiguous memory
locations, allowing for fast indexing. However, arrays have a fixed size, which means you must specify
the size when declaring them, and this size cannot be changed dynamically. Arrays are commonly used
for tasks that involve storing and processing a collection of similar data, such as a list of numbers or
strings.

You might also like