C++ Assignment
C++ Assignment
Enrollment : A023167023139
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;
if (number > 0) {
} else {
return 0;
#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:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 0:
break;
default:
std::cout << "Invalid input, please enter a number between 1-
7." << std::endl;
return 0;
c- For loop
#include <iostream>
int main() {
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) {
if (number == 0) {
sum += number;
std::cout << "The total sum is: " << sum << std::endl;
return 0;
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;
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;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Pointing to the first element of the array
return 0;
}
int main() {
char str[] = "Hello, World!";
char* ptr = str; // Pointer to the first character
return 0;
}
return 0;
}
4. Inline functions
#include <iostream>
using namespace std;
class InlineExample {
public:
inline int square(int x) {
return x * x;
}
int main() {
InlineExample obj;
int 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;
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;
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;
}
#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;
}
int main() {
autoSpecifier();
externSpecifier();
return 0;
}
int externVariable = 100;
#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;
}
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;
}