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

Sacred Thought

26 April 2024  Dear friends, I write the explanation of two verses of Geets for all of you, I hope you all will like it and benefit from it....