Lab01_DS
Lab01_DS
LAB MANUAL - 01
Course Instructor: Lecturer Anum Abdul Salam
Total 10
Lab Description:
A pointer is a variable whose value is the address of another variable. Like any variable or
constant, you must declare a pointer before you can work with it.type *var-name; There are few
important operations performed when using pointers are
Defining a Pointer Variable Pointer Variables To access num using iptr and
Assignment: indirection operator *
Value of memory location towards which pointer is pointing can be changed using pointers
*ptr_nam=val;For example *iptr=8;
It is always a good practice to assign the pointer NULL to a pointer variable in case you do not
have exact address to be assigned.
Example
void main () {
int *ptr=NULL;
If all unused pointers are given the null value you can avoid the accidental misuse of an
uninitialized pointer. Many times, uninitialized variables hold some junk values and it becomes
difficult to debug the program.
1. A pointer stores a reference to its pointee. The pointee, in turn, stores something useful.
2. The dereference operation on a pointer accesses its pointee. A pointer may only be
dereferenced after it has been assigned to refer to a pointee. Most pointer bugs involve
violating this one rule.
3. Allocating a pointer does not automatically assign it to refer to a pointee. Assigning the
pointer to refer to a specific pointee is a separate operation which is easy to forget.
4. Assignment between two pointers makes them refer to the same pointee which introduces
sharing.
In the static memory allocation, the amount of memory to be allocated is predicted and pre
known Memory is allocated during the compilation itself. All the declared variables declared
normally, are allocated memory statically.
Example
cout<<sqr(a); }
}
Variables declared and used locally inside a function definition are destroyed, when declared
variables within a function scope are needed to be used by other functions without creating an
overhead by copying these variables via function's return value.
void main { {
{ int*p; int*p;
} } }
Constructors
A constructor in C++ is a special function having the same name as that of its class which is
used to initialize some valid values to the data members of an object. It is executed automatically
whenever an object of a class is created. The only restriction that applies to the constructor is that
it must not have a return type or void. It is because the constructor is automatically called by the
compiler, and it is normally used to initialize values. The compiler distinguishes the constructor
from other functions of a class by its name which is the same as that of its class
Properties of constructors
Example
class Number{
public:
int firstNumber;
int secondNumber;
Number( ){
void displayMethod( ){
}
};
int main( ){
// driver code
Types of Constructors
1. Default
2. Parametrized
3. Copy
1. Copy Constructor
A copy constructor is a function that initializes an object using another object of the same class.
In simple terms, a constructor which creates an object by initializing it with an object of the same
class, which has been created previously is known as a copy constructor. Copy constructor takes
a reference to an object of the same class as an argument.
Copy Constructor
class Integer {
public:
int x, y;
x = a;
y = b;
x=p.x;
y=p.y;
void displayMethod( ){
};
int main( ){
Integer i1(15,20);
i2.displayMethod( );
i3.displayMethod( );
return 0;
Destructor
Dangling Pointers:
The delete operator does not delete the pointer it takes the memory being pointed to and returns
it to the heap. It does not even change the contents of the pointer. Since the memory being
pointed to is no longer available (and may even be given to another application), such a pointer is
said to be dangling.
LAB TASKS:
int main()
{
IntStaticPointer b;
int z=8;
b.AllocMem(z);
cout<<b.getValue();
mystery(b);
cout<<b.getValue();
}
Identify how the given code has logical errors. Can you correct them?
3. Design a class to represent a to-do list, where each task is dynamically added or
removed. A task should include task id and deadline.
Provide the following:
A default constructor to initialize an empty to-do list.
A parameterized constructor to initialize a to-do list with an initial
task.
A copy constructor to create a deep copy of a to-do list.
A destructor to release allocated memory.
Functions to be performed
AddTask: Add a new task to the list.
RemoveTask: Remove a task by name or index.
DisplayTasks: Display all tasks in the list.
THINK!!!