11. Create a class to represent a student with methods to set and get the student’s name and ID.
Required Input:
Student s; s.setName("John Doe"); s.setID(123);
Expected Output:
Student Name: John Doe, ID: 123
Code In Cpp
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int id;
public:
void setName(std::string n);
void setID(int i);
void display();
};
int main() {
// Logic to demonstrate Student functionality
return 0;
}
Run Code?
Click Run Button to view compiled output
12. Implement a program to sort an array of integers using the quicksort algorithm.
Required Input:
int arr[] = {5, 3, 8, 6, 2};
Expected Output:
Sorted array: 2 3 5 6 8
Code In Cpp
#include <iostream>
void quicksort(int arr[], int low, int high);
int main() {
// Logic to demonstrate quicksort
return 0;
}
Run Code?
Click Run Button to view compiled output
13. Write a program to demonstrate the use of templates by creating a simple generic stack.
Required Input:
Stack<int> intStack; intStack.push(10);
Expected Output:
Popped element: 10
Code In Cpp
#include <iostream>
template <typename T> class Stack {
private:
T *arr;
int top;
int capacity;
public:
Stack();
void push(T x);
T pop();
};
int main() {
// Logic to demonstrate template usage
return 0;
}
Run Code?
Click Run Button to view compiled output
14. Create a program that uses polymorphism to handle different shapes and calculate their areas.
Required Input:
Shape* shapes[2]; shapes[0] = new Circle(3); shapes[1] = new Rectangle(5, 10);
Expected Output:
Area of circle: 28.2743
Area of rectangle: 50
Code In Cpp
#include <cmath>
#include <iostream>
class Shape {
public:
virtual double area() = 0; // Pure virtual function
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r);
double area();
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h);
double area();
};
int main() {
// Logic to demonstrate polymorphism
return 0;
}
Run Code?
Click Run Button to view compiled output
15. Implement a program to calculate the factorial of a number using recursion.
Required Input:
int n = 5;
Expected Output:
Factorial of 5: 120
Code In Cpp
#include <iostream>
int factorial(int n);
int main() {
// Logic to demonstrate factorial calculation
return 0;
}
Run Code?
Click Run Button to view compiled output
16. Write a program to merge two sorted arrays into a single sorted array.
Required Input:
int arr1[] = {1, 3, 5}; int arr2[] = {2, 4, 6};
Expected Output:
Merged array: 1 2 3 4 5 6
Code In Cpp
#include <iostream>
void merge(int arr1[], int size1, int arr2[], int size2, int merged[]);
int main() {
// Logic to demonstrate merging arrays
return 0;
}
Run Code?
Click Run Button to view compiled output
17. Create a program to demonstrate the use of namespaces by defining a simple mathematical functions namespace.
Required Input:
using namespace Math;
Expected Output:
Sum: 15
Code In Cpp
#include <iostream>
namespace Math {
int add(int a, int b);
}
int main() {
// Logic to demonstrate namespace usage
return 0;
}
Run Code?
Click Run Button to view compiled output
18. Write a program to demonstrate function overloading by creating multiple versions of a print function.
Required Input:
print(5); print(5.5);
Expected Output:
Integer: 5
Double: 5.5
Code In Cpp
#include <iostream>
void print(int value);
void print(double value);
int main() {
// Logic to demonstrate function overloading
return 0;
}
Run Code?
Click Run Button to view compiled output
19. Create a class that represents a simple queue using an array with methods for enqueue and dequeue.
Required Input:
Queue q; q.enqueue(10); q.enqueue(20);
Expected Output:
Dequeued element: 10
Code In Cpp
#include <iostream>
#define MAX 100
class Queue {
private:
int arr[MAX];
int front, rear;
public:
Queue();
void enqueue(int x);
int dequeue();
};
int main() {
// Logic to demonstrate Queue functionality
return 0;
}
Run Code?
Click Run Button to view compiled output
20. Implement a program to calculate the Fibonacci sequence up to a given number using iteration.
Required Input:
int n = 10;
Expected Output:
Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34
Code In Cpp
#include <iostream>
void fibonacci(int n);
int main() {
// Logic to demonstrate Fibonacci calculation
return 0;
}
Run Code?
Click Run Button to view compiled output