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. Namespace
  • 2. Function Overloading
  • 3. Inline Functions
  • 4. Default Arguments
  • 5. Classes and Objects
  • 6. Constructors and Destructors
  • 7. Dynamic Memory Allocation
  • 8. Friend Functions
  • Conclusion
  1. 3. Programming Language and Its Applications

3.3 C++ Language Constructs with Objects and Classes

1. Namespace

  • A namespace organizes code and prevents naming conflicts between variables, functions, or classes.

  • Example: The standard C++ library is contained within the std namespace.

#include <iostream>
// Using a namespace
namespace MyNamespace {
    int value = 100;
}

int main() {
    std::cout << "Value in MyNamespace: " << MyNamespace::value << std::endl;
    // Output: Value in MyNamespace: 100
    return 0;
}

2. Function Overloading

  • Function overloading allows multiple functions with the same name but different parameter types or numbers of parameters.

  • The compiler differentiates between them based on the argument list.

#include <iostream>
using namespace std;

// Function Overloading Example
void display(int num) {
    cout << "Integer: " << num << endl; // Output: Integer: 5
}

void display(double num) {
    cout << "Double: " << num << endl; // Output: Double: 5.5
}

int main() {
    display(5);    // Calls the integer version
    display(5.5);  // Calls the double version
    return 0;
}

3. Inline Functions

  • Inline functions reduce function call overhead by embedding the function code directly into the calling code during compilation.

  • Use the inline keyword before the function definition.

#include <iostream>
using namespace std;

inline int square(int x) {
    return x * x;  // Inline function logic
}

int main() {
    cout << "Square of 5: " << square(5) << endl; // Output: Square of 5: 25
    return 0;
}

4. Default Arguments

  • Default arguments allow a function to be called with fewer arguments by specifying default values for some parameters.

  • Default arguments must be specified from right to left in the parameter list.

#include <iostream>
using namespace std;

// Function with Default Arguments
void greet(string name = "Guest") {
    cout << "Hello, " << name << "!" << endl;
}

int main() {
    greet();          // Output: Hello, Guest!
    greet("Alice");   // Output: Hello, Alice!
    return 0;
}

5. Classes and Objects

  • Classes are user-defined data types that represent real-world entities, encapsulating data members and member functions.

  • Objects are instances of classes.

#include <iostream>
using namespace std;

class Rectangle {
public:
    int length, width;

    // Member function
    int area() {
        return length * width;
    }
};

int main() {
    Rectangle rect;       // Create an object of the class
    rect.length = 10;
    rect.width = 5;

    cout << "Area: " << rect.area() << endl; // Output: Area: 50
    return 0;
}

6. Constructors and Destructors

  • Constructors initialize objects when they are created. They share the same name as the class and have no return type.

  • Destructors clean up resources when the object is destroyed. They have the same name as the class but are preceded by a ~.

#include <iostream>
using namespace std;

class Demo {
public:
    Demo() { // Constructor
        cout << "Constructor called!" << endl; 
    }
    ~Demo() { // Destructor
        cout << "Destructor called!" << endl;
    }
};

int main() {
    Demo obj; // Constructor is called here
    // Destructor is automatically called at the end of the program
    return 0;
}

7. Dynamic Memory Allocation

  • C++ provides new and delete operators for dynamic memory allocation and deallocation.

#include <iostream>
using namespace std;

int main() {
    // Allocate memory dynamically
    int* ptr = new int(42); 
    cout << "Value: " << *ptr << endl; // Output: Value: 42

    delete ptr; // Deallocate memory
    return 0;
}

8. Friend Functions

  • A friend function has access to private and protected members of a class. It is declared using the friend keyword inside the class.

#include <iostream>
using namespace std;

class Box {
private:
    int length;
public:
    Box(int l) : length(l) {}
    friend void printLength(Box b); // Friend function declaration
};

void printLength(Box b) {
    cout << "Length: " << b.length << endl; // Access private member
}

int main() {
    Box box(10);
    printLength(box); // Output: Length: 10
    return 0;
}

Conclusion

C++ is a powerful object-oriented programming language that supports advanced features such as namespaces, function overloading, inline functions, default arguments, classes and objects, constructors and destructors, dynamic memory allocation, and friend functions. These features provide flexibility, reusability, and efficiency, enabling developers to create robust and scalable software. With its rich standard library and versatile constructs, C++ remains a top choice for applications ranging from system software to complex simulations and games.

Previous3.2 Pointers, Structures, and Data FilesNext3.4 Features of Object-Oriented Programming

Last updated 5 months ago