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

C CPP Notes

Uploaded by

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

C CPP Notes

Uploaded by

Backroom Hero
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C/C++ 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

1. Activation - Each object refers to a block of memory known as the


instance of its class and this block contains separate values of
the fields defined by that class initialized by calling a special
method known as the constructor of that class.

2. Binding - Every instance has unique identity (address) and when a


method defined by a class is called on its object the identity of
the instance it refers to is passed (as this argument) to the
implementation of that method.

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():

 Purpose: Adjusts the dimensions of the banner.


 Operation: Receives a 'this' argument pointing to the instance referred by the object.
Changes the width and height accordingly.
 Definition:
void Resize(float w, float h)
{
width = w;
height = h;
}

Triangulate():

 Purpose: Sets the triangular property of the banner.


 Operation: Modifies the triangular property based on the parameter 'yes'.
 Definition:
void Triangulate(bool yes)
{
triangular = yes;
}
Price():

 Purpose: Calculates the price of the banner.


 Operation: Treats the instance addressed by the 'this' argument as read-only.
Calculates the price based on width, height, and triangular properties.
 Definition:
double Price() const
{
float k = triangular ? 0.5 : 1.0;
float rate = width <= height ? 0.75 : 0.80;
return k * width * height * rate;
}

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

Banner :: Resize(Banner* this, float w,float h)


->compiler passes the address of the object which is the address of the instance to `this`
parameter

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.

No Static (Instance Member Function):


Definition: Functions dependent on instance data, accessed using the "this"
argument.
Characteristics:
 Must be called on an object using the " . " operator.

Memory Allocation - are two types:


Every program call receives a portion in memory.

 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).

 Heap Memory Allocation:


o Activated using the "new" operator.
o Example: Banner* b = new Banner(30,8).
o Pointer in stack points to object in heap memory.

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

Banner* b = new Banner(30,8); //activating object on the heap

Example: 2: Created an array of n objects

Banner *other = new Banner[n]; //default constructor is going to be used

Automatic Memory Allocation:


 Banner a; allocates memory on the stack.
 Deactivated automatically when out of scope.
 Explicit Memory Deallocation:
 Banner b; requires explicit deletion to prevent memory leaks.

Local Object Activation and Deactivation:


 Banner a; is activated on the stack using the default constructor.
 Accessed using a.Price() which you can use only with stack objects
 Deactivated automatically when the scope ends {//code//}.

Heap Object Activation and Deactivation:


 Banner* b = new Banner(30, 8); activates an object on the heap.
 Accessed using b->Price() or with b[0].Price()
 Deallocated using delete b;.
Dynamic Array Allocation and Deallocation:
 Dynamically allocates an array of Banner objects on the heap using new[].
 Accessed and modified in a loop.
 Deleted using delete[] array_name; to deallocate the entire array.

Memory Management Notes:


Deleting other without [] would only delete the first instance, leading to a memory leak.

Hence, delete[] other; is used to deallocate the entire array.

You might also like