Oops Unit - 2
Oops Unit - 2
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.
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()).
Data Types
Data types define the type of data a variable can hold. There are two categories:
Operators
Operators perform operations on variables and values. Common operators include:
2
Expressions
An expression is a combination of variables, constants, operators, and functions
that are evaluated to produce a value.
For example:
Control Structures
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++:
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:
5
Constructors
Destructors
6
Operator Overloading
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++
8
By combining all of these elements, Object-Oriented Programming allows you to
create complex systems with reusable and modular components.
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.
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:
Heap Memory: Unlike stack memory (used for automatic variables), heap memory
remains allocated until explicitly freed, regardless of function calls or scope.
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