computer-nec-license
  • NEC-Computer
  • 1. Concept of Basic Electrical and Electronics Engineering
    • 1.1 Basic Concepts
    • 1.2 Network Theorems
    • 1.3 Alternating Current Fundamentals
    • 1.4 Semiconductor Device
    • 1.5 Signal Generator
    • 1.6 Amplifiers
    • MCQs
      • MCQs On Basic Electrical
        • set-1
        • set-2
      • MCQs On Basic Electronics
        • set-1
        • set-2
  • 2. Digital Logic and Microprocessor
    • 2.1 Digital Logic
    • 2.2 Combinational & Arithmetic Circuit
    • 2.3 Sequential Logic Circuits
    • 2.4 Microprocessor
    • 2.5 Microprocessor System
    • 2.6 Interrupt Operations
    • MCQs
      • MCQs On Digital Logic
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
        • set-7
        • set-8
        • set-9
        • set-10
        • set-11
        • set-12
      • MCQs On Microprocessor
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
        • set-7
        • set-8
        • set-9
  • 3. Programming Language and Its Applications
    • 3.1 Introduction to C Programming
    • 3.2 Pointers, Structures, and Data Files
    • 3.3 C++ Language Constructs with Objects and Classes
    • 3.4 Features of Object-Oriented Programming
    • 3.5 Pure Virtual Functions and File Handling
    • 3.6 Generic Programming and Exception Handling
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
  • 4. Computer Organization and Embedded System
    • 4.1 Control and CPU
    • 4.2 Computer Arithmetic and Memory System
    • 4.3 I/O Organization and Multiprocessor
    • 4.4 Embedded System Design
    • 4.5 Real-Time Operating and Control Systems
    • 4.6 Hardware Description Language (VHDL) and IC Technology
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
      • set-6
      • set-7
      • set-8
      • set-9
      • set-10
      • set-11
  • 5. Concept of Computer Network and Network Security System
    • 5.1 Introduction to Computer Networks
    • 5.2 Data Link Layer
    • 5.3 Network Layer
    • 5.4 Transport Layer
    • 5.5 Application Layer
    • 5.6 Network Security
    • MCQs
      • Basic Networking
        • set-1
        • set-2
      • Advanced Networking
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
  • 6. Theory of Computation and Computer Graphics
    • 6.1 Introduction to Finite Automata
    • 6.2 Introduction to Context-Free Languages (CFL)
    • 6.3 Turing Machines (TM)
    • 6.4 Introduction to Computer Graphics
    • 6.5 Two-Dimensional Transformation
    • 6.6 Three-Dimensional Transformation
    • MCQs
      • MCQs on Theory of Computation
        • set-1
        • set-2
        • set-3
      • MCQs On Computer Graphics
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
  • 7. Data Structures and Algorithm, Database System and Operating System
    • 7.1 Introduction to Data Structures, Lists, Linked Lists, and Trees
    • 7.2 Sorting, Searching, Hashing and Graphs
    • 7.3 Introduction to Data Models, Normalization, and SQL
    • 7.4 Transaction Processing, Concurrency Control, and Crash Recovery
    • 7.5 Introduction to Operating System and Process Management
    • 7.6 Memory Management, File Systems, and System Administration
    • MCQs
      • MCQs ON DSA
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
      • MCQs On DBMS
        • set-1
        • set-2
      • MCQs On Operating System
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
        • set-7
        • set-8
        • set-9
        • set-10
        • set-11
        • set-12
  • 8. Software Engineering and Object-Oriented Analysis & Design
    • 8.1 Software Process and Requirements
    • 8.2 Software Design
    • 8.3 Software Testing, Cost Estimation, Quality Management, and Configuration Management
    • 8.4 Object-Oriented Fundamentals and Analysis
    • 8.5 Object-Oriented Design
    • 8.6 Object-Oriented Design Implementation
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
      • set-6
      • set-7
      • set-8
      • set-9
  • 9. Artificial Intelligence and Neural Networks
    • 9.1 Introduction to AI and Intelligent Agents
    • 9.2 Problem Solving and Searching Techniques
    • 9.3 Knowledge Representation
    • 9.4 Expert System and Natural Language Processing
    • 9.5 Machine Learning
    • 9.6 Neural Networks
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
      • set-6
      • set-7
      • set-8
      • set-9
  • 10. Project Planning, Design and Implementation
    • 10.1 Engineering Drawings and Its Concepts
    • 10.2 Engineering Economics
    • 10.3 Project Planning and Scheduling
    • 10.4 Project Management
    • 10.5 Engineering Professional Practice
    • 10.6 Engineering Regulatory Body
    • MCQs
      • MCQs On Engineering Drawing
        • set-1
        • set-2
      • MCQs On Engineering Economics
      • MCQs On Project Planning & Scheduling
      • MCQs On Project Mangement
      • MCQs On Engineering Professional Practice
      • MCQs On Engineering Regulatory Body
  • Questions Sets
    • Set 1 (Chaitra, 2080)
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Set 2 (Aasadh, 2081)
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Set 3 (Asojh, 2080)
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Model Set - Computer Engineering By NEC
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Model Set - Software Engineering By NEC
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
  • Tips & Tricks
Powered by GitBook
On this page
  • 1. Function Templates
  • 2. Class Templates
  • 3. Standard Template Library (STL)
  • 4. Exception Handling
  • Conclusion
  1. 3. Programming Language and Its Applications

