none
How's is my code RRS feed

  • Question

  • // Written by Aadit Kapoor
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    struct Complex2;
    struct Complex3;
    struct Complex
    {
    Complex(int n) { num1 = n; }
    private:
    int num1;
    public:
    friend Complex3 operator+(Complex &c, Complex2 &c2);

    };
    struct Complex2
    {
    Complex2(int y){ num2 = y; }
    private:
    int num2;
    public:
    friend Complex3 operator+(Complex &c, Complex2 &c2);
    };

    struct Complex3
    {
    int sum;
    friend Complex3 operator+(Complex &c, Complex2 &c2)
    {
    Complex3 temp;
    temp.sum= c.num1 + c2.num2;
    return (temp);
    }
    };

    int main()
    {
    Complex c(12); // Complex::num1 = 12
    Complex2 c2(12); // Complex2::num2 = 12
    Complex3 c3 = c + c2; // Complex3::sum = 24 

    cout << "Sum = " << c3.sum << endl; // 24

    cin.ignore(1);

    return 0;
    }
    Saturday, September 21, 2013 2:00 PM