Sunday, March 2, 2014

Initialization of data with different data types in class

#include<iostream>
using namespace std;
 class Test {
    int &t,i;
    char *str;
    const int x;
    static int y;
public:
    Test(int &t):t(t),i(0),str(new char[10]),x(10) {}  //Initializer list must be used
    int getT() { return t; }
};
 int Test::y=0;
int main() {
    int x = 20;
    Test t1(x);
    cout<<t1.getT()<<endl;
    x = 30;
    cout<<t1.getT()<<endl;
    return 0;
}

Type Casting in C++

static_cast