Programming Fundamentals Assignment
Name: Rizwan
Roll No: 161
Section: BSCS A3
Subject: Programming Fundamentals
Q1. Mention the different data types present in C++?
C++ has many data types used to store different kinds of data. The main types are int, float, double,
char, and bool. It also has derived types like arrays, pointers, and functions. We can also create our
own types using struct, class, and enum. Each data type has a fixed size and use depending on the
value stored.
Q2. State the difference between C and C++?
C is a procedure-based language, while C++ is object-oriented. C focuses on functions, but C++
focuses on objects and classes. C++ has features like inheritance, polymorphism, and
encapsulation. C does not support these object-oriented features. C++ also supports function
overloading and templates, making it more advanced.
Q3. State and explain what are classes and objects in C++?
A class is a model or blueprint that defines data and functions. An object is a copy or example of a
class that uses real values. Classes help group data and code together in one unit. For example, a
class ‘Car’ may have speed and color. Objects like car1 and car2 use those features with their own
values.
Q4. What is operator overloading? List with the help of examples.
Operator overloading means giving special meaning to operators like + or - for objects. It allows you
to use operators with user-defined data types. For example, we can add two complex numbers
using the + operator. It makes the code easier and more natural to read.
Q5. What is polymorphism in C++? State and explain.
Polymorphism means one name having many forms. In C++, it allows one function or operator to
work in different ways. It can happen at compile-time (like function overloading) or run-time (like
virtual functions). For example, the same function name can work for different data types. It makes
programs flexible and easy to extend.
Q6. What do you mean by abstraction in C++? State and explain.
Abstraction means showing only important details and hiding the rest. It helps in reducing
complexity for the user. We use classes and access specifiers like public and private for
abstraction. For example, when you start a car, you don’t need to know how the engine works. You
just use the start button — that’s abstraction.
Q7. State inheritance in C++.
Inheritance means one class can use the features of another class. The main class is called the
base class, and the new one is the derived class. It helps in reusing code and saving time. There
are many types like single, multiple, multilevel, and hybrid. It’s one of the main parts of
object-oriented programming.
Q8. Write C++ Program to swap two numbers and find the output.
Program: #include using namespace std; int main() { int a = 5, b = 10, temp; temp = a; a = b; b =
temp; cout << 'After swapping: a=' << a << ', b=' << b; } Output: After swapping: a=10, b=5
Q9. Write C++ Program to check whether an alphabet is Vowel or Consonant and find the
output.
Program: #include using namespace std; int main() { char ch = 'a';
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') cout << 'Vowel'; else cout << 'Consonant'; } Output:
Vowel
Q10. Write a C++ Program to find an LCM and find the output.
Program: #include using namespace std; int main() { int a = 4, b = 6, lcm; lcm = (a > b) ? a : b;
while(true) { if(lcm % a == 0 && lcm % b == 0) break; lcm++; } cout << 'LCM = ' << lcm; } Output:
LCM = 12