Neww
Neww
cpp
Copy code
#include <iostream>
using namespace std;
void ifStatementExample() {
int num = 10;
if (num > 5) {
cout << "The number is greater than 5" << endl;
} else {
cout << "The number is less than or equal to 5" << endl;
}
}
int main() {
cout << "If Statement Example:" << endl;
ifStatementExample();
return 0;
}
b) Switch case statement and do while loop
cpp
Copy code
#include <iostream>
using namespace std;
void switchCaseAndDoWhile() {
int choice;
cout << "Enter a number (1-3): ";
cin >> choice;
switch(choice) {
case 1:
cout << "You chose option 1" << endl;
break;
case 2:
cout << "You chose option 2" << endl;
break;
case 3:
cout << "You chose option 3" << endl;
break;
default:
cout << "Invalid choice" << endl;
break;
}
int i = 1;
do {
cout << "Iteration " << i << endl;
i++;
} while (i <= 3);
}
int main() {
cout << "Switch Case and Do While Loop Example:" << endl;
switchCaseAndDoWhile();
return 0;
}
c) For loop
cpp
Copy code
#include <iostream>
using namespace std;
void forLoopExample() {
for (int i = 1; i <= 5; i++) {
cout << "For loop iteration " << i << endl;
}
}
int main() {
cout << "For Loop Example:" << endl;
forLoopExample();
return 0;
}
d) While loop
cpp
Copy code
#include <iostream>
using namespace std;
void whileLoopExample() {
int i = 1;
while (i <= 3) {
cout << "While loop iteration " << i << endl;
i++;
}
}
int main() {
cout << "While Loop Example:" << endl;
whileLoopExample();
return 0;
}
2. Programs to Understand Structure & Unions
a) Structure
cpp
Copy code
#include <iostream>
using namespace std;
struct Person {
string name;
int age;
};
void structureExample() {
Person person1;
person1.name = "John";
person1.age = 30;
cout << "Name: " << person1.name << ", Age: " << person1.age << endl;
}
int main() {
cout << "Structure Example:" << endl;
structureExample();
return 0;
}
b) Union
cpp
Copy code
#include <iostream>
using namespace std;
union Data {
int intData;
float floatData;
};
void unionExample() {
Data data;
data.intData = 5;
cout << "Integer data: " << data.intData << endl;
data.floatData = 3.14;
cout << "Float data: " << data.floatData << endl;
}
int main() {
cout << "Union Example:" << endl;
unionExample();
return 0;
}
3. Functions & Recursion
cpp
Copy code
#include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
void recursionExample() {
int n = 5;
cout << "Factorial of " << n << " is: " << factorial(n) << endl;
}
int main() {
cout << "Recursion Example:" << endl;
recursionExample();
return 0;
}
4. Programs to Understand Different Function Call Mechanisms
Call by Value & Call by Reference
cpp
Copy code
#include <iostream>
using namespace std;
void callByValue(int a) {
a = a + 5;
cout << "Call by Value: " << a << endl;
}
void functionCallExample() {
int value = 10;
callByValue(value);
cout << "Value after Call by Value: " << value << endl;
callByReference(value);
cout << "Value after Call by Reference: " << value << endl;
}
int main() {
cout << "Function Call by Value and Reference Example:" << endl;
functionCallExample();
return 0;
}
6. Constructors & Destructors
cpp
Copy code
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
cout << "Constructor called!" << endl;
}
~MyClass() {
cout << "Destructor called!" << endl;
}
};
int main() {
cout << "Constructor and Destructor Example:" << endl;
MyClass obj;
return 0;
}
7. Programs to Implement Inheritance and Function Overriding
a) Multiple Inheritance
cpp
Copy code
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal eats" << endl;
}
};
class Mammal {
public:
void walk() {
cout << "Mammal walks" << endl;
}
};
int main() {
cout << "Multiple Inheritance Example:" << endl;
Dog dog;
dog.eat();
dog.walk();
dog.bark();
return 0;
}
b) Hierarchical Inheritance and Function Overriding
cpp
Copy code
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing Shape" << endl;
}
};
int main() {
cout << "Hierarchical Inheritance and Function Overriding Example:" << endl;
Shape* shape;
Circle circle;
Square square;
shape = &circle;
shape->draw(); // Calls Circle's draw
shape = □
shape->draw(); // Calls Square's draw
return 0;
}
8. Implementation of Pure Virtual Function
cpp
Copy code
#include <iostream>
using namespace std;
class AbstractShape {
public:
virtual void draw() = 0; // Pure virtual function
virtual ~AbstractShape() {} // Virtual Destructor
};
int main() {
cout << "Pure Virtual Function Example:" << endl;
Triangle triangle;
AbstractShape* shape = ▵
shape->draw(); // Calls Triangle's draw
return 0;
}