0% found this document useful (0 votes)
7 views

CPP LAB

Object oriented programming C++ PROGRAMS

Uploaded by

carlgta2915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CPP LAB

Object oriented programming C++ PROGRAMS

Uploaded by

carlgta2915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Program 1 :

#include <iostream>
using namespace std;

class objCounter {
static int count;
public:
// Constructor
objCounter() {
count++;
cout << "Object created. Total objects: " << count << endl;
}

// Destructor
~objCounter() {
count--;
cout << "Object destroyed. Remaining objects: " << count << endl;
}

// Static function to get count


static int getCount() {
return count;
}
};

int objCounter::count = 0;

int main() {
objCounter obj1, obj2;
cout << "Current obj count: " << objCounter::getCount() << endl;

{
objCounter obj3;
}

cout << "Current obj count: " << objCounter::getCount() << endl;
return 0;
}
Program 2 :
#include <iostream>
using namespace std;

class B;

class A {
private:
int value;
public:
A(int v) : value(v) {}

friend int add(A, B);


};

class B {
private:
int value;
public:
B(int v) : value(v) {}

friend class A;

friend int add(A a, B b);


};

int add(A a, B b) {
return a.value + b.value;
}

int main() {
A objA(10);
B objB(20);
cout << "Sum: " << add(objA, objB) << endl;
return 0;
}
Program 3 :
#include <iostream>
using namespace std;

class overload {
public:

void print(int i) {
cout << "Integer: " << i << endl;
}

void print(double f) {
cout << "Double: " << f << endl;
}

void print(const char* s) {


cout << "String: " << s << endl;
}
};

int main() {
overload obj;

obj.print(10);
obj.print(5.5);
obj.print("Hello");

return 0;
}
Program 4 :
#include <iostream>
using namespace std;

class myClass {
int val;
public:
myClass(int v) : val(v) {}
void display() {
cout << "Value: " << val << endl;
}
};

int main() {
int* pInt = new int(10);
cout << "dynamically allocated integer: " << *pInt << endl;

int* pArray = new int[5];


for(int i = 0; i < 5; i++) {
pArray[i] = i * 10;
}
cout << "dynamically allocated array values: ";
for(int i = 0; i < 5; i++) {
cout << pArray[i] << " ";
}
cout << endl;

myClass* pObj = new myClass(100);


pObj->display();

delete pInt;
delete[] pArray;
delete pObj;

return 0;
}
Program 5 :
#include <iostream>
using namespace std;

class rohit {
private:
float real, imag;

public:
// Constructor to initialize complex numbers
rohit(float r = 0.0, float i = 0.0) : real(r), imag(i) {}

// Friend function to overload the + operator


friend rohit operator+(const rohit& c1, const rohit& c2);

// Function to display the complex number


void display() const {
cout << real << " + " << imag << "i" << endl;
}
};

// Definition of the friend function to overload the + operator


rohit operator+(const rohit& c1, const rohit& c2) {
// Create a new complex object by adding the real and imaginary parts
return rohit(c1.real + c2.real, c1.imag + c2.imag);
}

int main() {
rohit c1(2.3, 4.5); // First first number
rohit c2(1.2, 3.7); // Second second number

rohit c3 = c1 + c2; // Adding two complex numbers using overloaded +

// Displaying the result


cout << "c1 = "; c1.display();
cout << "c2 = "; c2.display();
cout << "c1 + c2 = "; c3.display();

return 0;
}
Program 6 :
#include <iostream>
using namespace std;

class Base {
public:
virtual void show() { // Virtual function
cout << "base class show function called" << endl;
}
};

class Derived : public Base {


public:
void show() override { // Overriding function
cout << "derived class show function called" << endl;
}
};

void display(Base& obj) {


obj.show();
}

int main() {
Base bObj
;
Derived dObj;

display(bObj
);
display(dObj);

return 0;
}
Program 7-a:
#include <iostream>
using namespace std;

class Base1 {
public:
void show() {
cout << "Base1's show function" << endl;
}
};

class Base2 {
public:
void show() {
cout << "Base2's show function" << endl;
}
};

class Derived : public Base1, public Base2 {


public:
void display() {
Base1::show();
Base2::show();
}
};

int main() {
Derived obj;
obj.display();
return 0;
}

7-b : The ambiguity caused by multiple inheritance is resolved by explicitly specifying which base
class's function should be called. In the example above, the ambiguity in the show() function is
resolved using Base1::show() and Base2::show() to distinguish between the two base classes.
Program 8:
#include <iostream>
using namespace std;
template <typename rohit>
class Stack {
private:
rohit arr[100];
int top; // top element index
public:
Stack() {
top = -1; // stack initially empty
}
void push(rohit value) {
if (top >= 99) {
cout << "Stack Overflow!" << endl;
} else {
top++; // top +1
arr[top] = value;
}
}
void pop() {
if (top < 0) {
cout << "Stack Underflow!" << endl;
} else {
top--;
}
}
rohit peek() {
if (top < 0) {
cout << "Stack is Empty!" << endl;
return -1;
}
return arr[top];
}

bool isEmpty() {
return top < 0;
}
};
int main() {
Stack<int> s; // Stack of integers

s.push(10);
s.push(20);
s.push(30);
cout << "Top element is: " << s.peek() << endl; // Should print 30

s.pop(); // Removing top element (30)

cout << "Top element after pop: " << s.peek() << endl; // Should print 20

return 0;
}
Program 9 :
#include <iostream>
using namespace std;

template <typename rohit>


void bubbleSort(rohit arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
rohit temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arrInt[] = {64, 34, 25, 12, 22, 11, 90}; // Array of integers
int nInt = sizeof(arrInt) / sizeof(arrInt[0]);

// Sorting integer array


bubbleSort(arrInt, nInt);
cout << "Sorted integer array: ";
for (int i = 0; i < nInt; i++) {
cout << arrInt[i] << " ";
}
cout << endl;

return 0;
}
Program 10 :
#include <iostream>
using namespace std;

template <typename rohit>


rohit findMin(rohit arr[], int n) {
rohit min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}

int main() {
int arrInt[] = {64, 34, 25, 12, 22, 11, 90}; // Integer array
int nInt = sizeof(arrInt) / sizeof(arrInt[0]);

// Find minimum in integer array


cout << "Minimum element in integer array: " << findMin(arrInt, nInt) << endl;

return 0;
}

You might also like