Name : Sahil Khan
Enrollment : A023167023139
B.Tech CSE Data Science – 3X
Object Oriented Programming Using
C++
ASSIGNMENT
1. Simple C++ Programs to Implement Various Control Structures. a. If
statement b. Switch case statement and do while loop c. For loop d.
While loop
a- If statement
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
if (number > 0) {
std::cout << "The number is positive." << std::endl;
} else if (number < 0) {
std::cout << "The number is negative." << std::endl;
} else {
std::cout << "The number is zero." << std::endl;
return 0;
b- Switch case and do while statement
#include <iostream>
int main() {
int day;
do {
std::cout << "Enter a number (1-7) for the day of the week (0 to
exit): ";
std::cin >> day;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
case 6:
std::cout << "Saturday" << std::endl;
break;
case 7:
std::cout << "Sunday" << std::endl;
break;
case 0:
std::cout << "Exiting..." << std::endl;
break;
default:
std::cout << "Invalid input, please enter a number between 1-
7." << std::endl;
} while (day != 0);
return 0;
c- For loop
#include <iostream>
int main() {
std::cout << "The first 10 natural numbers are: ";
for (int i = 1; i <= 10; ++i) {
std::cout << i << " ";
std::cout << std::endl;
return 0;
d- While loop
#include <iostream>
int main() {
int number;
int sum = 0;
std::cout << "Enter integers to sum (0 to end): ";
while (true) {
std::cin >> number;
if (number == 0) {
break; // Exit the loop if the input is zero
sum += number;
std::cout << "The total sum is: " << sum << std::endl;
return 0;
2. Programs to Understand Structure & Unions. a. Structure b. union
a- Structure
#include <iostream>
struct Person {
char name[50];
int age;
float height;
};
int main() {
Person person;
std::cout << "Enter name: ";
std::cin.getline(person.name, 50);
std::cout << "Enter age: ";
std::cin >> person.age;
std::cout << "Enter height (in meters): ";
std::cin >> person.height;
std::cout << "\nPerson Details:\n";
std::cout << "Name: " << person.name << "\n";
std::cout << "Age: " << person.age << "\n";
std::cout << "Height: " << person.height << " m\n";
return 0;
}
b- union
#include <iostream>
union Data {
int intValue;
float floatValue;
char charValue;
};
int main() {
Data data;
data.intValue = 5;
std::cout << "Data as Integer: " << data.intValue << std::endl;
data.floatValue = 3.14f;
std::cout << "Data as Float: " << data.floatValue << std::endl;
data.charValue = 'A';
std::cout << "Data as Char: " << data.charValue << std::endl;
std::cout << "Data as Integer after overwriting: " << data.intValue <<
std::endl;
return 0;
}
3. Programs to Understand Pointer Arithmetic.
a- Basic pointer arithmetic
#include <iostream>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Pointing to the first element of the array
std::cout << "Array elements using pointer arithmetic:\n";
for (int i = 0; i < 5; ++i) {
std::cout << "Element " << i << ": " << *(ptr + i) << std::endl; // Accessing
elements using pointer
}
return 0;
}
b- Pointer arithmetic with characters array
#include <iostream>
int main() {
char str[] = "Hello, World!";
char* ptr = str; // Pointer to the first character
std::cout << "Characters in the string using pointer arithmetic:\n";
while (*ptr != '\0') { // Until the null terminator
std::cout << *ptr << " ";
ptr++; // Move to the next character
}
std::cout << std::endl;
return 0;
}
c- Pointer arithmetic for array manipulation
#include <iostream>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr; // Pointer to the first element
// Modify array elements using pointer arithmetic
for (int i = 0; i < 5; ++i) {
*(ptr + i) *= 2; // Double each element
}
std::cout << "Array elements after modification:\n";
for (int i = 0; i < 5; ++i) {
std::cout << arr[i] << " "; // Print modified elements
}
std::cout << std::endl;
return 0;
}
4. Inline functions
#include <iostream>
using namespace std;
class InlineExample {
public:
inline int square(int x) {
return x * x;
}
inline int cube(int x) {
return x * x * x;
}
};
int main() {
InlineExample obj;
int number;
cout << "Enter a number: ";
cin >> number;
cout << "Square of " << number << " is: " << obj.square(number) << endl;
cout << "Cube of " << number << " is: " << obj.cube(number) << endl;
return 0;
}
5. Programs to Understand Different Function Call Mechanism. a. Call by
reference & Call by Value
#include <iostream>
using namespace std;
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "Inside swapByValue: a = " << a << ", b = " << b << endl;
}
int main() {
int x = 10, y = 20;
cout << "Before swapByValue: x = " << x << ", y = " << y << endl;
swapByValue(x, y);
cout << "After swapByValue: x = " << x << ", y = " << y << endl;
return 0;
}
B- Call by refrence
#include <iostream>
using namespace std;
void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
cout << "Inside swapByReference: a = " << a << ", b = " << b << endl;
}
int main() {
int x = 10, y = 20;
cout << "Before swapByReference: x = " << x << ", y = " << y << endl;
swapByReference(x, y);
cout << "After swapByReference: x = " << x << ", y = " << y << endl;
return 0;
}
6. Programs to Understand Storage Specifiers.
#include <iostream>
using namespace std;
void autoSpecifier() {
auto x = 10;
cout << "Auto specifier: " << x << endl;
}
void staticSpecifier() {
static int count = 0;
count++;
cout << "Static specifier: " << count << endl;
}
extern int externVariable;
void externSpecifier() {
cout << "Extern specifier: " << externVariable << endl;
}
int main() {
autoSpecifier();
for (int i = 0; i < 3; ++i) {
staticSpecifier();
}
externSpecifier();
return 0;
}
int externVariable = 100;
7. Constructor and destructor
#include <iostream>
using namespace std;
class Demo {
private:
int number;
public:
// Constructor
Demo() {
cout << "Constructor called!" << endl;
number = 0;
}
// Parameterized Constructor
Demo(int num) {
cout << "Parameterized Constructor called!" << endl;
number = num;
}
// Destructor
~Demo() {
cout << "Destructor called for object with number: " << number << endl;
}
// Function to display the value of 'number'
void display() {
cout << "Number: " << number << endl;
}
};
int main() {
Demo obj1; // Constructor is called
Demo obj2(42); // Parameterized constructor is called
obj1.display();
obj2.display();
// Destructor will be called automatically when obj1 and obj2 go out of scope
return 0;
}
8. Programs to Implement Inheritance and Function Overriding. a. Multiple
inheritance –Access Specifiers b. Hierarchical inheritance – Function
Overriding /Virtual Function 11. Programs to Overload Unary & Binary
Operators as Member Function & Non Member Function. a. Unary
operator as member function b. Binary operator as non member function