C++ Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1. What is the difference between C and C++?

o Answer: C++ is an extension of the C programming language. C++ supports


object-oriented programming features like classes and objects, whereas C is
a procedural programming language. Additionally, C++ supports features like
function overloading, inheritance, and polymorphism, which are not present in
C.
2. What is object-oriented programming?
o Answer: Object-oriented programming (OOP) is a programming paradigm
based on the concept of "objects", which can contain data in the form of fields
and code in the form of procedures. The primary principles of OOP include
encapsulation, inheritance, and polymorphism.
3. What is a class and an object in C++?
o Answer: A class is a blueprint for creating objects in C++. It defines the
attributes (data members) and methods (functions) that will be associated
with objects of that class. An object is an instance of a class, which can be
created using the class blueprint.
4. What is inheritance in C++?
o Answer: Inheritance is a feature of object-oriented programming in which a
class (called a child or derived class) inherits attributes and methods from
another class (called a parent or base class). This allows the child class to
reuse code and extend the functionality of the parent class.
5. What is polymorphism in C++?
o Answer: Polymorphism is a feature of OOP that allows objects to be treated
as instances of their parent class. There are two types of polymorphism:
compile-time polymorphism (achieved through function overloading and
operator overloading) and runtime polymorphism (achieved through function
overriding and virtual functions).
6. What is a constructor and destructor in C++?
o Answer: A constructor is a special member function of a class that is
automatically called when an object of that class is created. It is used to
initialize the object's data members. A destructor is a special member function
of a class that is automatically called when an object is destroyed. It is used
to release resources acquired by the object.
7. What is a pointer in C++?
o Answer: A pointer is a variable that stores the memory address of another
variable. Pointers are used to manipulate memory and to create dynamic data
structures like linked lists and trees.
8. What is the difference between pass by value and pass by reference in C++?
o Answer: Pass by value involves passing a copy of the actual parameter to a
function, whereas pass by reference involves passing the memory address of
the actual parameter. Pass by reference allows the function to modify the
actual parameter, while pass by value does not.
9. What is a virtual function in C++?
o Answer: A virtual function is a member function of a class that is declared as
virtual in the base class and can be overridden in the derived classes. Virtual
functions enable runtime polymorphism, allowing the correct function to be
called based on the type of object being referred to.
10. What is the difference between malloc() and new in C++?
o Answer: malloc() is a function in C used to allocate memory dynamically,
while new is an operator in C++ used to allocate memory dynamically for
objects. new automatically calls the constructor for the allocated memory,
while malloc() does not. Additionally, new returns a pointer to the allocated
memory of the specified type, whereas malloc() returns a void pointer that
needs to be typecasted.

1. Factorial of a Number: Write a C++ function to calculate the factorial of a non-


negative integer.

#include <iostream>
using namespace std;

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;
cout << "Enter a non-negative integer: ";
cin >> num;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

2. Palindrome Check: Write a C++ function to check if a given string is a palindrome or


not.

#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string str) {


int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str[left] != str[right])
return false;
left++;
right--;
}
return true;
}

int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isPalindrome(str))
cout << str << " is a palindrome" << endl;
else
cout << str << " is not a palindrome" << endl;
return 0;
}

3. Reverse a String: Write a C++ function to reverse a given string.

#include <iostream>
#include <string>
using namespace std;

string reverseString(string str) {


int n = str.length();
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
return str;
}

int main() {
string str;
cout << "Enter a string: ";
cin >> str;
cout << "Reversed string: " << reverseString(str) << endl;
return 0;
}

4. Fibonacci Series: Write a C++ function to print the Fibonacci series up to a given
number of terms.

#include <iostream>
using namespace std;

void fibonacci(int n) {
int prev = 0, curr = 1, next;
cout << "Fibonacci Series up to " << n << " terms: ";
for (int i = 0; i < n; i++) {
cout << prev << " ";
next = prev + curr;
prev = curr;
curr = next;
}
cout << endl;
}

int main() {
int numTerms;
cout << "Enter the number of terms: ";
cin >> numTerms;
fibonacci(numTerms);
return 0;
}

5. Sum of Array Elements: Write a C++ function to find the sum of all elements in an
array.

#include <iostream>

using namespace std;

int arraySum(int arr[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of array elements: " << arraySum(arr, size) << endl;
return 0;
}

6. Check Prime Number: Write a C++ function to check whether a given number is
prime or not.

#include <iostream>
using namespace std;

bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num))
cout << num << " is a prime number" << endl;
else
cout << num << " is not a prime number" << endl;
return 0;
}
7. Count Vowels and Consonants: Write a C++ function to count the number of
vowels and consonants in a given string.

#include <iostream>

#include <cctype>
using namespace std;

void countVowelsConsonants(string str) {


int vowels = 0, consonants = 0;
for (char ch : str) {
if (isalpha(ch)) {
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
}

int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
countVowelsConsonants(str);
return 0;
}

8. Find Maximum and Minimum Element in an Array: Write a C++ function to find
the maximum and minimum elements in an array.

#include <iostream>

#include <climits>
using namespace std;

void findMaxMin(int arr[], int size) {


int maxNum = INT_MIN;
int minNum = INT_MAX;
for (int i = 0; i < size; i++) {
if (arr[i] > maxNum)
maxNum = arr[i];
if (arr[i] < minNum)
minNum = arr[i];
}
cout << "Maximum element: " << maxNum << endl;
cout << "Minimum element: " << minNum << endl;
}

int main() {
int arr[] = {3, 1, 8, 4, 9, 2};
int size = sizeof(arr) / sizeof(arr[0]);
findMaxMin(arr, size);
return 0;
}

9. Check Armstrong Number: Write a C++ function to check whether a given number
is an Armstrong number or not.

#include <iostream>

#include <cmath>
using namespace std;

bool isArmstrong(int num) {


int originalNum = num;
int numDigits = 0;
int sum = 0;
while (num > 0) {
numDigits++;
num /= 10;
}
num = originalNum;
while (num > 0) {
int digit = num % 10;
sum += pow(digit, numDigits);
num /= 10;
}
return sum == originalNum;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isArmstrong(num))
cout << num << " is an Armstrong number" << endl;
else
cout << num << " is not an Armstrong number" << endl;
return 0;
}

10. Binary Search: Write a C++ function to perform binary search on a sorted array.

#include <iostream>
using namespace std;

int binarySearch(int arr[], int size, int target) {


int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

int main() {
int arr[] = {1, 3, 5, 7, 9, 11};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 7;
int index = binarySearch(arr, size, target);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found" << endl;
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy