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

pranav cpp all programs

Uploaded by

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

pranav cpp all programs

Uploaded by

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

Practical no1:- Demonstrate the use of if……else and control structure.

(even or odd, sum and average, prime or composite, sum, difference,


product and quotient of two integers)
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A
#include <iostream>
using namespace std;
int main() {
int num1, num2;

// Getting input from the user


cout << "Enter two integers: ";
cin >> num1 >> num2;

// Checking if num1 is even or odd


if (num1 % 2 == 0) {
cout << num1 << " is even." << endl;
} else {
cout << num1 << " is odd." << endl;
}

// Checking if num2 is even or odd


if (num2 % 2 == 0) {
cout << num2 << " is even." << endl;
} else {
cout << num2 << " is odd." << endl;
}
// Calculating and displaying sum
int sum = num1 + num2;
cout << "Sum: " << sum << endl;

// Calculating and displaying difference


int difference = num1 - num2;
cout << "Difference: " << difference << endl;

// Calculating and displaying product


int product = num1 * num2;
cout << "Product: " << product << endl;

// Checking for division by zero before performing division


if (num2 != 0) {
int quotient = num1 / num2;
cout << "Quotient: " << quotient << endl;
} else {
cout << "Cannot divide by zero." << endl;
}

return 0;

1
OUTPUT WINDOW

Enter two integers: 5 2


5 is odd.
2 is even.
Sum: 7
Difference: 3
Product: 10
Quotient: 2

Practical no2:- Write a to Program to Demonstrate Function Overloading

2
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A
#include <iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) {
return a + b;
}

// Function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

int main() {
cout << "Sum of 2 and 3: " << add(2, 3) << endl;
cout << "Sum of 2, 3, and 4: " << add(2, 3, 4) << endl;
return 0;
}

OUTPUT WINDOW

Sum of 2 and 3: 5
Sum of 2, 3, and 4: 9

Practical no 3:-Write a to Program to Demonstrate Encapsulation Using a


Class

3
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A
#include <iostream>
using namespace std;

class Student {
private:
int age;

public:
void setAge(int a) {
age = a;
}

int getAge() {
return age;
}
};

int main() {
Student s;
s.setAge(20);
cout << "Student's Age: " << s.getAge() << endl;
return 0;
}

OUTPUT WINDOW

Student's Age: 20

Practical no 4:-Write a to Program to Demonstrate Constructors and


Destructor.

4
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A

#include <iostream>
using namespace std;

class Demo {
public:
Demo() {
cout << "Constructor called!" << endl;
}

~Demo() {
cout << "Destructor called!" << endl;
}
};

int main() {
Demo d;
return 0;
}

OUTPUT WINDOW

Constructor called!

Destructor called!

Practical.no5:- Write a to Program to Demonstrate Single Inheritance.


Name:Pranav Sanjay Badhe Roll.no:-08

5
Batch no: B1 Div- A
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "Animal eats" << endl;
}
};

// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks" << endl;
}
};

int main() {
Dog d;
d.eat();
d.bark();
return 0;
}

OUTPUT WINDOW

Animal eats

Dog barks

Name:

Practical no 6:- Write a to Program to Demonstrate Multiple Inheritance.


Name:Pranav Sanjay Badhe Roll.no:-08

6
Batch no: B1 Div- A
#include <iostream>
using namespace std;
// Base class 1
class Base1 {
public:
void greet() {
cout << "Hello from Base1!" << endl;
}
};

// Base class 2
class Base2 {
public:
void greet() {
cout << "Hello from Base2!" << endl;
}
};

// Derived class
class Derived : public Base1, public Base2 {
public:
void show() {
Base1::greet(); // Resolve ambiguity
Base2::greet();
}
};

int main() {
Derived d;
d.show();
return 0;
}

OUTPUT WINDOW

Hello from Base1!

Hello from Base2!

Practical.no7:- Write a to Program to Demonstrate Operator Overloading


Using Friend Function.
Name:Pranav Sanjay Badhe Roll.no:-08

7
Batch no: B1 Div- A

#include <iostream>
using namespace std;

class Number {
private:
int value;

public:
// Constructor to initialize the number
Number(int v = 0) : value(v) {}

// Friend function to overload + operator


friend Number operator+(const Number &n1, const Number &n2);

// Function to display the number


void display() const {
cout << value << endl;
}
};

// Overloading the + operator using a friend function


Number operator+(const Number &n1, const Number &n2) {
return Number(n1.value + n2.value);
}

int main() {
Number num1(5); // First number
Number num2(10); // Second number

// Adding two numbers using the overloaded + operator


Number result = num1 + num2;

// Displaying the result


cout << "Number 1: ";
num1.display();
cout << "Number 2: ";
num2.display();
cout << "Sum: ";
result.display();

return 0;

8
}

OUTPUT WINDOW

Number 1: 5

Number 2: 10

Sum: 15

Practical no 8:-Write a to Program to Demonstrate Use of Friend


Function.
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A

9
#include <iostream>
using namespace std;

class Box {
private:
int length;

public:
Box() : length(5) {}

friend void showLength(Box);


};

void showLength(Box b) {
cout << "Length of Box: " << b.length << endl;
}

int main() {
Box b;
showLength(b);
return 0;
}

OUTPUT WINDOW

Length of Box: 5

Practical no 9:-Write a to Program to Demonstrate Use of Virtual


Functions .
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A

10
#include <iostream>
using namespace std;

class Base {
public:
virtual void display() {
cout << "Display from Base class" << endl;
}
};

class Derived : public Base {


public:
void display() override {
cout << "Display from Derived class" << endl;
}
};

int main() {
Base *b;
Derived d;
b = &d;
b->display(); // Calls Derived's display function
return 0;
}

OUTPUT WINDOW

Display from Derived class

Practical no10:- Write a to Program to Demonstrate Use of Pointer.


Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A

11
#include <iostream>
using namespace std;

int main() {
int a = 10;
int *p = &a;
int **pp = &p;

cout << "Value of a: " << a << endl;


cout << "Value of *p: " << *p << endl;
cout << "Value of **pp: " << **pp << endl;

return 0;
}

OUTPUT WINDOW (a)

Value of a: 10

Value of *p: 10

Value of **pp: 10

b) Pointer to Objects
#include <iostream>
using namespace std;

class Box {
public:
void display() {
cout << "Box display function called." << endl;
}
};

int main() {
Box b;
Box *ptr = &b;
ptr->display();
return 0; }
OUTPUT WINDOW (b)

Box display function called.

12
Pointer to Function

#include <iostream>
using namespace std;

void display() {
cout << "Display function called!" << endl;
}

int main() {
void (*funcPtr)() = display;
funcPtr();
return 0;
}

OUTPUT WINDOW

Display function called!

Practical no.11:- Write a to Program to Demonstrate Use of Exception


Handling .
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A

13
#include <iostream>
using namespace std;

int main() {
int a = 10, b = 0;
try {
if (b == 0) {
throw "Division by zero error!";
}
cout << "Result: " << a / b << endl;
} catch (const char *msg) {
cout << "Exception: " << msg << endl;
}
return 0;
}

OUTPUT WINDOW

Exception: Division by zero


error!

14

You might also like