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. Pure Virtual Functions
  • 2. Dynamic Binding
  • 3. File Input/Output Operations
  • 4. Stream Class Hierarchy
  • Conclusion
  1. 3. Programming Language and Its Applications

3.5 Pure Virtual Functions and File Handling

This section introduces pure virtual functions, dynamic binding, and the fundamentals of file input/output operations using C++. The concept of stream classes is also explained to help manage file operations effectively.


1. Pure Virtual Functions

A pure virtual function is a function declared within a base class that has no definition relative to the base class. It enforces the concept of abstraction and makes a class abstract.

  • A pure virtual function is declared using the syntax:

    virtual return_type function_name() = 0;
  • Any class containing at least one pure virtual function is called an abstract class, and objects of such classes cannot be instantiated.

Example: Pure virtual function and dynamic binding.

#include <iostream>
using namespace std;

// Abstract base class
class Shape {
public:
    virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing a Circle" << endl;
    }
};

class Rectangle : public Shape {
public:
    void draw() override {
        cout << "Drawing a Rectangle" << endl;
    }
};

int main() {
    Shape* shape1 = new Circle();
    Shape* shape2 = new Rectangle();

    shape1->draw(); // Output: Drawing a Circle (Dynamic Binding)
    shape2->draw(); // Output: Drawing a Rectangle (Dynamic Binding)

    delete shape1;
    delete shape2;
    return 0;
}

2. Dynamic Binding

Dynamic binding ensures that the correct method is called for a derived class object when the object is accessed through a pointer or reference to the base class. This is achieved using virtual functions.

  • It allows runtime polymorphism.

  • Ensures that the appropriate overridden function is called for an object.


3. File Input/Output Operations

C++ provides the <fstream> library for file handling. The key classes for file I/O are:

  • ifstream: For input (reading from a file).

  • ofstream: For output (writing to a file).

  • fstream: For both input and output.

File Write Example

#include <iostream>
#include <ofstream> // File stream library
using namespace std;

int main() {
    ofstream outFile("example.txt"); // Open file for writing
    if (!outFile) {
        cout << "Error opening file!" << endl;
        return 1;
    }

    outFile << "Hello, File Handling in C++!" << endl;
    outFile << "This is a test file." << endl;

    outFile.close(); // Close file after writing
    cout << "Data written to file successfully!" << endl;

    return 0;
}

The file contains:

Hello, File Handling in C++!
This is a test file.

File Read Example

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

int main() {
    ifstream inFile("example.txt"); // Open file for reading
    if (!inFile) {
        cout << "Error opening file!" << endl;
        return 1;
    }

    string line;
    while (getline(inFile, line)) { // Read line by line
        cout << line << endl;
    }

    inFile.close(); // Close file after reading
    return 0;
}

The file contains:

Hello, File Handling in C++!
This is a test file.

File Append Example

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

int main() {
    ofstream outFile("example.txt", ios::app); // Open file in append mode
    if (!outFile) {
        cout << "Error opening file!" << endl;
        return 1;
    }

    outFile << "Appending more data to the file." << endl;

    outFile.close();
    cout << "Data appended successfully!" << endl;

    return 0;
}

The file now contains:

Hello, File Handling in C++!
This is a test file.
Appending more data to the file.

4. Stream Class Hierarchy

C++ provides a hierarchy of stream classes to manage input and output operations:

ios_base
   |
   +--> ios
         |
         +--> istream (input stream)
         |      |
         |      +--> ifstream (file input stream)
         |
         +--> ostream (output stream)
                |
                +--> ofstream (file output stream)
                |
                +--> iostream (input/output stream)
                       |
                       +--> fstream (file input/output stream)

Conclusion

  • Pure Virtual Functions:

    • Declared with = 0 in the base class.

    • Makes a class abstract and ensures derived classes implement specific functionality.

  • Dynamic Binding: Ensures runtime polymorphism through virtual functions.

  • File Handling:

    • ifstream, ofstream, and fstream are key classes for file I/O.

    • Files can be opened in modes such as ios::in, ios::out, or ios::app.

  • Stream Class Hierarchy: Defines how different streams (standard and file-based) interact.

Previous3.4 Features of Object-Oriented ProgrammingNext3.6 Generic Programming and Exception Handling

Last updated 5 months ago