ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Removed Areas > C / C++

Exploring C++

<< < (3/3)

arunpawar:
I'm going thru confusion after reading herbt scildt book of complete reference.It causesmore
pain than cure.I have some of the questions which are not properly answered by this book,can
you take a look at that & let me know you perspective.

1. What #include<filename> can do?can we use our own C++ file to include in our program,is
it possible to access classes from that file,what about main function in that file,how it
will be treated?

2. What is preprocessor directive?Where and why it is used?(I want to know the situations
and limitations of this thing)

3. When to use which loops i.e when to use "for" or "While" loop?(Actually i think using
"while" in most of the cases will put you into infinite loop mode if you go wrong,right)

4. What are cin,cout (Operators or objects of iostream) in comparison with printf and
scanf?Simmilarly what are scanf & printf?

5.What is Strong type checking & Type casting? (Both sounds simmilar but  type casting  is
rarely used while type checking is abt the code we write ,am i right?)

6.What is late binding & Dynamic binding?(I understand thru books is that till run time your
code is unknown to compiler is known as dynamic binding,i think i m right here.)

7.What is reference variable? What is the major use of this variable?

8.What is Namespace? what are these?
I have searched the internet for them but unable to get quite beginner friendly tutorial for
it.

Just let me know about these concepts of C++.Just post your perspectives about them.

f0dder:
#1: "#include <file.h>" does exactly the same thing as opening file.h and copy-pasting it where the "#include <file.h>" statement is located. No more, no less.

#2: preprocessor directives are the things that start with a "#" hash sign. It can be used for preprocessor macros (you generally want to stay away from those), pragmas to control compiler-specific behavour, the classic #ifdef/#endif, and perhaps the most important, #include.

#3: you use loops when you need a loop? :P

#4: cin,cout are C++ iostreams, which are a lot more flexible than C-style scanf/printf (they let you write your own output formatters, and a lot more). They also have the potential to be buffer-overflow safe, which scanf/printf don't.

#5: type checking is done all the time by the compiler, to ensure you don't shoot of your leg doing something stupid. C has pretty weak strong type checking, C++ has relatively strong but not over-insane type checking (contrast that to Pascal which has 'char' and 'byte' types that need typecasting >_<). You use typecasting when you need to convert one "unrelated" type to another - this doesn't happen often with decently designed C++ code, but happens a lot if you need to interface with legacy C code. Prime example is using the PlatformSDK for Windows.

#6: dynamic binding/polymorphism... let's say you have a whole class of output streams you can support (to file, to console, to network, to memory...). You design an interface class for this, with all virtual functions. You can't instantiate this interface class directly, but you can instantiate the concrete derived classes (ie, FileStream, NetworkStream, ...), but all code that uses the streams can use the GenericStream class interface. Using dynamic binding, the "generic" calls are bound to the "concrete" calls of the class you're using.

That explanation was probably a bit confusing, but it's pretty simple in practice.

#7: a reference variable is similar to a pointer variable, but they're not equal. One thing is the syntactic sugar: with a pointer, you need to do "*ptr = value" or "ptr->field = value". With a reference, you can simply do "ref = value" or "ref.field = value". Also, references can't be reassigned, and they don't take up any additional memory - the are the variables they refer to. Think of them as aliases.

#8: namespaces are wonderful :-* basically, they were created to avoid polluting the global namespace, and having variable/function/class name clashes in large projects, or when using libraries. If two people wrote functions called SuperFormatter(), you'd usually be in trouble, but if they were put in separate namespaces, you aren't.

PS: is this a homework assignment?

nosh:
Error C:\DC\ARUNPAWAR.CPP 001: Undefined symbol 'herbt scildt'

SCILDT SUX!!! C0M p1lers scuk ev3n M0re!
 

arunpawar:
 ;D
I never ask homework stuff cause i'm graduated with electronics engg major.

Navigation

[0] Message Index

[*] Previous page

Go to full version