Difference between


C++ Struct & Class
C++ Struct Class
Default access specifier is Public Default access specifier is Private


C Struct & C++ struct
C Struct C++ Stuct
No access specifiers Access specifiers can be present like class
Cannot have functions Can have functions like class
Keyword struct must while creating instances of struct type Keyword struct not necessary while creating objects of struct type(But not an error to use so)


C Const & C++ Const
C Const C++ Const
A C Const occupies a storage(value got at runtime only) and is global.So always a const statement is a definition(storage allocated).Doesnt necessarily create storage.If no storage is created values can be folded into the code , for greater efficiency,after typechecking.If you take address of a const(by passing to fn that accepts ref) or you define it as extern and initialize(only if initializer is there it is a definition else only decl) then storage is created.
Defaults to external linkage for const.Compiler can assume that definition is else where and only declaration here.Defaults to internal linkage for const.To accomplish external linkage use keyword extern for const.(diff from C and other c++ identifiers)
Not very useful.C forces you to use #define in preprocessor.Not so.


Overloading & Overriding
Overloading functions Overriding virtual functions
All overloaded functions are available for use in different situations Overriding function overrides the base/older functions and so older versions are hidden.
Accomplishes Compile time polymorphism Accomplishes Run time polymorphism
Return type may or maynot be different.But number of arguments or type of arguments must differ.(Two fns cannot be overloaded when one takes reference parameter and other takes normal parameter) Function prototype must match exactly with overrided function(same Fn name,args,ret type;Just they are placeholder fns in base class.)
Constructors can be overloaded Constructors cannot be overrided.Destructors can be.
No such conditions Must be nonstatic member functions and so cannot be friend function.


