[go: up one dir, main page]

0% found this document useful (0 votes)
11 views12 pages

Oops Unit - 2

The document provides an overview of Object-Oriented Programming (OOP), focusing on the concepts of classes and objects, which are essential for creating modular and reusable code. It covers various programming elements such as data types, operators, control structures, access specifiers, constructors, destructors, and dynamic memory allocation. Additionally, it discusses storage classes in C/C++, highlighting their impact on variable scope, lifetime, and memory management.

Uploaded by

arman8119anand
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)
11 views12 pages

Oops Unit - 2

The document provides an overview of Object-Oriented Programming (OOP), focusing on the concepts of classes and objects, which are essential for creating modular and reusable code. It covers various programming elements such as data types, operators, control structures, access specifiers, constructors, destructors, and dynamic memory allocation. Additionally, it discusses storage classes in C/C++, highlighting their impact on variable scope, lifetime, and memory management.

Uploaded by

arman8119anand
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/ 12

Assignment of Object Oriented Programming – 2

 Classes And Objects:

In Object-Oriented Programming (OOP), the concepts of classes and objects form


the foundation of this programming paradigm. These two concepts allow for the
creation of modular, reusable, and organized code that mimics real-world entities
and behaviors.

A class in OOP is a blueprint or a template that defines the properties and behaviors
common to all objects of a particular type. It is essentially a user-defined data type
that specifies what data (attributes or fields) and operations (methods or functions)
are associated with the objects. A class defines the structure and the functionality
that objects created from it will have, but it does not create objects by itself. It is a
conceptual definition, and no memory is allocated until an object of that class is
instantiated.

An object, on the other hand, is an instance of a class. When a class is defined, it is


essentially just a template. Only when an object is created from that class does it
occupy memory and hold actual data values. An object contains real data for its
attributes and can invoke the methods defined in the class. Every object created
from the same class will have the same structure, meaning they will have the same
types of attributes and behaviors, but the values stored in those attributes may
differ between objects. In simpler terms, an object represents a specific entity
created based on the blueprint provided by the class.

For example, if you have a class called Car, the class might define attributes like
speed, color, and fuelLevel, along with methods like accelerate() or brake(). When
you create an object of the Car class, such as myCar, it will have specific values for
these attributes (like speed = 60, color = “Red”) and will be able to execute the
methods (like accelerate()).

In summary, a class serves as the template or definition of an object, describing its


structure and the functions it can perform, while an object is the actual instance of
the class, storing real data and executing the behaviors as described by the class.
The relationship between classes and objects is fundamental to the concept of
encapsulation in OOP, where data and functions are bundled together into objects.

Classes and Objects in Programming (Object-Oriented Programming – OOP)

Object-oriented programming (OOP) is a programming paradigm based on the


concept of “objects,” which can contain data and methods to manipulate that data.
In OOP, classes are blueprints for creating objects (instances).

Let’s break down each concept in detail:

 Data Types
Data types define the type of data a variable can hold. There are two categories:

Primitive (Basic) Data Types: Integer, float, char, boolean, etc.


Derived Data Types: Arrays, strings, etc.
User-defined Data Types: Classes, structures, unions, etc.

 Operators
Operators perform operations on variables and values. Common operators include:

Arithmetic operators: +, -, *, /, % (addition, subtraction, multiplication, division,


modulus).
Relational operators: ==, !=, <, >, <=, >= (compare two values).
Logical operators: &&, ||, ! (AND, OR, NOT).
Assignment operators: =, +=, -=, *=, /= (assign values).
Bitwise operators: &, |, ^, ~, <<, >> (operate on bits).
Increment/Decrement operators: ++, -- (increment or decrement by 1).

2
 Expressions
An expression is a combination of variables, constants, operators, and functions
that are evaluated to produce a value.
For example:

 Control Structures

Control structures determine the flow of the program. These include:

Conditional Statements: if, else if, else, switch (to execute different code based on
conditions).
Loops: for, while, do-while (for repeated execution of a block of code).
Jump Statements: break, continue, return (to control loop execution or exit
functions).
Example:

3
 Arrays
An array is a collection of elements of the same data type, stored in contiguous
memory locations.
Example:

 Strings
A string is an array of characters terminated by a null character (\0). In modern
languages, strings are often treated as objects.
Example in C++:

 Classes and Object:


Class: A class is a user-defined blueprint or prototype from which objects are
created. A class defines the properties (data members) and behaviors (methods) of
objects. Example:

4
Object: An object is an instance of a class

 Access Specifiers
Access specifiers define the visibility of class members (data members and
methods). The most common are:

Public: Accessible from anywhere in the program.


Private: Accessible only within the class.
Protected: Accessible within the class and derived classes.
Example

5
 Constructors

A constructor is a special member function that initializes objects. It is automatically


