Sunday, February 25, 2018

C ++ Program : Type casting operator

#include <iostream>
using namespace std;
class A {
    int i;
public:
    A(int ai) : i(ai) {}
    int get() const { return i; }
    void update() { ++i; }
};
class B {
    int i;
public:
    B(int ai) : i(ai) {}
    int get() const { return i; }
B( const A x){ i= x.get();}
operator A(){get();}
 
 
 
 

           
  
    void update() { ++i; }
};
int main() {
    int i;
    cin >> i;
    A a(i++);
    B b(i);
    const B &r = static_cast<B>(a);
    a.update();
    cout << a.get() << ":";
    cout << r.get() << ":";
    const A &s = static_cast<A>(b);
    b.update();
    cout << b.get() << ":";
    cout << s.get() << ":";
    return 0;
}

No comments:

Post a Comment

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...