0% found this document useful (0 votes)
64 views6 pages

Tutorial Object Oriented Programming Using C++ CST 157

The document is a tutorial on object oriented programming using C++ that provides answers to 5 questions about dynamic memory allocation, the differences between malloc() and new, advantages and disadvantages of pointers, dynamically allocating memory to different data types and initializing values at compile time vs runtime, and storing student details like name, UID, marks in subjects for multiple students using classes and dynamic memory allocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views6 pages

Tutorial Object Oriented Programming Using C++ CST 157

The document is a tutorial on object oriented programming using C++ that provides answers to 5 questions about dynamic memory allocation, the differences between malloc() and new, advantages and disadvantages of pointers, dynamically allocating memory to different data types and initializing values at compile time vs runtime, and storing student details like name, UID, marks in subjects for multiple students using classes and dynamic memory allocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tutorial

Object Oriented Programming Using C++


CST 157

Name: Bhuvan chandra UID: 19BCS2921


Class & Section: CSE 27 B Date: 12 April,2020

Q1. What are the advantages of dynamic memory allocation over the
compile time memory allocation?

Answer: ​The benefits of dynamic memory allocation over compile time


memory allocation are as follows:

1. The memory allocated is precisely the amount we need, so the space


complexity is under control.
2. The size of data structures, like array can be changed during run time
as per requirements.
3. The allocated memory could be de-allocated when the need is over,
however it's a double edged sword.
4. Dynamic memory allocation paves the path for many new data
structures like linked list, stacks and queue.

Q2. What are the differences between malloc() and new?

Answer: ​Both malloc() and new are used to dynamically allocate variables,
however the two also show some differences:

new malloc()
1. new is an operator. 1. malloc() is a function.
2. new keyword could be used 2. malloc() is used to
to call constructors. It could dynamically allocate memory
dynamically declare objects only to primitive data types,
as well as primitive data i.e., int, float, char, etc.
types.
3. new returns exact data type of 3. malloc() returns void data
the pointer it is allocating. No type and needs typecasting to
need for typecast. decide the return type.
4. Required size of memory is 4. We need to calculate the
calculated by compiler for required size of data structure
new. to call malloc.
5. It never returns NULL, even in 5. It returns NULL in case of
case of fault, it throws. fault.

Q3. What are the advantages and disadvantages of using pointers?

Answer: ​Using pointers in a program can have some benefits, and some
drawbacks as well. Some of the advantages and disadvantages of using
pointers are discussed below:

Advantages

● Pointers provide direct access to memory


● Pointers provide a way to return more than one value to the functions
● Reduces the storage space and complexity of the program
● Reduces the execution time of the program
● Provides an alternate way to access array elements
● Pointers can be used to pass information back and forth between the
calling function and called function.
● Pointers allows us to perform dynamic memory allocation and
deallocation.
● Pointers helps us to build complex data structures like linked list,
stack, queues, trees, graphs etc.
● Pointers allows us to resize the dynamically allocated memory block.
● Addresses of objects can be extracted using pointers

Disadvantages

● Uninitialized pointers might cause segmentation fault.


● Dynamically allocated block needs to be freed explicitly. Otherwise, it
would lead to memory leak.
● Pointers are slower than normal variables.
● If pointers are updated with incorrect values, it might lead to memory
corruption.
● Pointer bugs are difficult to debug.

Q4. Write a program to dynamically allocate memory to integer, float,


and character type of data. Initialise the value of float during the
compile time, and the value of integer and character must be entered
during the execution time.

Answer:

#include<iostream>
using namespace std;
int main()
{
int *ptri = new int;
float *ptrf = new float(19.79);
char *ptrc = new char;
cout<<"Enter an integer : ";
cin>>*ptri;
cout<<"Enter an alphabet : ";
cin>>*ptrc;
cout<<"\nInteger :"<<*ptri<<"\nFloat :"<<*ptrf<<"\nCharacter :"<<*ptrc;
delete ptri, ptrf, ptrc;
return 0;
}

Output

Q5. Write a program to store name,UID and marks in 5 subjects for n


number of students using the concept of classes and dynamic
memory allocation. (allocate memory to n number of objects using
new and delete the same using delete [])

Answer:

#include<iostream>
using namespace std;
class student
{
string name;
string uid;
float marks[5];
public:
void input()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter UID:";
cin>>uid;
cout<<"Enter marks for 5 subjects:\n";
for(int i=0;i<5;i++)
cin>>marks[i];
}
void output()
{
cout<<"\nName:"<<name<<"\nUID:"<<uid<<"\nMarks:\t";
for(int i=0;i<5;i++)
cout<<marks[i]<<" ";
}
};
int main()
{
int n;
cout<<"How many students' details you want to enter? \n";
cin>>n;
student *ptr = new student[n];
for(int i=0;i<n;i++)
{
ptr[i].input();
}
cout<<"\nDetails entered are as follows:";
for(int i=0;i<n;i++)
{
ptr[i].output();
}
delete []ptr;
return 0;
}

You might also like