called when an object is created. Constructors have the same name as the class and
no return type.
Example:

 Destructors

A destructor is a special function called when an object is destroyed. It is used to


free memory or perform cleanup tasks. It has the same name as the class, but with
a tilde (~) before it.
Example:

6
 Operator Overloading

Operator overloading allows you to redefine the functionality of operators (like +, -,


*) for user-defined data types (such as classes). This enables objects to behave like
built-in types for certain operations.
Example:

 Type Conversion

Type conversion refers to converting one data type into another. There are two
types:
Implicit Type Conversion (Automatic): Performed by the compiler.
Explicit Type Conversion (Casting): Performed by the programmer.
Example of explicit conversion (casting):

7
Example: Putting it All Together in C++

This example demonstrates a class with a constructor, a method, an overloaded


operator, and a destructor.

8
By combining all of these elements, Object-Oriented Programming allows you to
create complex systems with reusable and modular components.

 Storage Classes in C/C++:

In C/C++ programming, storage classes define the scope, lifetime, visibility, and
memory location of variables. They help in determining how and where a variable is
stored, and for how long it retains its value during the execution of a program.
There are several storage classes, and they are broadly classified into automatic and
fixed types based on their properties.

 Fixed vs. Automatic Declaration


Fixed Declaration: Variables declared with fixed storage classes (like static and
extern) have a fixed duration for their lifetime. This means that the variable is
created when the program starts and destroyed when the program ends. It is not
created and destroyed each time a function is called.

Automatic Declaration: Variables declared with the auto storage class (which is the
default for local variables) have a limited lifetime, existing only during the function's
execution in which they are declared. They are created when the function is called
and destroyed when the function exits.

 Scope of Variables

The scope of a variable refers to the region of the program in which the variable is
accessible or visible.

 Local Variables: These are variables declared inside a function or block and are only
accessible within that function/block. By default, local variables have the auto
storage class, meaning they are automatically created when the function is called
and destroyed when the function exits.

 Global Variables: These are declared outside of any function, usually at the top of a
file, and they can be accessed by any function throughout the program. Global

9
variables have a longer lifetime as they are created when the program starts and
destroyed when the program terminates.
 Static Variables: Static variables have local scope (they can be accessed only within
the function or block where they are declared) but a longer lifetime, as they retain
their value between function calls.

 Extern Variables: These variables are declared outside a function but can be used by
any function within the program. They are typically used to declare global variables
in header files and share them across multiple source files.

 Global Variables

 Global variables are declared outside of all functions, typically at the top of the
program. These variables are accessible by all functions in the program, making
them useful for sharing information across different parts of the program. The
lifetime of a global variable is the entire duration of the program’s execution.
However, overuse of global variables can lead to problems like lack of encapsulation
and difficulty in maintaining code.
For example, a global variable might be declared as:

 Register Specifier

The register specifier is used to suggest to the compiler that a variable should be
stored in a CPU register rather than RAM, for faster access. However, it is only a
hint to the compiler, and the compiler may choose to ignore it based on the
availability of registers.

10
Variables declared with the register keyword have automatic storage class and local
scope. They are typically used for loop counters or frequently accessed variables
where speed is crucial.

For example:

 Dynamic Memory Allocation

Dynamic memory allocation refers to the process of allocating memory during


runtime using specific functions from the C standard library (e.g., malloc(), calloc(),
realloc(), free()). This memory is allocated from the heap, and the programmer has
control over the allocation and deallocation of this memory.

Heap Memory: Unlike stack memory (used for automatic variables), heap memory
remains allocated until explicitly freed, regardless of function calls or scope.

Functions for Dynamic Memory:

Malloc(size_t size) – Allocates a specified number of bytes and returns a pointer to


the allocated memory.
Calloc(size_t num, size_t size) – Allocates memory for an array of num elements of a
specified size and initializes the memory to zero.
Realloc(void* ptr, size_t size) – Resizes previously allocated memory.
Free(void* ptr) – Frees dynamically allocated memory.
For example, dynamic memory allocation can be used to create arrays whose size is
determined at runtime:

11
Dynamic memory allocation is critical in programs where the size of data structures
cannot be determined at compile-time, allowing for more flexible and efficient use
of memory.

Conclusion
Storage classes in C/C++ are a powerful mechanism for controlling the lifetime,
scope, and visibility of variables in a program. The automatic declaration is primarily
used for local variables with temporary storage, while fixed declarations like static
and extern are used for variables that need longer lifetimes or global visibility. The
register specifier provides a hint to store variables in CPU registers for fast access.
Dynamic memory allocation allows for the creation of memory blocks at runtime,
providing flexibility for more complex data management. Understanding and using
the correct storage class is crucial for efficient memory management and program
behavior.

12

You might also like