You must Sign In to post a response.
  • Exam question on C++ program to read, add and display complex data


    Unsure of how to answer a programming-related query in a previous year's exam paper of Bengaluru University? Let our software programming experts assist you in solving the exam question.

    Following is the question asked in the Bengaluru University B.E. (E&E)- III semester question paper for January 2018 examination. This question is asked for 12 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a C++ program to define a class complex with real and imaginary as data members and get_data(), add(), and display_data() as member functions to read, add and display complex object.
  • Answers

    1 Answers found.
  • #include
    using namespace std;

    class Complex {
    private:
    float real;
    float imaginary;

    public:
    // Function to get data
    void get_data() {
    cout << "Enter real part: ";
    cin >> real;
    cout << "Enter imaginary part: ";
    cin >> imaginary;
    }

    // Function to add two complex numbers
    void add(Complex c1, Complex c2) {
    real = c1.real + c2.real;
    imaginary = c1.imaginary + c2.imaginary;
    }

    // Function to display the complex number
    void display_data() {
    cout << "Sum of complex numbers: " << real << " + " << imaginary << "i" << endl;
    }
    };

    int main() {
    Complex complex1, complex2, result;

    cout << "Enter details for the first complex number:\n";
    complex1.get_data();

    cout << "Enter details for the second complex number:\n";
    complex2.get_data();

    result.add(complex1, complex2);

    result.display_data();

    return 0;
    }

    Fear is The Path to Dark Side.


  • Sign In to post your comments