0% found this document useful (0 votes)
75 views5 pages

Code Topic Wise C+++

The document provides code snippets for 10 modules that cover fundamental C++ concepts: (1) basic input/output, (2) control flow, (3) arrays and strings, (4) functions, (5) object-oriented programming, (6) pointers and references, (7) dynamic memory allocation, (8) file input/output, (9) exception handling, and (10) the standard template library. The snippets are intended as simple examples to demonstrate key ideas in each module.

Uploaded by

Arshad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views5 pages

Code Topic Wise C+++

The document provides code snippets for 10 modules that cover fundamental C++ concepts: (1) basic input/output, (2) control flow, (3) arrays and strings, (4) functions, (5) object-oriented programming, (6) pointers and references, (7) dynamic memory allocation, (8) file input/output, (9) exception handling, and (10) the standard template library. The snippets are intended as simple examples to demonstrate key ideas in each module.

Uploaded by

Arshad Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Certainly!

Here is a compilation of simple code snippets for all the modules:

Module I: Basic Input/Output


‘‘‘c++
#include <iostream>

int main() {
int num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << "Sum: " << num1 + num2 << std::endl;
return 0;
}
‘‘‘

Module II: Control Flow


‘‘‘c++
#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;

if (num > 0) {
std::cout << "Positive" << std::endl;
} else if (num < 0) {
std::cout << "Negative" << std::endl;
} else {
std::cout << "Zero" << std::endl;
}

return 0;
}
‘‘‘

Module III: Arrays and Strings


‘‘‘c++
#include <iostream>
#include <string>

int main() {
int nums[5] = {1, 2, 3, 4, 5};
std::cout << "Element at index 2: " << nums[2] << std::endl;

std::string greeting = "Hello";


std::cout << "Length of greeting: " << greeting.length() << std::endl;

return 0;
}
‘‘‘

Module IV: Functions


‘‘‘c++
#include <iostream>
// Function to add two numbers
int add(int a, int b) {
return a + b;
}

int main() {
int num1 = 5;
int num2 = 7;
int sum = add(num1, num2);
std::cout << "Sum: " << sum << std::endl;

return 0;
}
‘‘‘

Module V: Object-Oriented Programming


‘‘‘c++
#include <iostream>
#include <string>

// Class definition for a simple student


class Student {
public:
std::string name;
int age;
float gpa;

void display() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "GPA: " << gpa << std::endl;
}
};

int main() {
Student student1;
student1.name = "John";
student1.age = 20;
student1.gpa = 3.5;
student1.display();

return 0;
}
‘‘‘

Module VI: Pointers and References


‘‘‘c++
#include <iostream>

int main() {
int num1 = 10;
int* ptr1 = &num1;
std::cout << "Value of num1: " << *ptr1 << std::endl;
int num2 = 20;
int& ref1 = num2;
std::cout << "Value of num2: " << ref1 << std::endl;

*ptr1 = 30;
std::cout << "Value of num1 after update: " << num1 << std::endl;

return 0;
}
‘‘‘

Module VII: Dynamic Memory Allocation


‘‘‘c++
#include <iostream>

int main() {
int* ptr = new int;
*ptr = 42;
std::cout << "Value at pointer: " << *ptr << std::endl;

int* arr = new int[5];


arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;

std::cout << "Array values: ";


for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

delete ptr; // Deallocate memory pointed by ptr


delete[] arr; // Deallocate memory pointed by arr

return 0;
}

Module VIII: File Input/Output


‘‘‘c++
#include <iostream>
#include <fstream>

int main() {
std::ofstream outfile("output.txt"); // Create an output file stream
if (outfile.is_open()) {
outfile << "Hello, world!" << std::endl;
outfile.close(); // Close the output file stream
} else {
std::cout << "Failed to open file for writing!" << std::endl;
}

std::ifstream infile("input.txt"); // Create an input file stream


if (infile.is_open()) {
std::string line;
while (std::getline(infile, line)) {
std::cout << line << std::endl;
}
infile.close(); // Close the input file stream
} else {
std::cout << "Failed to open file for reading!" << std::endl;
}

return 0;
}
‘‘‘

Module IX: Exception Handling


‘‘‘c++
#include <iostream>

int main() {
int num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;

try {
if (num2 == 0) {
throw "Division by zero!";
} else {
std::cout << "Result: " << num1 / num2 << std::endl;
}
} catch (const char* error) {
std::cout << "Error: " << error << std::endl;
}

return 0;
}
‘‘‘

Module X: Standard Template Library (STL)


‘‘‘c++
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> numbers = {5, 2, 8, 1, 9, 3};

// Sort the vector


std::sort(numbers.begin(), numbers.end());

// Print the sorted vector


std::cout << "Sorted numbers: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
‘‘‘

Note: These code snippets are intended to be simple examples and may not cover all edge cases or error
handling. They are meant to provide a basic understanding of the concepts covered in each module. It’s
always a good practice to thoroughly understand the concepts and write robust code in real-world scenari
os.

You might also like