constructor
constructor
constructor
#include <iostream>
class Base {
public:
Base() {
};
public:
Derived() {
};
int main() {
Derived obj;
return 0;
Output:
#include <iostream>
public:
Base(int x) {
cout << "Base class constructor called with value: " << x << endl;
};
public:
cout << "Derived class constructor called with value: " << y << endl;
};
int main() {
return 0;
Output
#include <iostream>
class Base1 {
public:
Base1() {
};
class Base2 {
public:
Base2() {
};
public:
Derived() {
};
int main() {
Derived obj;
return 0;
Output
4. Function Overloading
#include <iostream>
class Calculator {
public:
return a + b;
}
return a + b;
return a + b + c;
};
int main() {
Calculator calc;
cout << "Addition of 2 integers: " << calc.add(10, 20) << endl;
cout << "Addition of 2 doubles: " << calc.add(10.5, 20.5) << endl;
cout << "Addition of 3 integers: " << calc.add(10, 20, 30) << endl;
return 0;
Output
Addition of 2 integers: 30
Addition of 2 doubles: 31
Addition of 3 integers: 60
5. Function Overriding
#include <iostream>
class Base {
public:
};
public:
};
int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj;
return 0;
Output
#include <iostream>
class Base {
public:
void display(int x) {
cout << "Base class overloaded display function with value: " << x << endl;
};
public:
void display(int x) {
cout << "Derived class overloaded display function with value: " << x << endl;
};
int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj;
return 0;
Output
7. Arrays
Here are three C++ programs demonstrating the use of arrays in different scenarios, along
with their outputs.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
cout << "Sum of array elements: " << sum << endl;
return 0;
}
Output:
2. Transpose of a 2D Array
#include <iostream>
using namespace std;
int main() {
int rows = 2, cols = 3;
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transpose[3][2];
return 0;
}
Output:
Original Matrix:
1 2 3
4 5 6
Transpose of the Matrix:
1 4
2 5
3 6
#include <iostream>
using namespace std;
int main() {
int arr[] = {45, 67, 89, 12, 34, 90, 23};
int size = sizeof(arr) / sizeof(arr[0]);
int maxElement = arr[0];
cout << "Maximum element in the array: " << maxElement << endl;
return 0;
}
Output:
Here are three C++ programs demonstrating various operations with stacks, including basic
stack operations, reversing a string using a stack, and evaluating a postfix expression.
This program demonstrates the basic stack operations like push, pop, and display using the
std::stack library.
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> myStack;
return 0;
}
Output:
Stack size: 3
Top element: 30
Top element: 20
Top element: 10
Stack is empty now.
#include <iostream>
#include <stack>
using namespace std;
return reversed;
}
int main() {
string str = "Hello, World!";
cout << "Original String: " << str << endl;
cout << "Reversed String: " << reverseString(str) << endl;
return 0;
}
Output:
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;
switch (c) {
case '+':
s.push(val1 + val2);
break;
case '-':
s.push(val1 - val2);
break;
case '*':
s.push(val1 * val2);
break;
case '/':
s.push(val1 / val2);
break;
}
}
}
return s.top();
}
int main() {
string postfixExpr = "53+82-*";
cout << "Postfix Expression: " << postfixExpr << endl;
cout << "Evaluation Result: " << evaluatePostfix(postfixExpr) << endl;
return 0;
}
Output: