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

Oop Assignment

The document discusses the key differences between procedural programming (POP) and object-oriented programming (OOP). In POP, programs are designed as a sequence of procedures or functions that operate on data, with a focus on the process. In OOP, programs are designed as a collection of objects that have both data and functions associated with them, with a focus on modeling interacting objects.

Uploaded by

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

Oop Assignment

The document discusses the key differences between procedural programming (POP) and object-oriented programming (OOP). In POP, programs are designed as a sequence of procedures or functions that operate on data, with a focus on the process. In OOP, programs are designed as a collection of objects that have both data and functions associated with them, with a focus on modeling interacting objects.

Uploaded by

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

Q 1:

In POP, programs are designed as a sequence of procedures or functions that


operate on data. The data and the functions are separate entities, and the focus is on
breaking down the program into smaller parts that can be executed one after
another. POP is more focused on the process of performing actions than the data
being manipulated.

In OOP, programs are designed as a collection of objects that have both data and
functions (methods) associated with them. The focus is on modeling the problem
domain as a set of interacting objects, where each object has a defined set of
behaviors and properties.

Q 2:

1. Enumeration Data Type:

Enumeration is a data type that allows you to define a list of named constants. These
constants can then be used throughout your code, providing a more readable and
maintainable way to represent certain values. Here is an example of how you might
use an enumeration in Java:

typescriptCopy code
enum DayOfWeek { MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY ,
SUNDAY }

In this example, we define an enumeration called DayOfWeek with seven named


constants representing each day of the week.

2. String Data Type:

A string is a data type that represents a sequence of characters.

pythonCopy code
name = "John" age = 25 print ( "My name is " + name + " and I am " + str (age) + " years old." )

In this example, we create a variable called name of type string and assign it the
value "John". We also create a variable called age of type integer and assign it the
value 25. We then use string concatenation to create a new string that combines the
values of name and age. Finally, we print out the resulting string using the print
function, which will output "My name is John and I am 25 years old."

Q 3:

In C++, new and delete are used for dynamic memory allocation and deallocation,
respectively.

Here's an example:

int * myArray = new int [ 5 ];

In this example, we're using the new operator to dynamically allocate an array of 5
integers on the heap.

delete [] myArray;

Here, we're using the delete operator to deallocate the array of integers we allocated
earlier.

Q 4:

Data hiding is a principle in object-oriented programming that involves


encapsulating data and methods within a class, such that the data cannot be
accessed or modified by external code without going through the class's interface.
Data hiding helps to ensure that the internal state of an object remains consistent
and valid, which can help prevent bugs and improve code quality.
In C++, data hiding is achieved through the use of access modifiers such as public ,
private, and protected. These access modifiers determine which parts of a class are
visible and accessible from outside the class. The public keyword indicates that a
member is accessible from anywhere, while the private keyword indicates that a
member is only accessible from within the class.

Here's an example:

javaCopy code
class BankAccount { public : void deposit(double amount) { balance += amount; } void withdraw(double
amount) { if (balance >= amount) { balance -= amount; } else { std::cout << "Insufficient funds\n" ; } }
private : double balance = 0.0 ; };

In this example, we have a BankAccount class that contains a balance member variable
and two member functions, deposit and withdraw. The balance variable is marked as
private, which means that it can only be accessed from within the class. The deposit
and withdraw functions are marked as public , which means that they can be called
from outside the class.

By hiding the balance member variable from external code, we ensure that the
balance can only be modified through the deposit and withdraw functions, which can
enforce constraints and ensure the validity of the account balance.

Q 5;

a) Objects and classes: Objects and classes are fundamental concepts in object-oriented
programming. A class is a blueprint or a template that defines the characteristics and
behavior of a group of objects. It contains member variables, methods, and constructors that
define the properties and actions of the objects that belong to the class. On the other hand,
an object is an instance of a class. It is a concrete entity that is created based on the
definition provided by the class. In other words, a class is a general concept, while an object
is a specific implementation of that concept.

b) Data abstraction and data encapsulation: Data abstraction and data encapsulation are
two related concepts that are used in object-oriented programming to hide the
implementation details of data and methods. Data abstraction is the process of representing
the essential features of an object without showing the implementation details. It involves
defining the public interface of a class, which consists of the methods that can be used to
manipulate the data stored in the object. Data encapsulation, on the other hand, is the
process of hiding the implementation details of an object and making the data and methods
private. It involves wrapping the data and methods of a class into a single unit, which
prevents external code from accessing the data directly. The encapsulated data can only be
accessed through the public interface provided by the class.

You might also like