Priyanshioops 1
Priyanshioops 1
BTCSE 508
B.Tech (CSE) 5th Semester, 3rd Year
Sec – ‘C’
Submitted By - Submitted To -
PRIYANSHI THAPA DR. AAINA
2021-310-176 (Assistant Professor)
Jamia Hamdard
School Of Engineering Sciences & Technology
Department Of Computer Science & Engineering
New Delhi-110062
Page 1 of 36
S No. LIST OF PROGRAM Date Remarks
1 Write a program to swap two number without using
third variable in C++.
2 Write a program to print n largest number from an array
where n is user input.
3 Write a Program to implement the concept of function
overloading to overload the function name area to
calculate the area of following figure.
4 Write a Program to implement the concept of function
overwriting.
5 Write a Program to implement the function of inheritance.
6 Write a Program to overload constructor..
INDEX
Page 2 of 36
PROGRAM NO-1
class Swapper {
private:
int num1;
int num2;
public:
Swapper(int n1, int n2) : num1(n1), num2(n2) {}
void swapNumbers() {
int temp = num1;
num1 = num2;
num2 = temp;
}
void displayNumbers() {
cout << "Number 1: " << num1 << endl;
cout << "Number 2: " << num2 << endl;
}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
Swapper numSwapper(5, 10);
cout << "Before swapping:" << endl;
numSwapper.displayNumbers();
numSwapper.swapNumbers();
cout << "\nAfter swapping:" << endl;
numSwapper.displayNumbers();
return 0;
}
Page 3 of 36
OUTPUT:
PROGRAM NO-2
Page 4 of 36
AIM:Write a program to print nth largest number from an array where n is user
Input
#include <iostream>
#include <algorithm>
using namespace std;
class NthLargestFinder {
private:
int* array;
int size;
public:
NthLargestFinder(int* arr, int s) : array(arr), size(s) {}
int findNthLargest(int n) {
if (n > 0 && n <= size) {
std::sort(array, array + size, greater<int>());
return array[n - 1];
} else {
return -1;
}
}
};
int main() {
int size, n;
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
cout << "Enter the size of the array: ";
cin >> size;
int* arr = new int[size];
cout << "Enter the array elements:\n";
for (int i = 0; i < size; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> arr[i];
}
cout << "Enter the value of n: ";
cin >> n;
NthLargestFinder finder(arr, size);
int nthLargest = finder.findNthLargest(n);
if (nthLargest != -1) {
Page 5 of 36
cout << "The " << n << "th largest number is: " << nthLargest << endl;
} else {
cout << "Invalid value of n. Please enter a valid value within the range." << endl;
}
delete[] arr;
return 0;
}
OUTPUT:
Page 6 of 36
PROGRAM NO-3
AIM:Write a program to implement of function overloading to overload and
calculate the area of:
Page 7 of 36
i. circle
ii. Square
iii. rectangle
iv. triangle
#include <iostream>
using namespace std;
class calcArea{
int l; int b; int h; int a; float r; char shape;
public:
void area(){
cout<<"Enter length: "<<endl;
cin>>l;
cout<<"Enter breadth: "<<endl;
cin>>b;
cout<<"Area of Rectangle is: "<<l*b<<"m^2"<<endl;
}
void area(int b, int h){
cout<<"Area of Triangle is: "<<(0.5*b*h)<<" "<<"m^2"<<endl;}
void area(float r){
cout<<"Area of Circle is : "<<(3.14*r*r)<<" "<<"m^2"<<endl;}
void area(int a, char shape){
if(shape == 's'){
cout<<"Area of Square is : "<<(a*a)<<" "<<"m^2"<<endl;}
else{
cout<<"Invaild shape";
}}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
calcArea a;
a.area();
a.area(20,10);
a.area(2.5);
a.area(6,'s');
return 0;
}
OUTPUT:
Page 8 of 36
PROGRAM NO-4
Page 9 of 36
AIM:Write a program to implement function overriding
#include <iostream>
using namespace std;
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
int sum1 = add(5, 10);
int sum2 = add(5, 10, 15);
double sum3 = add(3.5, 7.2);
cout << "Sum 1: " << sum1 << endl;
cout << "Sum 2: " << sum2 << endl;
cout << "Sum 3: " << sum3 << endl;
return 0;
}
OUTPUT:
Page 10 of 36
PROGRAM NO-5
AIM: Write a program to implement the concept of inheritance
Page 11 of 36
i. Single Inheritance
ii. Multilevel Inheritance
ii. Multiple Inheritance
#include <iostream>
using namespace std;
class SingleInheritanceBase {
public:
void displaySingle() {
cout << "Single Inheritance Base Class" << endl;
}
};
class MultipleInheritanceBase1 {
public:
void displayBase1() {
cout << "Multiple Inheritance Base Class 1" << endl;
}
};
class MultipleInheritanceBase2 {
public:
void displayBase2() {
cout << "Multiple Inheritance Base Class 2" << endl;
}
};
OUTPUT:
Page 13 of 36
PROGRAM NO-6
AIM: Write a program to overload constructors
Page 14 of 36
#include <iostream>
using namespace std;
class Person {
private:
string name;
public:
Person() {
name = "Aditya";
}
Person(string n) {
name = n;
}
void display() {
cout << "Name: " << name << endl;
}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
Person person1;
Person person2("Priyanshi");
cout << "Person 1: "<<endl;
person1.display();
cout << "Person 2: "<<endl;
person2.display();
return 0;
}
OUTPUT:
Page 15 of 36
PROGRAM NO-7
AIM: Write a program to show the function of destructors
Page 16 of 36
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
cout << "Constructor called" << endl;
}
~MyClass() {
cout << "Destructor called" << endl;
}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
cout << "Creating an object" << endl;
MyClass obj;
cout << "Inside the main function" << endl;
return 0;
}
OUTPUT:
Page 17 of 36
PROGRAM NO-8
AIM: Write a program to implement the concept of friend class & friend
function
Page 18 of 36
#include <iostream>
using namespace std;
class FriendClass;
void friendFunction(FriendClass& obj);
class MyClass {
private:
int data;
public:
MyClass(int val) : data(val) {}
friend void friendFunction(MyClass& obj);
friend class FriendClass;
};
class FriendClass {
public:
void accessPrivateMember(MyClass& obj) {
cout << "Accessing private member of MyClass from FriendClass: " << obj.data <<
endl;
}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
MyClass obj(42);
friendFunction(obj);
FriendClass friendObj;
friendObj.accessPrivateMember(obj);
return 0;
}
OUTPUT:
Page 19 of 36
PROGRAM NO-9
AIM: Write a program to implement 8-queen problem.
Page 20 of 36
#include <iostream>
#include <vector>
using namespace std;
class EightQueens {
private:
const int boardSize;
vector<vector<int>> chessboard;
public:
EightQueens(int size) : boardSize(size) {
chessboard.resize(boardSize, vector<int>(boardSize, 0));
}
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
EightQueens chessboard(8);
if (chessboard.solveNQueens(0)) {
cout << "Solution for 8-Queens problem:\n";
chessboard.displayBoard();
} else {
cout << "No solution found for 8-Queens problem.\n";
}
return 0;
}
OUTPUT:
Page 22 of 36
PROGRAM NO-10
AIM: Write a program to implement 17-coin problem
Page 23 of 36
#include<iostream>
using namespace std;
int main(){
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
int c=17;
int a,b;
while (c>=0)
{
while (true){
else {
c=c-a;
break;
if(c<=4){
cout<<"P2 wins";
return 0;
}
}
}
while (true){
Page 24 of 36
else{
c=c-b;
if(c<=4){
cout<<"P1 wins";
return 0;
}
break;
}
}
};
return 0;
}
OUTPUT:
Page 25 of 36
PROGRAM NO-11
AIM: Write a program using function to calculate the factorial using recursion
Page 26 of 36
#include<iostream>
using namespace std;
class FactorialCalculator {
public:
int calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * calculateFactorial(n - 1);
}
}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
FactorialCalculator calculator;
int number;
cout << "Enter a number to calculate its factorial: "<<endl;
cin >> number;
int result = calculator.calculateFactorial(number);
cout << "Factorial of " << number << " is: " << result << endl;
return 0;
}
OUTPUT:
Page 27 of 36
PROGRAM NO-12
AIM: Write a program to print Fibonacci series
#include<iostream>
Page 28 of 36
using namespace std;
class FibonacciSeries {
public:
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
FibonacciSeries fibonacciSeries;
int terms;
cout << "Enter the number for the Fibonacci series: ";
cin >> terms;
cout << "Fibonacci Series: ";
fibonacciSeries.printFibonacciSeries(terms);
return 0;
}
OUTPUT:
Page 29 of 36
PROGRAM NO-13
AIM: Write a program to implement the concepts of virtual functions
Page 30 of 36
#include <iostream>
using namespace std;
class Shape {
public:
virtual void display(){
cout << "This is a generic shape." <<endl;
}
virtual ~Shape() {}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
Rectangle rectangle;
Circle circle;
Shape* shape1 = &rectangle;
Shape* shape2 = &circle;
shape1->display();
shape2->display();
return 0;
}
OUTPUT:
Page 31 of 36
PROGRAM NO-14
AIM: Write a program to implement the concepts of pure virtual functions
#include <iostream>
using namespace std;
Page 32 of 36
class Animal {
public:
virtual void Sound()=0;
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
Dog myDog;
Cat myCat;
Animal* animal1 = &myDog;
Animal* animal2 = &myCat;
animal1->Sound();
animal2->Sound();
return 0;
}
OUTPUT:
Page 33 of 36
PROGRAM NO-15
AIM: Write a program of Complex number addition by overloading plus (+)
operator
Page 34 of 36
#include <iostream>
using namespace std;
class Complex{
float real;float imag;
public:
Complex(){};
Complex(float, float);
Complex operator+(Complex);
void display();
};
Complex::Complex(float real, float imag){
this->real = real;
this->imag = imag;
}
void Complex::display(){
cout<<this->real<<" + "<<this->imag<<"i"<<endl;
}
Complex Complex::operator+(Complex c){
Complex temp;
temp.real = this->real + c.real;
temp.imag = this->imag + c.imag;
return temp;
}
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
Complex c1, c2, c3;
c1 = Complex(2.5, 3.5);
c2 = Complex(1.6, 2.7);
c3 = c1 + c2;
c1.display();
c2.display();
c3.display();
return 0;
}
OUTPUT:
Page 35 of 36
Page 36 of 36