CPP SP Solutions
CPP SP Solutions
Ans : A class in C++ is a blueprint for creating objects with related data
and functions. It encapsulates data (variables) and functions (methods)
into a single unit.
Access specifiers (public, private, and protected) control the visibility and
accessibility of class members. They ensure encapsulation, which means
hiding the internal implementation details of a class from outside
interference.
2 Describe the difference between linear and non-linear data structures. CO2
Provide an example of each.
Ans : The difference between linear and non-linear data structures are -
3 Explain how a stack functions and provide an example use case within CO4
a program.
The flexibility and efficiency of linked lists make them suitable for
various applications, especially when the size of the data structure is
unpredictable or when frequent insertion and deletion operations are
expected.
5 What is the purpose of a circular queue, and how does it differ from a CO5
linear queue?
Section B
4 x 5 = 20 marks
6 Write a program in C++ demonstrating the use of constructors and CO1
destructors. Explain their importance.
class Example {
private:
int data;
public:
// Constructor
Example(int value) {
data = value;
cout << "Constructor called with value: " << value << endl;
}
// Destructor
~Example() {
cout << "Destructor called with value: " << data << endl;
}
int main() {
// Creating an object and invoking constructor
Example obj(10);
while(left<=right){
int mid=left + (right-left)/2;
if(arr[mid]==target){
return mid;
}else if(arr[mid]<target){
left=mid+1;
}else{
right=mid-1;
}
}
return -1;
}
int main(){
int n;
cout<<"Enter number of elements : ";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cout<<"Enter element "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\nArray created. The elements are : "<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
sort(a,a+n);
int key;
cout<<"\nEnter element to be searched : ";
cin>>key;
int result=binary(a,n,key);
if(result!=-1){
cout<<"Element found at position "<<result+1<<endl;
}else{
cout<<"Element not found in array"<<endl;
}
return 0;
}
Code explanation :
• The code defines a function binary for performing binary search on a
sorted array.
• Inside the binary function, it initializes two pointers left and right to
the start and end of the array respectively.
• It iterates through the array using a while loop, updating left and right
pointers based on whether the middle element is less than or greater
than the target element.
• If the target element is found, the function returns its index;
otherwise, it returns -1 to indicate that the element is not present in
the array.
• In the main function, the user inputs the number of elements and then
enters each element of the array.
• The array is displayed after creation.
• The array is sorted using the sort function from the <algorithm>
library.
• The user inputs a key element to be searched in the array.
• The binary function is called to search for the key element in the
sorted array.
• If the key element is found, its position in the array is displayed;
otherwise, a message indicating that the element is not found is
printed.
Evaluation Process:
1. The postfix notation eliminates the need for parentheses and operator
precedence rules.
2. It relies on the order of operators and operands.
As a result, postfix expressions can be evaluated efficiently using a stack.
Stack Operations:
1. A stack is used to store operands.
2. When an operator is encountered, the required number of operands are
popped from the stack, the operation is performed, and the result is pushed
back onto the stack.
3. This process continues until the entire expression is scanned.
Ans : The different types of queues are explained and compared in the
table below -
Section C
3 x 10 = 30 marks
10 Explain the different modes of inheritance in C++. Provide a code CO2
example demonstrating single inheritance.
Ans :
In C++, inheritance is a mechanism by which a class (derived class) can
inherit properties and behaviors (member variables and functions) from
another class (base class). There are different modes of inheritance,
including:
• Single Inheritance: A derived class inherits from only one base class.
• Multiple Inheritance: A derived class inherits from more than one
base class.
• Multilevel Inheritance: A derived class inherits from another derived
class, creating a hierarchy of inheritance.
• Hierarchical Inheritance: Multiple derived classes inherit from a
single base class.
• Hybrid Inheritance: Combination of multiple inheritance types.
// Base class
class Animal {
public:
void eat() {
std::cout << "Animal is eating" << std::endl;
}
};
int main() {
// Create an object of the derived class
Dog dog;
return 0;
}
11 Compare and contrast different sorting algorithms (e.g., bubble sort, CO3
insertion sort, selection sort). Discuss the advantages and
disadvantages of any one of the sorting techniques and write their
algorithm as well.
Ans : The comparison of the different sorting algorithms is presented
below -
12 Explain the different types of linked lists (singly, doubly, circular) and CO4
discuss their features.
#include<iostream>
using namespace std;
int main(){
int n,el,p;
cout<<"Enter number of elements : ";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cout<<"Enter element "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\nArray created. The elements are : "<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
--------