Assignment of OOP: Solutions
Assignment of OOP: Solutions
Solutions:
1.What is difference b/w reference and pointer?
ANS :-The basic difference among pointer and reference is that a pointer variable points to a variable
whose memory location is stored in it whereas the reference variable is an alias for a variable
that is assigned to it. Moreover, a pointer is an independent variable that can be reassigned to
refer to different objects whereas reference must be assigned at initialization and once created,
#include <iostream>
class A {
public:
A() {}
~A() {
throw 30;
};
try
A a;
throw 20;
catch(int a)
std::cout << a;
}
}
OUTPUT :
#include <iostream>
struct A
int data[2];
A(int x, int y) :
data{x, y} {}
};
{ A a(22, 45);
OUTPUT:
45
then D will get two copies of A's members: one through B, and one through C. Instead, if virtual
inheritance is used then classes B and C inherit virtually from class A and objects of class D will
friend function, polymorphism and many more which are not available in case of Structure.
6. Howdo you allocatememory in c++? What’s the difference b/w
malloc and new?
ANS :- There are two ways to allocate memory on c++:-
In Static Memory Allocation, memory is allocated before the execution of the program and
allocated memory size can't be changed during the execution. This leads to wastage of memory
Whereas, in Dynamic Memory Allocation , memory allocation and deallocation are done during
the execution of the program which saves a lot a memory as the user can allocate the exact
Malloc() is a function that is used for memory allocation in C whereas New is an operator that is
used to allocate memory in C++. New returns exact data type whereas Malloc() returns void*.
#include<iostream>
int main()
cout << "Enter CGPA of Each student one by one" << endl;
cout << "Enter the number of student to print his/her CGPA" << endl;
cin >>index ;
cout << student_cgpa[index-1] << endl;
Submitted By:
PARDEEP
C12
11801033