Professional Documents
Culture Documents
Operators are symbols which take one or two or three operands or expressions and
performs arithmetic or logical computations.
There are three type of operators in 'C', they are: unary operators binary opera
tors and trenary operators.
Operands are variables or expressions which are used as parameters for the opera
tors to evaluate the expression.
Expressions can be a combination of operands and operator in a legal structure.
Statements are combination of expressions and keywords.
What is polymorphism?
'Polymorphism' is an object oriented term. Polymorphism may be defined as the ab
ility of related objects to respond to the same message with different, but appr
opriate actions. In other words, polymorphism means taking more than one form.
Polymorphism leads to two important aspects in Object Oriented terminology - Fun
ction Overloading and Function Overriding.
Overloading is the practice of supplying more than one definition for a given fu
nction name in the same scope. The compiler is left to pick the appropriate vers
ion of the function or operator based on the arguments with which it is called.
Overriding refers to the modifications made in the sub class to the inherited me
thods from the base class to change their behaviour.
What is operator overloading?
When an operator is overloaded, it takes on an additional meaning relative to a
certain class. But it can still retain all of its old meanings.
Examples:
1) The operators >> and << may be used for I/O operations because in the <iostre
am> header, they are overloaded.
2) In a stack class it is possible to overload the + operattor so that it append
s the contents of one stack to the contents of another. But the + operator still
retains its original meaning relative to other types of data.
Declare a function pointer which takes a pointer to char as an argument and retu
rns a void pointer
void* (*func_ptr)(char* parameter)
Type-define a function pointer which takes a int and float as parameter and retu
rns a float *.
i think should define in this way:
typedef float*(*PF)(int , float );
and use PF as pointer to function.( of one int argument, one floaat argument, re
turning float *)
like PF a;
the pointer to function can be type defined as:
typedef float*(*pf)(int a, float b) tagPF;
What does the following C statement do?
while(*c++ = *d++); assuming c and d are pointers to characters.
The loop will be executed until d reaches a null character
string copy
string copy utill zero appears
String copy is performed indeed but be careful with the space allocated for the
destination string.
Check this example:
char s1[10]="abcde";
char s2[3];
char* c,*d;
c=s2;
d=s1;
while(*c++ = *d++);
printf("%s - %s\n",s1,s2);
How do you call a C module within a C++ module.
You should use extern "C" for functions, compiled by C compiler and called withi
n a C++ class. You should do that to force the linker to resolve the function na
me (precisely, the mangling of the name) correctly.
extern "C" {
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <whatever.h>
......
};
What is the difference between run time binding and compile time binding? Discus
s.
I guess this is about dynamic binding and static binding.
Dynamic Binding :
The address of the functions are determined at runtime rather than @ compile tim
e. This is also known as "Late Binding".
Static Binding :
The address of the functions are determined at compile time rather than @ run ti
me. This is also known as "Early Binding"
Static binding in C++ is achieved through Operator overloading where as runtime
binding is achieved through virtual functions.
Question:
All classes A, B, C have default constructor, foo() that calls parent foo() and
allocates 100 bytes to their own private local variable, and a destructor that f
rees the 100 bytes. I create a C object and then destroy it. What's the problem?
Did all the memory get freed? What if I create C, cast to A, and then destroy i
t. How would I make sure memory is freed? (destructor must be "virtual" and each
destructor must call parent destructor)
In this case all memory will not be freed.
To avoid this problem declare the destructor as Virtual
What errors are caught at compile time vs link time?
Syntactical and symantical errors (code errors) are caught at compile time.
Dependency errors (for example resolving function calls or errors in including o
ther modules) are caught at link time
code errors
What is the value of "a" after this?
int (*a) [10];
a++;
int (*a)[10]; represents the declaration of a pointer to an array of ten integer
s. So the value of a is initially some address allocated by the compiler and don
't rely on the fact the address is 0 as it happens for static variables.
The value can be zero if you add the "static" keyword in front of declaration bu
t I don't advise you to further use this pointer to access some elements.
If the integer is n bytes ( 2 or 4 depending on the language) it is true that th
e value of a will be increase with 10*n.
Test this program to understand:
#include <stdio.h>
void main(int argc,char*argv[])
{
int b[10]={1,2,3,4};
int (*a)[10];
printf("%p\n",a);
printf("%d\n",(*a)[0]);
a++;
printf("%p\n",a);
a=&b;
printf("%p\n",a);
printf("%d\n",(*a)[0]);
}
What is wrong with this?
main(){
int *ptr;
*ptr=10;
}The memory is not allocated...before assigning it a value
Given int n, i=10, j=20, x=3, y = 100;
What is the value of n and y at the end of each of the following expressions?
a) n = (i > j) && (x < ++y);
b) n = (j - i) && (x < y++);
c) n = (i < j)
1> n = 0, y = 100, second condition will not be evaluated.
2> n = 1, y = 101
3> n = 1, y = 100
What's the difference between C and C++?
C does not have a class/object concept.
C++ provides data abstraction, data encapsulation, Inheritance and Polymorphism.
C++ supports all C syntax.
In C passing value to a function is
"Call by Value" whereas in C++ its "Call by Reference"
File extention is .c in C while .cpp in C++.(C++ compiler compiles the files wit
h .c extention but C compiler can not!)
In C structures can not have contain functions declarations. In C++ structures a
re like classes, so declaring functions is legal and allowed.
C++ can have inline/virtual functions for the classes.
In addition to Herbert Schildt book these are few differences.
C is Top Down Approach
C++ is Bottom Up Programming approach.
C does not support OOP (Object Oriented Programming) and do not support PolyMorp
hism, Inheritence, Encapsulation, Function OverLoading.
Some common C style commands and there corresponding C++ style commands are show
n below.
Console I/O
***********
C
===
printf("Hello World!\n");
scanf("%s", name);
C++
===
cout << "Hello World!" << endl;
cin >> name;
Comments
********
C
===
/* comment */
C++
===
// comment
File extensions
***************
C
===
.c, .h
C++
===
.C, .h, .CPP, .HPP
File I/O
*********
C
===
out = fopen("output_file.dat", "wb");
in = fopen("input_file.dat", "rb");
C++
===
ofstream out("output_file.dat");
ifstream in("input_file.dat");
Dynamic Memory
**************
C
===
text = (char *) malloc(1000);
free(text);
C++
===
text = new char[1000];
delete [] text;
Constants
*********
C
===
#define PI 3.14159
C++
===
const float PI = 3.14159;
Macros
******
C
===
#define MAX(a,b) ((a) > (b) ? (a) : (b))
C++
===
inline int MAX(int a, int b) { return a > b ? a : b; }
A function call in
C is call by value, while in C++ it is call by reference.
C supports only call by value that's true..
but C++ supports both call by value and call by reference not just call by refer
ence
Is it possible to keep 2 stacks in a single array, if one grows from position on
e of the array, and the other grows from the last position. Write a procedure PU
SH(x,s) that pushes element x onto stack S, where S is one or the other of these
two stacks. Include all necessary error checks
Is it possible to keep 2 stacks in a single array, if one grows from position on
e of the array, and the other grows from the last position. Write a procedure PU
SH(x,s) that pushes element x onto stack S, where S is one or the other of these
two stacks. Include all necessary error checks
Its better to use nodes here - in this way you use what is called the dynamic da
ta structure. This means the size of the structure grows at run time.
Start by defining a pointer to node structure.
each node has a data field and pointer field