Oop Assignment
Oop Assignment
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:
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 }
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:
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:
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.