0% found this document useful (0 votes)
16 views36 pages

Priyanshioops 1

This document contains a series of programming assignments focused on Object-Oriented Programming concepts in C++. Each program demonstrates various OOP principles such as function overloading, inheritance, friend classes, and recursion, with detailed code examples and outputs. The assignments are submitted by Priyanshi Thapa to Dr. Aaina at Jamia Hamdard University.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views36 pages

Priyanshioops 1

This document contains a series of programming assignments focused on Object-Oriented Programming concepts in C++. Each program demonstrates various OOP principles such as function overloading, inheritance, friend classes, and recursion, with detailed code examples and outputs. The assignments are submitted by Priyanshi Thapa to Dr. Aaina at Jamia Hamdard University.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

OBJECT ORIENTED PROGRAMMING

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

7 Write a Program to show function of destructor.

8 Write a Program to implement the concept of friend class


and friend function.
9 Write a Program to implement the concept of 8 Queen
problem.
10 Write a Program to implement the concept of 17 coin
problem.
11 Write a program using function to calculate the factorial
using recursion
12 Write a program to print fibonacci series

13 Implement the concept of virtual functions

14 Implement the concept of pure virtual functions

15 Complex number addition by overlaoding plus (+) operator

INDEX

Page 2 of 36
PROGRAM NO-1

AIM:Write a program to SWAP 2 numbers


#include <iostream>
using namespace std;

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 add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

double add(double a, double b) {


return a + b;
}

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 SingleInheritanceDerived : public SingleInheritanceBase {


public:
void displayDerived() {
cout << "Single Inheritance Derived 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;
}
};

class MultipleInheritanceDerived : public MultipleInheritanceBase1, public


MultipleInheritanceBase2 {
public:
void displayDerived() {
cout << "Multiple Inheritance Derived Class" << endl;
}
};
class MultiLevelInheritanceBase {
Page 12 of 36
public:
void displayBase() {
cout << "Multi-level Inheritance Base Class" << endl;
}
};
class MultiLevelInheritanceDerived : public MultiLevelInheritanceBase {
public:
void displayDerived() {
cout << "Multi-level Inheritance Derived Class" << endl;
}
};

class MultiLevelInheritanceFurtherDerived : public MultiLevelInheritanceDerived {


public:
void displayFurtherDerived() {
cout << "Multi-level Inheritance Further Derived Class" << endl;
}
};
int main() {
cout<<"Name: Priyanshi Thapa"<<endl;
cout<<"Enrollment No.: 2021-310-176"<<endl;
cout<<endl;
SingleInheritanceDerived singleDerived;
singleDerived.displaySingle();
singleDerived.displayDerived();
std::cout << "\n";
MultipleInheritanceDerived multipleDerived;
multipleDerived.displayBase1();
multipleDerived.displayBase2();
multipleDerived.displayDerived();
std::cout << "\n";
MultiLevelInheritanceFurtherDerived multiLevelDerived;
multiLevelDerived.displayBase();
multiLevelDerived.displayDerived();
multiLevelDerived.displayFurtherDerived();
return 0;
}

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;
}
};

void friendFunction(MyClass& obj) {


cout << "Accessing private member of MyClass from friend function: " << 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));
}

void displayBoard() const {


for (int i = 0; i < boardSize; ++i) {
for (int j = 0; j < boardSize; ++j) {
cout << (chessboard[i][j] ? "Q " : ". ");
}
cout << "\n";
}
}

bool isSafe(int row, int col) const {


for (int i = 0; i < col; ++i)
if (chessboard[row][i])
return false;
for (int i = row, j = col; i >= 0 && j >= 0; --i, --j)
if (chessboard[i][j])
return false;
for (int i = row, j = col; i < boardSize && j >= 0; ++i, --j)
if (chessboard[i][j])
return false;
return true;
}

bool solveNQueens(int col) {


if (col == boardSize) {
return true;
}

for (int i = 0; i < boardSize; ++i) {


if (isSafe(i, col)) {
Page 21 of 36
chessboard[i][col] = 1;
if (solveNQueens(col + 1))
return true;
chessboard[i][col] = 0;
}
}
return false;
}
};

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){

cout <<"Choose Number P1:";


cin>>a;
if (a>=5 ){
cout <<"Invalid\n";
continue;
// cout <<"Choose Number P1:";
// cin>>a;
}

else {
c=c-a;
break;
if(c<=4){
cout<<"P2 wins";
return 0;
}
}
}
while (true){

cout<<"Choose number P2:";


cin>>b;
if (b>=5){
cout <<"Invalid\n";
continue;
// cout <<"Choose Number P2:";
// cin>>b;

Page 24 of 36
else{
c=c-b;
if(c<=4){
cout<<"P1 wins";
return 0;
}
break;
}
}

cout <<"Remaning value:"<<c<<endl;

};
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);
}
}

void printFibonacciSeries(int terms) {


for (int i = 0; i < terms; ++i) {
cout << fibonacci(i) << " ";
}
cout << endl;
}
};

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() {}
};

class Rectangle : public Shape {


public:
void display() override {
cout << "This is a rectangle." <<endl;
}
};

class Circle : public Shape {


public:
void display() override {
cout << "This is a circle." << endl;
}
};

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;
};

class Dog : public Animal {


public:
void Sound() override {
cout << "Woof! Woof!" << endl;
}
};

class Cat : public Animal {


public:
void Sound() override {
cout << "Meow! Meow!" <<endl;
}
};

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

You might also like