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

constructor

Constructor

Uploaded by

yeshwanthy420
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)
5 views

constructor

Constructor

Uploaded by

yeshwanthy420
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/ 11

1.

Single Inheritance with Constructor

#include <iostream>

using namespace std;

class Base {

public:

Base() {

cout << "Base class constructor called." << endl;

};

class Derived : public Base {

public:

Derived() {

cout << "Derived class constructor called." << endl;

};

int main() {

Derived obj;

return 0;

Output:

Base class constructor called.


Derived class constructor called.

2. Parameterized Constructor with Inheritance

#include <iostream>

using namespace std;


class Base {

public:

Base(int x) {

cout << "Base class constructor called with value: " << x << endl;

};

class Derived : public Base {

public:

Derived(int x, int y) : Base(x) {

cout << "Derived class constructor called with value: " << y << endl;

};

int main() {

Derived obj(10, 20);

return 0;

Output

Base class constructor called with value: 10

Derived class constructor called with value: 20

3. Multiple Inheritance with Constructors

#include <iostream>

using namespace std;

class Base1 {

public:

Base1() {

cout << "Base1 class constructor called." << endl;

};
class Base2 {

public:

Base2() {

cout << "Base2 class constructor called." << endl;

};

class Derived : public Base1, public Base2 {

public:

Derived() {

cout << "Derived class constructor called." << endl;

};

int main() {

Derived obj;

return 0;

Output

Base1 class constructor called.

Base2 class constructor called.

Derived class constructor called.

4. Function Overloading

#include <iostream>

using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;
}

double add(double a, double b) {

return a + b;

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

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>

using namespace std;

class Base {

public:

virtual void show() {


cout << "Base class show function called." << endl;

};

class Derived : public Base {

public:

void show() override {

cout << "Derived class show function called." << endl;

};

int main() {

Base* basePtr;

Derived derivedObj;

basePtr = &derivedObj;

basePtr->show(); // Calls the Derived class version due to virtual function

return 0;

Output

Derived class show function called.

6. Function Overloading and Overriding Combined

#include <iostream>

using namespace std;

class Base {

public:

virtual void display() {

cout << "Base class display function." << endl;


}

void display(int x) {

cout << "Base class overloaded display function with value: " << x << endl;

};

class Derived : public Base {

public:

void display() override {

cout << "Derived class display function." << endl;

void display(int x) {

cout << "Derived class overloaded display function with value: " << x << endl;

};

int main() {

Base* basePtr;

Derived derivedObj;

basePtr = &derivedObj;

basePtr->display(); // Calls the Derived class version (overriding)

derivedObj.display(42); // Calls the Derived class overloaded function

return 0;

Output

Derived class display function.


Derived class overloaded display function with value: 42

7. Arrays

Here are three C++ programs demonstrating the use of arrays in different scenarios, along
with their outputs.

1. Sum of Elements in a 1D Array

This program calculates the sum of elements in a 1D array.

#include <iostream>
using namespace std;

int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;

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


sum += arr[i];
}

cout << "Sum of array elements: " << sum << endl;
return 0;
}

Output:

Sum of array elements: 150

2. Transpose of a 2D Array

This program computes the 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];

// Transposing the matrix


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

cout << "Original Matrix:" << endl;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

cout << "Transpose of the Matrix:" << endl;


for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cout << transpose[i][j] << " ";
}
cout << endl;
}

return 0;
}

Output:

Original Matrix:
1 2 3
4 5 6
Transpose of the Matrix:
1 4
2 5
3 6

3. Finding Maximum Element in an Array

This program finds the maximum element in a 1D array.

#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];

for (int i = 1; i < size; i++) {


if (arr[i] > maxElement) {
maxElement = arr[i];
}
}

cout << "Maximum element in the array: " << maxElement << endl;
return 0;
}

Output:

Maximum element in the array: 90


8. Operations with stack

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.

1. Basic Stack Operations

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;

// Push elements onto the stack


myStack.push(10);
myStack.push(20);
myStack.push(30);

cout << "Stack size: " << myStack.size() << endl;

// Display and pop elements from the stack


while (!myStack.empty()) {
cout << "Top element: " << myStack.top() << endl;
myStack.pop();
}

cout << "Stack is empty now." << endl;

return 0;
}

Output:

Stack size: 3
Top element: 30
Top element: 20
Top element: 10
Stack is empty now.

2. Reversing a String Using a Stack

This program demonstrates how to reverse a string using a stack.

#include <iostream>
#include <stack>
using namespace std;

string reverseString(string str) {


stack<char> s;
// Push characters onto the stack
for (char c : str) {
s.push(c);
}

// Pop characters to reverse the string


string reversed = "";
while (!s.empty()) {
reversed += s.top();
s.pop();
}

return reversed;
}

int main() {
string str = "Hello, World!";
cout << "Original String: " << str << endl;
cout << "Reversed String: " << reverseString(str) << endl;

return 0;
}

Output:

Original String: Hello, World!


Reversed String: !dlroW ,olleH

3. Postfix Expression Evaluation Using Stack

This program evaluates a postfix expression using a stack.

#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;

int evaluatePostfix(string expr) {


stack<int> s;

for (char c : expr) {


if (isdigit(c)) {
s.push(c - '0'); // Convert character to integer
} else {
int val2 = s.top();
s.pop();
int val1 = s.top();
s.pop();

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:

Postfix Expression: 53+82-*


Evaluation Result: 48

You might also like