3.6 Generic Programming and Exception Handling

This section explores generic programming in C++, which allows writing flexible, reusable code using templates. We also discuss exception handling, which is used to handle errors gracefully using try, catch, and throw mechanisms.


1. Function Templates

A function template allows you to define a function that works with any data type. It acts as a blueprint for creating functions that can operate on different data types.

  • Syntax:

    template <typename T>
    T add(T a, T b) {
        return a + b;
    }

Example: Using function templates.

#include <iostream>
using namespace std;

template <typename T> // Function template for any type T
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add(5, 10) << endl;       // Output: 15 (int)
    cout << add(3.5, 2.5) << endl;     // Output: 6.0 (double)
    cout << add('A', 'B') << endl;     // Output: 131 (char)
    return 0;
}

Output:

15
6
131

2. Class Templates

A class template enables you to define a class that can operate with any data type.

  • Syntax:

    template <typename T>
    class MyClass {
        T data;
    public:
        MyClass(T value) : data(value) {}
        T getData() { return data; }
    };

Example: Using class templates.

#include <iostream>
using namespace std;

template <typename T> // Class template
class MyClass {
    T data;
public:
    MyClass(T value) : data(value) {}
    T getData() { return data; }
};

int main() {
    MyClass<int> obj1(100);          // T is int
    MyClass<double> obj2(3.14);      // T is double

    cout << obj1.getData() << endl;  // Output: 100
    cout << obj2.getData() << endl;  // Output: 3.14

    return 0;
}

Output:

100
3.14

3. Standard Template Library (STL)

C++ provides the Standard Template Library (STL), which is a collection of template classes and functions. It provides useful data structures and algorithms, making it easier to work with collections of data.

Containers

Containers are used to store collections of data. Some common types are:

  • Vector: Dynamic array.

  • List: Doubly linked list.

  • Queue: FIFO structure.

  • Stack: LIFO structure.

  • Map: Key-value pair collection.

Example: Using a vector (dynamic array) container.

#include <iostream>
#include <vector>  // Include vector header
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5}; // Initialize vector with values

    // Iterating through vector
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";  // Output: 1 2 3 4 5
    }
    cout << endl;

    vec.push_back(6); // Adding element to the vector
    cout << "Last element: " << vec.back() << endl;  // Output: 6

    return 0;
}

Output:

1 2 3 4 5 
Last element: 6

Algorithms

The STL provides useful algorithms for operations like sorting, searching, and manipulating data structures.

Example: Using the sort algorithm to sort a vector.

#include <iostream>
#include <vector>
#include <algorithm> // Include for sorting algorithm
using namespace std;

int main() {
    vector<int> vec = {5, 3, 8, 1, 2};

    // Sorting the vector
    sort(vec.begin(), vec.end());  // Sorting in ascending order

    // Display sorted vector
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";  // Output: 1 2 3 5 8
    }
    cout << endl;

    return 0;
}

Output:

1 2 3 5 8

Iterators

Iterators are used to traverse through the elements of a container.

Example: Using iterators to traverse a vector.

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

int main() {
    vector<int> vec = {10, 20, 30, 40};

    // Using iterator to access vector elements
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        cout << *it << " ";  // Output: 10 20 30 40
    }
    cout << endl;

    return 0;
}

Output:

10 20 30 40

4. Exception Handling

Exception handling in C++ helps to manage errors during program execution using the try, catch, and throw mechanisms.

  • try: A block of code that might throw an exception.

  • catch: A block of code that handles the exception.

  • throw: Used to throw an exception.

Syntax:

try {
    // Code that may throw an exception
}
catch (exception_type e) {
    // Code to handle the exception
}

Example: Basic Exception Handling

#include <iostream>
using namespace std;

int divide(int a, int b) {
    if (b == 0) {
        throw "Division by zero error!";  // Throwing an exception
    }
    return a / b;
}

int main() {
    try {
        int result = divide(10, 0);  // This will throw an exception
        cout << "Result: " << result << endl;
    }
    catch (const char* msg) {  // Catching the exception
        cout << "Error: " << msg << endl;  // Output: Error: Division by zero error!
    }

    return 0;
}

Output:

Error: Division by zero error!

Example: Multiple Exceptions

You can have multiple catch blocks to handle different exceptions.

#include <iostream>
using namespace std;

int divide(int a, int b) {
    if (b == 0) {
        throw "Division by zero error!";
    }
    if (a < 0 || b < 0) {
        throw "Negative number error!";
    }
    return a / b;
}

int main() {
    try {
        int result = divide(-10, 0);  // This will throw a negative number error
        cout << "Result: " << result << endl;
    }
    catch (const char* msg) {
        cout << "Error: " << msg << endl;  // Output: Error: Negative number error!
    }

    return 0;
}

Output:

Error: Negative number error!

Conclusion

  • Function Templates: Allow you to create generic functions that can work with any data type.

  • Class Templates: Allow you to define classes that work with different data types, providing flexibility and reusability.

  • STL:

    • Containers: Store collections of data (e.g., vector, list, map).

    • Algorithms: Predefined functions like sort(), find(), reverse() to operate on data in containers.

    • Iterators: Used to traverse containers in a generic way.

  • Exception Handling: Handles errors using try, catch, and throw. It ensures that your program can recover gracefully from runtime errors, including handling multiple types of exceptions.

Previous3.5 Pure Virtual Functions and File HandlingNextMCQs

Last updated 5 months ago