pranav cpp all programs
pranav cpp all programs
return 0;
1
OUTPUT WINDOW
2
Name:Pranav Sanjay Badhe Roll.no:-08
Batch no: B1 Div- A
#include <iostream>
using namespace std;
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
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
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!
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:
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
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) {}
int main() {
Number num1(5); // First number
Number num2(10); // Second number
return 0;
8
}
OUTPUT WINDOW
Number 1: 5
Number 2: 10
Sum: 15
9
#include <iostream>
using namespace std;
class Box {
private:
int length;
public:
Box() : length(5) {}
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
10
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Display from Base class" << endl;
}
};
int main() {
Base *b;
Derived d;
b = &d;
b->display(); // Calls Derived's display function
return 0;
}
OUTPUT WINDOW
11
#include <iostream>
using namespace std;
int main() {
int a = 10;
int *p = &a;
int **pp = &p;
return 0;
}
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)
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
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
14