You are on page 1of 7

What is Operator, Operand, Expression, Statement in 'C'?

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.

Also Polymorphism can be achieved in C++ through operator overloading


What are templates?
C++ Templates allow u to generate families of functions or classes that can oper
ate on a variety of different data types, freeing you from the need to create a
separate function or class for each type. Using templates, u have the convenienc
e of writing a single generic function or class definition, which the compiler a
utomatically translates into a specific version of the function or class, for ea
ch of the different data types that your program actually uses.
Many data structures and algorithms can be defined independently of the type of
data they work with. You can increase the amount of shared code by separating da
ta-dependent portions from data-independent portions, and templates were introdu
ced to help you do that
Declare a void pointer
void *malloc(size_t number_of_bytes);
I think the answer is simply
void* p;
malloc is just the library function called to allocated some memory and of cours
e a void pointer will be returned , but it is the declaration of a void pointer.

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.

In static binding,the compiler decides which function to invoke during compile t


ime itself
Compile Time Binding : In case of operator overloading and function overloading
the name of the function is resolved during the compile time . Even if there are
two or more functions with the same name the compiler mangles the name so that
each function is uniquely identified . This has to be resolved at compile time a
nd is known as compile-time binding or early binding.
Run Time Binding : In case of polymorphism (virtual functions) if a base class p
ointer(or reference) is allocated a pointer(or reference) of derived class the a
ctual function called is determined only during runtime through the virtual tabl
e entry . This is runtime binding or late binding
Compare and contrast C++ and Java
1>Platform Independent : Java code is said to be a multiplatform code and can ru
n on any platform because after the compilation of the source code byte code(s)
are created rather than a binary code so it can run on any platform which suppor
ts JVM concept but on the contrast at time(s) it slows down the application trem
endously
2> Garbage Collection : Java handles freeing up of the memory but this is not gu
ranteed since the GC thread has the lowest priority
3>Operator Overloading : is not provided in Java,but what are the advantages of
Operator Overloading but one may question what are its advantages, well it makes
a more readable and a modular code. In c++ cin and cout objects can also be ove
rloaded which again leads to a better readability and flexibility
4> Multiple Inheritance : Java does provide multiple inheritance in form of Inte
rfaces, In Java a class can not inherit from more than one class but it definite
ly can implement any number of interfaces
5> Templates: in c++ give such a lot of flexibility and avoids redundant coding
which again is not provided by Java
Why does C/C++ give better run-time performance then Java?
That's because the Java bytecode is interpreted, not compiled. Programs
written in C are compiled into binaries which can be executed by a specific comp
uter processor. Programs
written in Java require one more step -- they must be interpreted by the Java "v
irtual machine" before running
on a particular computer architecture. As a result, a computer running a Java pr
ogram has to execute more
machine-language instructions to do the same amount of work than a computer runn
ing an equivalent
program written in C
Does C++ come with in-built threading support
No. C++ does not support in-built Multithreading. To do so, you must use the ope
rating system functions manually
Class A derived B derived C. All have foo(). I cast C to A and call foo(). What
happens?
it depends. if in A foo is defined as virtual function. then call C's foo(),
if it doesn't defined virtual, then
call A's foo()
Actually, if access is NOT specified, it deafults to private derivation. In priv
ate derivation, binding is static. So, whether foo is declared virtual or not it
still defaults to static binding. So, A->foo() is called.
However, If a public derivation is specified from C <-- B and B <-- A, then if f
oo() is virtual C->foo() is called; if foo() is non virtual A->foo() is called.

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

You might also like