CPP 5 Mark
CPP 5 Mark
1.Create a class Employee with data members empId, age, and salary. Write a
program to initialize the data members inside the main function and display them
using a member function definition outside the class.
#include <iostream>
class Employee {
public:
int empId;
int age;
double salary;
void display() {
};
int main() {
Employee emp1;
emp1.empId = 101;
emp1.age = 30;
emp1.salary = 50000.50;
emp1.display();
return 0;
2. Apply the concept of OOP to read three sides of a triangle. Calculate and display
the area of the triangle.
#include <iostream>
#include <cmath>
class Triangle {
public:
double area() {
};
int main() {
Triangle t;
cout << "Area of the triangle: " << t.area() << endl;
return 0;
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
};
class LinkedList {
public:
Node* head;
LinkedList() {
head = nullptr;
if (head == nullptr) {
} else {
temp = temp->next;
temp->next = newNode;
void display() {
temp = temp->next;
};
int main() {
LinkedList list;
list.insert(10);
list.insert(20);
list.insert(30);
list.display();
return 0;
These features enable OOP to create more flexible, scalable, and easier-to-maintain systems.
They help in managing the complexity of large systems by providing a way to organize and
structure the code effectively.
#include <iostream>
if (arr[i] == key) {
int main() {
if (index != -1) {
cout << "Element found at index: " << index << endl;
} else {
return 0;
#include <iostream>
class Queue {
private:
Join my community : https://chat.whatsapp.com/K0fZdK8oYOTBJNulvKlbwU
Important question by MrDnobody
int arr[5];
public:
Queue() {
front = -1;
rear = -1;
if (rear == 4) {
} else {
arr[++rear] = val;
void deleteElement() {
} else {
void display() {
} else {
};
int main() {
Queue q;
do {
cout << "\nMenu: \n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ";
switch (choice) {
q.insert(value);
break;
case 2:
q.deleteElement();
break;
case 3:
q.display();
break;
case 4:
break;
default:
return 0;
#include <iostream>
private:
int arr[5];
int top;
public:
Stack() {
top = -1;
// Push operation
if (top == 4) {
} else {
arr[++top] = val;
// Pop operation
void pop() {
if (top == -1) {
bool isEmpty() {
bool isFull() {
return top == 4;
void display() {
if (top == -1) {
} else {
};
int main() {
Stack s;
do {
cout << "\nMenu: \n1. Push\n2. Pop\n3. Display\n4. Check if Empty\n5. Check if
Full\n6. Exit\nEnter your choice: ";
switch (choice) {
case 1:
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
cout << (s.isEmpty() ? "Stack is empty" : "Stack is not empty") << endl;
break;
case 5:
cout << (s.isFull() ? "Stack is full" : "Stack is not full") << endl;
break;
case 6:
break;
default:
return 0;
In C++, constructors are special member functions that are invoked automatically when an
object of a class is created. The primary purpose of a constructor is to initialize the data
members of a new object. There are several types of constructors, each serving a specific
purpose in initializing objects in different ways:
1. Default Constructor:
A default constructor is a constructor that takes no arguments and provides default
values for the data members of the object. It is automatically called when an object is
created without any arguments
class Rectangle {
public:
Rectangle() {
length = 5;
width = 10;
};
int main() {
cout << "Length: " << rect.length << ", Width: " << rect.width << endl;
return 0;
}
2. A parameterized constructor accepts arguments when an object is
created. This allows the user to initialize data members with specific
values at the time of object creation.
class Rectangle {
public:
// Parameterized constructor
Rectangle(int l, int w) {
length = l;
width = w;
};
int main() {
cout << "Length: " << rect.length << ", Width: " << rect.width << endl;
return 0;
class Rectangle {
public:
// Parameterized constructor
Rectangle(int l, int w) {
length = l;
width = w;
// Copy constructor
length = rect.length;
width = rect.width;
};
int main() {
cout << "Length: " << rect2.length << ", Width: " << rect2.width << endl;
class Array {
public:
int* arr;
int size;
// Dynamic constructor
Array(int s) {
size = s;
~Array() {
delete[] arr;
};
int main() {
arr1.arr[i] = i + 1;
return 0;
Dynamic memory allocation is a process where memory is allocated during the program's
runtime, rather than at compile-time. In C++, this is done using
the new and delete operators. Dynamic memory allocation is useful when the size of the
memory required is not known in advance or changes during program execution.
The new operator is used to allocate memory for a single variable or an array on the heap.
The delete operator is used to free up the memory allocated using new to avoid memory
leaks.
#include <iostream>
int main() {
int* ptr;
int n;
cin >> n;
ptr[i] = i + 1;
delete[] ptr;
return 0;
In the above example, memory for the integer array is allocated dynamically using
the new operator. After using the array, the delete[] operator frees the memory to avoid
memory leaks. Dynamic memory allocation allows you to create arrays of any size, even
when the size isn't known beforehand.
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The last
element added to the stack is the first one to be removed. Operations performed on a stack
include:
#include <iostream>
class Stack {
private:
int arr[5];
int top;
public:
Stack() {
top = -1;
// Push operation
if (top == 4) {
} else {
arr[++top] = value;
// Pop operation
void pop() {
} else {
void peek() {
if (top == -1) {
} else {
bool isEmpty() {
bool isFull() {
return top == 4;
void display() {
if (top == -1) {
} else {
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.display();
s.pop();
s.peek();
s.display();
return 0;
Function Overloading occurs when multiple functions with the same name exist
but differ in the number or type of parameters.
Operator Overloading allows existing operators to be used in new ways for user-
defined classes.
class Example {
public:
void display(int i) {
void display(double d) {
};
12.Create a class to calculate the area of a rectangle and square, using method
overloading.
#include <iostream>
class Shape {
public:
};
int main() {
Shape shape;
return 0;
#include <iostream>
class Complex {
private:
public:
// Constructor
void display() {
cout << real << " + " << imag << "i" << endl;
};
int main() {
c3.display(); // Output: 4 + 6i
return 0;