C++ Assignments

Que1. What is C++? Differentiate OOPs and POPs.

C++ is an object-oriented programming language developed by Bjarne Stroustrup as an extension of C language.

Differences between OOPs and POPs:

  • OOPs focuses on objects and data, while POPs focuses on functions and logic
  • OOPs supports inheritance and polymorphism, POPs doesn’t
  • OOPs provides better data security through encapsulation, POPs has limited data security
  • OOPs promotes code reusability, POPs has limited reusability

Que2. What is Object oriented programming? Explain its features.

Object-oriented programming is a programming paradigm based on objects that contain data and code.

Main features:

  • Encapsulation: Bundling data and methods that operate on that data
  • Inheritance: Creating new classes from existing ones
  • Polymorphism: Ability of objects to take multiple forms
  • Abstraction: Hiding complex implementation details

Que3. Explain the following
 Data Types and its classification
 Variable and identifier
 Input and output function
 Type casting

a) Data Types and classification:

  • Primary: int, char, float, double, bool
  • Derived: array, pointer, function
  • User-defined: class, structure, union, enum

b) Variable and identifier:

  • Variable: Named storage location in memory
  • Identifier: Name given to program elements (variables, functions, etc.)

c) Input/output function:

  • Input: cin>> (extraction operator)
  • Output: cout<< (insertion operator)

d) Type casting: Converting one data type to another

  • Implicit casting: Automatic conversion
  • Explicit casting: Manual conversion using (type)value

Que4. What is operator and operator precedence? Explain the types of operator with example.

Operators are symbols that perform operations. Types:

  • Arithmetic (+, -, *, /, %)
  • Relational (<, >, ==, !=)
  • Logical (&&, ||, !)
  • Assignment (=, +=, -=)
  • Precedence: Order in which operations are performed (*, / before +, -)

Que5. What is class and object? Write a program to demonstrate class and object.

class Student {
    private:
        int roll;
        string name;
    public:
        void getData() {
            cin >> roll >> name;
        }
};

int main() {
    Student s1; //s1 is object
    s1.getData();
    return 0;
}

Que6. What is control statement? Explain the types of ‘IF STATEMENT’ with its program.

Control statements are used to control the flow of program execution. There are three main types:

  1. Selection/Decision Statements
  2. Iteration/Loop Statements
  3. Jump Statements
//Simple if
if(age >= 18) {
    cout << "Can vote";
}

//if-else
if(num % 2 == 0) {
    cout << "Even";
} else {
    cout << "Odd";
}

//else-if ladder
if(marks >= 90) cout << "A";
else if(marks >= 80) cout << "B";
else cout << "C";

Que7. What is looping? Differentiate the While and DO While loop using a suitable program.

Looping is a programming construct that allows repetitive execution of a block of code multiple times based on a condition. C++ has three main types of loops:

  1. For Loop
  2. While Loop
  3. Do while loop
//While loop
int i = 1;
while(i <= 5) {
    cout << i++;
} //Output: 12345

//Do-While loop
int j = 1;
do {
    cout << j++;
} while(j <= 5); //Executes at least once

Que8. Write a program for simple calculator using switch statement.

#include<iostream.h>
#include<conio.h>
void main()
{
  char op;
  int a=500,b=300;
switch(op) {
    case '+': result = a + b; break;
    case '-': result = a - b; break;
    case '*': result = a * b; break;
    case '/': result = a / b; break;
    default: cout << "Invalid";
}
getch();
}

Que9. Write a program to print 1 to 1000 numbers using for loop.

#include<iostream.h>
#include<conio.h>
void main()
{
for(int i = 1; i <= 1000; i++) {
    cout << i << " ";
}
getch();
}

Que10. What is Array? Write a program to demonstrate 2D array.

int arr[2][3] = {{1,2,3}, {4,5,6}};
for(int i = 0; i < 2; i++)
    for(int j = 0; j < 3; j++)
        cout << arr[i][j];

Que11. What is Pointer? Explain its uses and Write a program to demonstrate the use of pointer.

int x = 10;
int *ptr = &x;
cout << *ptr; //Value at address

Que12. What is Constructor and destructor? Write a program to demonstrate the Constructor and
Destructor.

class Test {
    public:
        Test() { cout << "Constructor"; } //Constructor
        ~Test() { cout << "Destructor"; } //Destructor
};

Que13. Explain friend class and friend function. Write a program to demonstrate friend class and
function.

Let me explain friend class and friend function in C++ concisely.

Friend Function and Friend Class are features in C++ that allow access to private and protected members of a class from outside the class.

class Box {
    private:
        int data;
    friend void print(Box b); //Friend function
};

Que14. What is polymorphism? Explain its types.

Polymorphism means “many forms” and allows objects/functions to behave differently in different contexts. There are two main types:

Compile-time: Function overloading, operator overloading

Runtime: Function overriding through virtual functions

Que15. Write a program to demonstrate the compile time polymorphism.

class Math {
    int add(int a, int b) { return a + b; }
    float add(float a, float b) { return a + b; }
};

Que17. What is Inheritance? Explain its types.

Inheritance is a fundamental concept in Object-Oriented Programming where a class (called derived/child class) can inherit properties and methods from another class (called base/parent class). It promotes code reusability and establishes a relationship between classes.

Types of Inheritance

Single

Multiple

Multilevel

Hierarchical

Hybrid

Que22. Explain Exception handling. Write a program to demonstrate the exception handling.

Exception handling is a mechanism to deal with runtime errors or exceptional situations in a program. It helps maintain normal flow of the program by separating error-handling code from regular code.

Key components:

  1. try: Contains code that might throw an exception
  2. throw: Used to raise an exception when a problem occurs
  3. catch: Handles the exception that was thrown
try {
    if(x == 0) throw "Division by zero";
    result = a/x;
}
catch(const char* msg) {
    cout << msg;
}

Leave a Reply