InterviewSolution
| 1. |
Give keywords introduced by language c++ which are not in c language |
|
Answer» below are some features that are found in C++, not found in C, but still have nothing to do with Object Oriented Programming.CastsIn C, if you want to cast an int to a long int, for example, you'd use int i=0; long l = (long) i; In C++, you can use a function-like call to make the cast. long l = long(i); It's easier to read. Since it's possible to create functions to perform casts involving user-defined types, this makes all the casts look consistent. For example, you may have a user-defined type -- complex numbers. You have a function that accepts an integer and casts it to a complex number: 1 --> 1 + 0i (real part is 1 and imaginary part is 0) Suppose your function call is named 'complex', then it may look like: Complex x; int i=1; x = complex(i); Flexible DeclarationsIn C, all var declarations within a SCOPE occur at the beginning of that scope. Thus, all global declartions MUST appear before any functions, and any local declarations must be made before any executable statements.C++, on the other hand, allows you to mix data declarations with functions and executable statements. E.g. In C,void makeit(void) { float i; char *cp; /* imagine 2000 lines of code here */ /* allocate 100 bytes for cp */ cp = malloc(100); /* 1st use of cp */ for (i=0; i<100; ++i) /* 1st use of i */ { /* do something */ } /* more code */ } In C++, void makeit(void) { // 2000 lines of code char *cp = new char[100]; for (int i=1; i<10; i++) { } } 'struct' and 'union' TagsIn C, we would have this segment: struct foo {int a; float b;} struct foo f; This declares a struct with the tag name 'foo' and then creates an instance of foo named f. Notice when you DECLARE var of that struct, you have to say 'struct foo'. In C++, struct and union tags are considered to be type name, just as if they had been declared by the 'typedef' statement. struct foo {int a; float b;} foo f; which is EQUIVALENT to the following in C: typedef struct { int a; float b; } foo; foo f; |
|