This discussion will not only cover the implementation of these programs but also delve into the underlying principles of C++, providing a comprehensive understanding suitable for beginners.
- Basic Input/Output Program
A simple program that demonstrates basic input and output in C++ can help understand how data is received from the user and then displayed back.
#include <iostream>
int main() {
int number;
std::cout << “Enter a number: “;
std::cin >> number;
std::cout << “You entered: ” << number << std::endl;
return 0;
}
Explanation:
This program introduces cin for input and cout for output, fundamental components of the iostream library. It showcases how to prompt the user for a number and then display that number back to the screen.
- Basic Arithmetic Calculator
A simple calculator that performs basic arithmetic operations demonstrates the use of arithmetic operators and conditional statements.
#include <iostream>
int main() {
double num1, num2;
char operation;
std::cout << “Enter first number, operator, and second number: “;
std::cin >> num1 >> operation >> num2;
switch(operation) {
case ‘+’:
std::cout << “Result: ” << (num1 + num2);
break;
case ‘-‘:
std::cout << “Result: ” << (num1 – num2);
break;
case ‘*’:
std::cout << “Result: ” << (num1 * num2);
break;
case ‘/’:
if(num2 != 0.0)
std::cout << “Result: ” << (num1 / num2);
else
std::cout << “Cannot divide by zero”;
break;
default:
std::cout << “Invalid operator”;
}
std::cout << std::endl;
return 0;
}
Explanation:
This program introduces the switch statement for conditional logic based on the operator entered by the user. It handles addition, subtraction, multiplication, and division, including a check to prevent division by zero.
- Looping Constructs: Counting Program
A counting program that uses a loop to count to a number specified by the user demonstrates the use of loops.
#include <iostream>
int main() {
int countTo;
std::cout << “Enter a number to count to: “;
std::cin >> countTo;
for(int i = 1; i <= countTo; ++i) {
std::cout << i << ” “;
}
std::cout << std::endl;
return 0;
}
Explanation:
This program introduces the for loop, a fundamental control structure for iterating a set number of times. It counts from 1 to the number entered by the user.
- Functions: Factorial Calculator
A program that calculates the factorial of a number using a function demonstrates the definition and invocation of functions.
#include <iostream>
int factorial(int n) {
if(n <= 1) return 1;
else return n * factorial(n – 1);
}
int main() {
int number;
std::cout << “Enter a number to find its factorial: “;
std::cin >> number;
std::cout << “Factorial of ” << number << ” is ” << factorial(number) << std::endl;
return 0;
}
Explanation: This program introduces functions, recursion, and the if-else statement. It calculates the factorial of a number using a recursive function, demonstrating a fundamental algorithm in computer science.
- Arrays and Searching: Linear Search
A program that searches for an element in an array using linear search demonstrates the use of arrays and basic search algorithms.
#include <iostream>
int linearSearch(int arr[], int size, int searchKey) {
for(int i = 0; i < size; ++i) {
if(arr[i] == searchKey) return i;
}
return -1;
}
int main() {
int arr[] = {1, 3, 5, 7, 9};
int searchKey;
std::cout << “Enter a number to search for: “;
std::cin >> searchKey;
int result = linearSearch(arr, 5, searchKey);
if(result != -1)
std::cout << “Number found at index: ” << result << std::endl;
else
std::cout << “Number not found” << std::endl;
return 0;
}
Explanation:
This program introduces arrays and a basic searching algorithm, linear search. It searches for an element within an array and returns its index or indicates if the element is not found.
- Object-Oriented Programming: Basic Class
A simple program that defines a class and uses it to create and manipulate an object demonstrates the basics of object-oriented programming.
#include <iostream>
class Box {
public:
double length;
double breadth;
double height;
// Constructor
Box() : length(1), breadth(1), height(1) {} // Default constructor
double volume() {
return length * breadth * height;
}
};
int main() {
Box box1; // Create an object of Box
box1.length = 2.5;
box1.breadth = 3.5;
box1.height = 4.5;
std::cout << “Volume of box1: ” << box1.volume() << std::endl;
return 0;
}
Explanation:
This program introduces the concept of classes and objects, the cornerstone of object-oriented programming in C++. It defines a Box class with properties and a method to calculate its volume, demonstrating encapsulation and the use of constructors.