C CPP Notes
C CPP Notes
26:03:2024
Recap:
Activation: Creation of an object.
Binding: Assigning values to fields and accessing them using
the dot operator.
SOLID Principles:
1. Single Responsibility Principle (SRP):
Objective: One reason for changing functionality per object.
Rule: Define members in a class required by all instances
identically.
Example: A universal commission calculation function must
not include specific department details.
2. Open Closed Principle (OCP):
Objective: Object functionality should be open for extension,
closed for modification.
Rule: Utilize class methods for interaction, avoiding direct
field manipulation.
Example: Interaction with class instances limited to specified
methods.
Examples:
Default Constructor:
Initializes object fields.
If constructor with parameters exists, define a default
constructor to ensure its presence.
Const Methods:
Methods not altering object state.
Use 'const' as an optional flag.
Modification of fields in const methods results in error.
'this' Concept:
o Implicitly passes instance address to methods.
o Compiler transforms method definitions to include
'this' argument.
Enums:
Flag containers (e.g., yes/no).
Automatically assigns values from 0 to n.
Classes Modification Guidelines:
Open for addition, closed for modification.
Public declarations immutable, inner code modifiable.
Private function declarations can change.
Destructor:
Called before instance deactivation.
Removes constructor side effects.
Best Practices:
Avoid passing objects by value to prevent unnecessary
copying.
Object copying consumes memory and leads to extra
destructor calls.
Given Notes:
Modularization: Dividing the implementation of a large software into smaller
sets of related functions known as modules is called modularization.
A module can publish a subset of its functions so that they can be called
from external modules.
Modularization can enable multiple programmers to simultaneously and
independently implement, test and maintain different parts of a software
provided it is performed according to following rules
1. Functions whose internal implementations depend upon each other
should be defined within the same module.
2. A module should only share the immutable information required to
call its published functions with external modules.
Data Abstraction: It is a modularization methodology (systamatic style)
in which the data processed by a program is divided into smaller blocks
known as objects so that each object controls its own state (data) and
supports its own behaviour (code).
A class defines a set of members for a particular type of object and
these members include fields (variables) whose values indicate the state
of that object and methods (functions) whose implementation describe the
behaviour of the that object. It provides support for following mechanisms
SOLID Principles
1. Single Responsibility Principle (SRP): There should be exactly one reason
for changing the functionality supported by an object.
Rule: Define only members in a class which are required by all of its
instances in an identical manner.
2. Open Closed Principle (OCP): The functionality exposed by an object should
be open for extension but closed for modification.
Rule: Use a class only by calling methods defined by that class on its
instances.
Class Banner:{
Constructor:
Purpose: Initializes each new instance of the class.
Definition: A function with the same name as the class, declared without a return type.
Default Constructor: Declared without parameters or with all optional parameters.
Automatically defined if no constructor is explicitly defined.
Constructor:
Banner()
{
width = 20;
height = 5;
triangular = false;
}
Methods:
Resize():
Triangulate():
Deconstructor:
~Banner()
{
puts("Banner instance deactivated");
}
Private Members:
width, height: Dimensions of the banner.
triangular: Indicates whether the banner is triangular or not.
}
Notes:
1. When you call a function from through an object it gets called like:
Example:
Banner b;
b.Resize(x,y)
->how compiler convert it implicitly
2. A const method have only read-only privilege for the state of the object
27-03-2024
Static Member Function:
Definition: Function not dependent on instance data; denoted by placing
"static" before the data type.
Characteristics:
o Marked with "static" to denote independence from instance data.
o Doesn't require the "this" argument.
o Can be called directly on the class using the " :: " operator.
Stack Memory:
o Space for function-specific memory.
o Freed upon function return.
o Limited size (checked using "ulimit -s").
o Risk of stack overflow with large objects (>8192 kb).
new Operator:
Dynamically allocates memory on the heap.
Returns the address of the allocated memory.
Deallocating Heap Memory:
Must use delete operator after use (delete obj_name).
Example: 1 initialized with heap