Wednesday, January 14, 2009

In Java
field and member function

In C++
Member and member function

in Java function has to be included inside a class but in C++ free functions are possible in a program.
One such free function in c++ is main.
In Program you need to use namespace as well as include iostream.
To print output "cout" is used.
cout [[ x [[ y [[ z [[ endl;
This function is left-associative so value is evaluated from left to right.

x=y=z=0;
assignment operator is right associative so first z is set to 0 followed by x and y.

To call a free function from any other function or class in the program, no class declaration is required. Just calling by name of the function will suffice but a prototypical declaration of function will be required (as similar to c). Either the function implementation should be placed before the function call or a forward declaration should be there.

if program involves more than one file then g++ file1.c file2.c can be used to generate a final single executable.
One more option is to create a separate object files using -c option then link them at later. By this, if later any file changes then we don't need to recompile all the files.

g++ -c hello.c ====> hello.o
g++ -c hello1.c ===> hello1.o

g++ hello.o hello1.o

C++ types is combination of both C and Java types:
1. primitive types
2. arrays
3. enum
4. structure
5. union
6. pointer
7. classes
new type can be defined using typedef.

C++ supports c type interpretation of integer values where they can be considered as boolean values for conditional statement. This might lead to assignment related error like
if (x = 0) // we wanted to compare x to 0 but by mistake we made it like x= 0 which is syntactically correct but hard to debug.
To resolve this one should write constant first instead of variable.
if (x == 0) should be practiced to be written like this if (0 == x).


C++ arrays lacks many important feature which are useful in Java.
For example:
no length operation.
no dynamic resizing of array.
No run time check on out-of-bound index.
Not possible to assign one array to another. No array copy possible.
However one can use vector class in STL or define there own such class which can provide all these functionality.

Array declaration in C++
int arr[10]; // array of 10 int variable;

In java brackets can precede the variable name but in C++ it is not so.
int[10] arr; // java correct but in C++ incorrect.
In java array declaration includes size so memory is allocated at same moment.

Think of a real life programming situation where union keyword is efficient to use?

In C++ null is upper-case but in java it is lower case value;
In C++ if structure declaration is like:
struct x {
int x1;
int x2;
};
x x3, x4; ======> valid in C++ but in C this should be like struct x x3, x4;

There is no garbage collection mechanism provided in C++ so we need to deallocate heap memory by your own. use Delete keyword for this purpose;

A subtle case to consider:

int *p, *q;
p = new int;
q = p;
delete q;
*p = 5; here p is a dangling pointer.

also 2nd case can be:
int *p = new int;
*p = null
This will leave memory leak in the program.
To debug pointer use purify.

To allocate space to pointer using dynamic allocation, use like this:
int *p = new int[10]; // array of 10 pointers.
delete[] p;

code snippet to read input file and output file:

#include

int main(){
int x;
ifstream infile;
infile.open("input.dat");
if (infile.fail()){
cerr << "error in opening file" << endl;
exit(1);
}
while(infile >> x)
some operations

// to read character by character
while(infile.get(c))

}

Important point to note and remember:
Input and outputstream are always passed by reference.

In Java parameter are passed by values only but in C++ they can be passed by
1. value
2. reference
3. Const- reference
for example:

void func(int a, int &b, const int &c);
here a is pass by value
b is pass by reference
c is cons-reference

A wonderful concept is this:
IN the following example, even though pointer is an address to a memory location. When such pointer is passed by value, value stored at the address stored by pointer can change but pointer it self does not change.

void f(int *q){
*q = 5;
q = null;
}

main() {
int *p;
int x = 2;

*p = &x
f(p);
// value of x = 5 but p != null
}

Parameter should be passed by reference if any function has to return multiple values.
Const-reference is used when you don't want function to modify parameter value but you want to avoid making copy of parameter becuase parameter is large or resource overhead might be high.

Const-reference may lead to compile-time error so all the member functions should be const as well. Arrays are always passed by reference. To pass an array by value one should user Vector.
Always put a practice of keeping general code in c files and declaration in header files. Include
header files in this way:
#include "prog1.h"

class implementation should be divided in 2 files.
header file (.h) should include the definition and declaration. Generally variables and methods are declared public and private according to their usage.
.c file should include all the implementation of API's and member function specified in corresponding header file.
Unlike java class definition should end in semicolon.
Constructor should have same name as class.

class IntList {
public:
IntList();
void AddToEnd(int k);
void print(ostream &out) const;

private:
int x1;
int x2;
int x3;
static const int SIZE = 10; // initial size of the array
int *Items; // Items will point to the dynamically allocated array
int numItems; // number of items currently in the list
int arraySize; // the current size of the array

};

here print is declared as const bacause in case if the member function are not changing any variable then function should be declared as const.
if a situation is like this
void f(const IntList &L) {
L.Print(cout);
}
here if Print is not declared to be const then compiler will throw some compile time error.
C file will look like this:

#include "prog.h"

IntList::IntList():Items(new int[SIZE]), numItems(0), arraySize(SIZE) {
}

void IntList::AddToEnd(int k) {
...
}

void IntList::Print(ostream &output) const {
...
}

Inthe above code, one must not forget to include the header file.
standard library header files should be included in angle brackets while quotes should be used to include your own file.
The way constructor code is written:
1. Member initialization list
2. Initialization inside constructor body.

Some confusion on use of Member initialization list.

String in C++
include [string] at the top of program
strings can be declared with or without initial value
String s1 or String s1("hello") or String s1 = "hello"

s1.size()
s1 == s2 [comparison]
+ for concatenating.....
size() for calculating size;

string can be indexed as well.

No comments:

Post a Comment