CPP LAB
CPP LAB
#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;
}
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) {}
class B {
private:
int value;
public:
B(int v) : value(v) {}
friend class A;
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;
}
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;
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) {}
int main() {
rohit c1(2.3, 4.5); // First first number
rohit c2(1.2, 3.7); // Second second number
return 0;
}
Program 6 :
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() { // Virtual function
cout << "base class show function called" << endl;
}
};
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;
}
};
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
cout << "Top element after pop: " << s.peek() << endl; // Should print 20
return 0;
}
Program 9 :
#include <iostream>
using namespace std;
int main() {
int arrInt[] = {64, 34, 25, 12, 22, 11, 90}; // Array of integers
int nInt = sizeof(arrInt) / sizeof(arrInt[0]);
return 0;
}
Program 10 :
#include <iostream>
using namespace std;
int main() {
int arrInt[] = {64, 34, 25, 12, 22, 11, 90}; // Integer array
int nInt = sizeof(arrInt) / sizeof(arrInt[0]);
return 0;
}