[go: up one dir, main page]

0% found this document useful (0 votes)
129 views4 pages

Assignment of OOP: Solutions

The document contains solutions to 7 questions on Object Oriented Programming concepts. It explains the differences between references and pointers, discusses the output of two code snippets involving exceptions and inheritance, explains when to use virtual inheritance, compares classes and structs, discusses memory allocation using malloc and new, and provides a C++ program to store and print the CGPA of n students.

Uploaded by

freaky man
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)
129 views4 pages

Assignment of OOP: Solutions

The document contains solutions to 7 questions on Object Oriented Programming concepts. It explains the differences between references and pointers, discusses the output of two code snippets involving exceptions and inheritance, explains when to use virtual inheritance, compares classes and structs, discusses memory allocation using malloc and new, and provides a C++ program to store and print the CGPA of n students.

Uploaded by

freaky man
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/ 4

❖ Assignment of OOP

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,

the address values cannot be reassigned.

2.What is output of following code:-


PROGRAM:

#include <iostream>

class A {

public:

A() {}

~A() {

throw 30;

};

int main(int argc, const char * argv[])

try

A a;

throw 20;

catch(int a)

std::cout << a;

}
}

OUTPUT :

terminate called after throwing an instance of 'int'

3.What is the output of following code:-


PROGRAM

#include <iostream>

struct A

int data[2];

A(int x, int y) :

data{x, y} {}

virtual void f() {}

};

int main(int argc, char **argv)

{ A a(22, 45);

int *arr = (int *) &a;

std::cout << arr[2] << std::endl; return 0;

OUTPUT:

45

4.When you should use virtual inheritance?


ANS:- Virtual inheritance should be used if multiple inheritances are done. In case virtual inheritance
is not used then if two classes B and C inherit from class A, and class D inherits from both B and C,

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

contain only one set of the members from class A.

5.Is there a difference in class and struct? Explain.


ANS:- Yes, there are differences between class and structure. The structure is a user-defined data
type that can store different types of data whereas class is a blueprint that defines data and
methods to create objects. The class also provides features like inheritance, constructor/destructor,

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++:-

(a) Static Memory Allocation

(b) Dynamic Memory Allocation

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

but is faster in execution than Dynamic Memory Allocation.

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

amount of memory required by him.

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*.

7.Write a C++ program to store CGPA of n number of students:-


Program to Store CGPA of n students :

#include<iostream>

using namespace std;

int main()

int no_of_student , index ;

cout << "ENTER NUMBER OF STUDENTS" << endl ;

cin >> no_of_student;

float *student_cgpa = new float[no_of_student ];

cout << "Enter CGPA of Each student one by one" << endl;

for ( int i=0 ; i<no_of_student ; i++ )

cin >> student_cgpa[i];

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

You might also like