Inheritance & Composition
Inheritance Composition
New class is of base class type.(is-a relationship) New class is composed of base class object(has a relationship)
Usually done to maintain common interface.Sometimes to reuse code. Usually done to take a general purpose class and specialize according to need.
Derived class object can access base class member directly (if that member is not redefined)or through scope resolution operator(if that member is redefined in the derived class).[Redefined members hide base versions(///r to function overriding)] Derived class object can access base class members through the embedded object's name with dot operator.Or access the derived class member which in turn accesses the base class member.[You cannot redefine a base class member]
Constructor initializer list calls base class constructors(by mentioning class name) after the constructor arguments list and a colon. Constructor initializer list calls member object constructors(by mentioning object's name) after the constructor arguments list and a colon.
When creating objects,all base class constructors are called automatically. Call to constructors of member objects must be made explicitly thru constructor initializer list.No other way to call the constructors.
Order of constructor calls starts at the very root of the hierarchy.Order of constructor calls is unaffected by order of constructor calls in the initializer list.Order is determined by the order that the member objects are declared in the class.
Accomplished at Compile timeAccomplished at Runtime.


Early binding & Late binding
Early binding Late binding
Refers to events that occur at compile time.Can create programs that respond to events that occur while program executes.
All information needed to call a function is known at compile time.Object and function are bound during compilation.Function calls are not resolved until runtime(virtual fn).(Actual function called is determined by the type of object pointed to by the pointer).Object and function are not linked until runtime.
Advantage is efficiencySlower exectution time.Advantage is flexibility.


C & C++ Though C + OOP + STL = C++, this 42 points list gives a gist of some other appealing differences between them.
C C++
Structured programming language.Object oriented programming language and thus has inheritance,polymorphism,Operator overloading,'This' pointer,Conversion function to convert object into required type, etc.
STL not vailable. Templates can be used for generic programming.
We check explicitly for error occurance and stop the program. Exception handling is available and we can change program execution for error recovery.(Need not stop the program)
General style of program is
#includes
function declarations
main( )
{...}
function definitions
General style of program is
#includes
base class declarations
derived class declarations(means class defn.since no memeory allocated its called declaration)
nonmember fn prototypes
main( )
{...}
non member function definitions
(in large programs all class declarations will be put in seperate header file and included with each module)
Header files have .h extension. Like C. But also Includes header files from standard Namespace.(older '.h' header files are included from global library.)No ' .h' extension because '.h' is redundant to '<>' in saying it is header file included.These files without '.h' are automatically mapped to latest file in compiler.It is still used to have compatibility with those code.(%s is like cin>> ,takes input upto a space).There are more files in standard library.
If same name used in outer and inner scopes,not necessarily inner scope's vaiable/array is used in the inner scope. 'Name Hiding' present.If same name is used in outer and inner scopes,inner scope's vaiable/array is used in the inner scope.
Variables inside nested structures are treated as having the same scope, when used from outside the nest. Inner scope's variable is not available directly,outside the outer scope.(only thru :: operator)
No such operator. Scope resolution operator :: available.
30 keywords(eg).In C, some c++ keywords are maros defined in standard headers .eg.wchar_t. C's keywords + 32 keywords.Few among other keywords are bool,mutable,asm,explicit,using.
Program form is different.input is thru 'scanf' statement. cin.getline(x,256,'\n').(C type i/p is getline(cin,x,'\n')).
Static keyword used for local scope.source file Namespacing available for scope manipulations.(using keyword)
Use static keyword for mentioning that a variable is local.Source files and header files are also used. Static keyword is deprecated(encouraged NOT to use).Instead use namespacing.
No such thing. 'Default argument' available.i.e.,If no value is passed for an argument a particular value mentioned in the arg list against that argument can be used.
f(2) poor C code. f(2) is an erroneous statement.
const a=5; gets implicit int type in older C. No default type in ISO C and C++.
Struct like datatypes can have definition in the argument list or return type Different rules for defining.
Allows jumps to bypass initializationInitialization cannot be bypassed.
No '0' allocation for array elements with no initializers. Static array and global array elements that donot have initializers are assigned '0'.
Not must. Named constants must have initializers at the time of definition.
Local variables can be dynamically initialized.Not global variables.(atoi("234"),i+4 etc)Both local and global variables can be dynamically initialized.
global constant by default has external linkage. 'extern' keyword is must for using outside a file.If no keyword then only compile time/internal linkage.
No such thing.Linkage specification of language is possible(tell the copiler to link a fn as Fortran or C ) globally.
Declarations can be done as many times(not good) and all must be present before any action statement begins.Declarations must be done only once and local variables can be declared anywhere in the program before usage.(A class name, and typedef name of different type, cannot be same in same scope)
Function declaration is a must.(Here declaration means argument list can be empty meaning it states nothing about the args.)Function prototyping is a must.(Prototype is an exact match to function return type and argument list at the time of declaration and usage)(empty arg list means 'void')
Void return type supported for main.ISO standard requires main to be specified as int always, though some compilers accept default returntype as int and dont groan when int is not mentioned explicitly.A void return type or no return type is not standard.
Though obsolete classic form of function declaration is supported.
func(a,b)
int a;
int b;
{....}
Only modern form supported.
func(int a,int b)
{...}.
keyword struct must be mentioned to create instances of structkeyword 'struct' not necessary,only tagname of struct is enough while creating struct instances.
No such thing.'unnamed arguments' are possible.placeholder arguments-No identifier.
Macros are available.Instead 'inline' functions are available.
All variable declarations happen only at compile time.Object declaration happens at runtime.
Lesser type checking.Run time type identification is not possible.More sophisticated type checking.(void* etc).Unions also have extra type checking.RTTI is possible.
void* is allowed on the right hand side to be assigned to any other pointer type or to be used as an operand of an assignment statement.Not allowed without a type cast.
Not explicit.Function type casts and 4 explicit type casts.(static,reinterpret,const typecasts can do what C casts can do.)onemore thing is dynamic cast.
No restriction.There is a restriction to use 'variable argument listing'(ellipses '...' )(Naturally, C++ has prototyping)
enum instance's values are int.enum instance is a distinct datatype. Not int.Less use of enum bcos of 'class' availability.
Memory allocation is through malloc().We have to check seperately for availability.New() checks itself for availability and returns 'bad_alloc' allocation error, if no memory is available.(older c++ returned 0)
Size of character constant and size of enumeraion ,equals size of int. Sizeof(a) equals sizeof(char).enumeration size can be choosed by implementation.
A character constant is automatically elevated to an integerNot so.
You can call main from within your programNot allowed.
An identifier may be upto 31 characters long.No limits.
You cannot take the address of a register variable. You can.
Strings are represented by character arrays.Last element is always null. Strings have special datatype 'string'.No '\0' at end.
Has 'goto' statements.Has i/o manipulation statements.
RPC & Sockets
Sha1sum & Md5

Hosted by www.Geocities.ws

1