hi guys i am having trouble with a walk through. The part i am having trouble with is the c_lang class print function. I don't understand what str[shift] = str[shift] - (x - '0'); is doing. Specifically the '0' is messing me up. The first character is send in is an x. so i do str[0]=x(the string at 0)-('x'-'0'). This some how becomes v and i have no clue why.
Code:class c_lang { protected: int shift; char *str; public: c_lang(char *s) { str = s; // storing the ADDRESS of 's' (be careful!) shift = 0; cout << "#include <stdio.h>" << endl; } virtual void print(char x) { str[shift] = str[shift] - (x - '0'); shift++; } virtual void operator<<(char *); ~c_lang( ) { cout << str << endl; } }; void c_lang::operator<<(char *s) { for(int i=0; s[i]; i++) { str[i] = str[i] - 2; cout << s[i]; } } class c_plus_plus : public c_lang { // derived from base protected: // class (2nd level) int len, pos; public: c_plus_plus(char *s, int p) : c_lang(s) { len = strlen(s); pos = p; cout << "using namespace::std;" << endl; } virtual void print(char y) { str[pos] = str[pos] + (y - '0'); cout << str << endl; } virtual void operator<<(char *); }; void c_plus_plus::operator<<(char *s) { for(int i=0; i<len; i++) { print(s[i]); pos++; } } class c_sharp : public c_plus_plus { // derived from previously // derived class (3rd level) public: c_sharp(char *s, int p) : c_plus_plus(s, p) { cout << "using System;" << endl; } virtual void operator<<(char *); }; void c_sharp::operator<<(char *s) { for(int i=0; i<len; i++) { c_lang::print(s[i]); } } void compile(c_lang &r, char *s) { r << s; } int main( ) { char L1[10]="xszfiis 9", L2[10]="kba-0301b"; c_sharp cs(L1, 0); c_plus_plus cpp(L2, 0); compile(cs, "241524103"); compile(cpp, "312014002"); cout << "---------" << endl; return 0; }



LinkBack URL
About LinkBacks


