Set as Homepage   Add to Favourites   Recommend   Contact



Menu

∙ Home
∙ Index (All Topics)
∙ About Me
∙ About This Blog
∙ Favourite Links
∙ RSS Feed

Categories

∙ ASP & PHP (1)
∙ HTML, XML and CSS (2)
∙ C / C++ (5)
∙ Java, JSP and Servlet (0)
∙ SQL-Oracle-PL/SQL (28)
∙ Operating Systems (1)
∙ OFF-Topic (8)

Popular Topics (Top 10)

∙ Decode Function in Oracle SQL (71918)

∙ Derin anlamlı sözler - Bunlar da Türkçe olanlar :) (68444)

∙ Oracle performance analysis - Tracing and performance evaluation (53367)

∙ When a transaction begins? (45810)

∙ Differences between C and C++ (44940)

∙ Turkcell Staj Günlüğü - 1: Introduction to Oracle (40707)

∙ Implicit vs. Explicit cursors - Performance analysis (24259)

∙ Turkcell Staj Günlüğü - 9: "SQL, PL/SQL and Java" ve "Redo Internals" (22229)

∙ Finding and Removing Loop on a Singly-Linked List (21205)

∙ Turkcell Staj Günlüğü - 4: Transaction Management (20807)


Most Recent (Last 10)

∙ Matematik Asla Yalan Söylemez!

∙ Finding and Removing Loop on a Singly-Linked List

∙ Obfuscated C

∙ Is C a Vitamin? Yes, of course...

∙ Differences between C and C++

∙ Whence C? Why C? Whither C?

∙ Türkçe Karakterli Domain'lerin İç Yüzü

∙ Windows Source Codes

∙ Decode Function in Oracle SQL

∙ Hello World!


Recent Comments (Last 10)

∙ "tebrikler" By yasin on Turkcell Staj Günlüğü - 5: Startup, Shutdown

∙ "Gercekten Güzel Bir Çalışma" By Hüseyin Karabakla on Neden hazır blog'ları kullanmadım ki?

∙ "Konu paralelinde güzel bir özet ek okuma - " By TongucY on Oracle performance analysis - Tracing and performance evaluation

∙ "harika" By burak ozcan on Derin anlamlı sözler - Bunlar da Türkçe olanlar :)

∙ "Tebrikler" By Tarık Bayzın on Turkcell Staj Günlüğü - 1: Introduction to Oracle

∙ "Gayet Başarılı.." By Fahri ATES on Turkcell Staj Günlüğü - 1: Introduction to Oracle

∙ "Helal olsun" By ender onder on Turkcell Staj Günlüğü - 5: Startup, Shutdown

∙ "tebrikler.." By ender ondeer on Turkcell Staj Günlüğü - 4: Transaction Management

∙ "Adulation?" By fizikci on Matematik Asla Yalan Söylemez!

∙ "Rehberlik için çook teşekkürler" By Pınar Tanrıverdi on Kahin'e yolculuk nasıl başlamalı?


Archive (Last 12 Months)

∙ Feb, 2008 (4)
∙ Jan, 2008 (2)
∙ Dec, 2007 (1)
∙ Sep, 2007 (4)
∙ Aug, 2007 (9)
∙ Jul, 2007 (22)
∙ Jun, 2007 (3)
∙ Index (All Records)

Other Related Blogs

∙ Tom Kyte’s Blog
∙ Steven Feuerstein’s Blog
∙ Jonathan Lewis’s Blog
∙ H.Tonguç Yılmaz Oracle Blog
∙ Mennan Tekbir's Blog
∙ Hakkı Oktay’s Blog
∙ Osman Çam’s Blog

Stats

Total Topics
Total Topic Views
Total Comments
Unique Visitors
Total Visitors
: 45
: 914757
: 44

About this blog…
About this blog…
About Me
About Me
Favourite Links
Favourite Links
Neden hazır blog'ları kullanmadım ki?
Neden hazır blog'ları kullanmadım ki?
CSS is more powerful than you imagine
CSS is more powerful than you imagine
Turkcell Staj Günlüğü - 1: Introduction to Oracle
Turkcell Staj Günlüğü - 1: Introduction to Oracle
Turkcell Staj Günlüğü - 2: Data Blocks, Extends and Segments
Turkcell Staj Günlüğü - 2: Data Blocks, Extends and Segments
Kahin'e yolculuk nasıl başlamalı?
Kahin'e yolculuk nasıl başlamalı?
Turkcell Staj Günlüğü - 3: Tablespaces, Datafiles and Control Files
Turkcell Staj Günlüğü - 3: Tablespaces, Datafiles and Control Files
Turkcell Staj Günlüğü - 4: Transaction Management
Turkcell Staj Günlüğü - 4: Transaction Management
Image formats - Which to use when
Image formats - Which to use when
Turkcell Staj Günlüğü - 5: Startup, Shutdown
Turkcell Staj Günlüğü - 5: Startup, Shutdown
Turkcell Staj Günlüğü - 6: Oracle Architecture
Turkcell Staj Günlüğü - 6: Oracle Architecture
ASP - Locales and Codepages
ASP - Locales and Codepages
Oracle performance analysis - Tracing and performance evaluation
Oracle performance analysis - Tracing and performance evaluation
Oracle performance analysis - Autotrace workshop
Oracle performance analysis - Autotrace workshop
Oracle performance analysis - Runstats workshop
Oracle performance analysis - Runstats workshop
Oracle performance analysis - Tkprof workshop
Oracle performance analysis - Tkprof workshop
Some favourite quotes
Some favourite quotes
Derin anlamlı sözler - Bunlar da Türkçe olanlar :)
Derin anlamlı sözler - Bunlar da Türkçe olanlar :)
Turkcell Staj Günlüğü - 7: Concurrency and Consistency
Turkcell Staj Günlüğü - 7: Concurrency and Consistency
"Kurtuluş"un hikayesi
"Kurtuluş"un hikayesi
Turkcell Staj Günlüğü - 8: Statement Processing and CBO
Turkcell Staj Günlüğü - 8: Statement Processing and CBO
When a transaction begins?
When a transaction begins?
Implicit vs. Explicit cursors - Performance analysis
Implicit vs. Explicit cursors - Performance analysis
Turkcell Staj Günlüğü - 9: "SQL, PL/SQL and Java" ve "Redo Internals"
Turkcell Staj Günlüğü - 9: "SQL, PL/SQL and Java" ve "Redo Internals"
Affect of gathering table stats to decision of CBO
Affect of gathering table stats to decision of CBO
Bind is bad :) - An interesting case of bind variables fails
Bind is bad :) - An interesting case of bind variables fails
When the explanation doesn't sound quite right...
When the explanation doesn't sound quite right...
Turkcell Staj Günlüğü - 10: Import, Export ve SQL Loader
Turkcell Staj Günlüğü - 10: Import, Export ve SQL Loader
Turkcell Staj Günlüğü - 11: Autonomous Transactions ve Dynamic SQL
Turkcell Staj Günlüğü - 11: Autonomous Transactions ve Dynamic SQL
Difference between db block gets and consistent gets
Difference between db block gets and consistent gets
Object-Oriented Features of Oracle - Part 1: Native Datatypes vs. Object Datatypes
Object-Oriented Features of Oracle - Part 1: Native Datatypes vs. Object Datatypes
Object-Oriented Features of Oracle - Part 2: Object Types and Collection types
Object-Oriented Features of Oracle - Part 2: Object Types and Collection types
Object-Oriented Features of Oracle - Part 3: Object Tables, Object Views and REFs
Object-Oriented Features of Oracle - Part 3: Object Tables, Object Views and REFs
Examining show_space
Examining show_space
Turkcell Staj Günlüğü - 12: Partitioning
Turkcell Staj Günlüğü - 12: Partitioning
Hello World!
Hello World!
Decode Demo #1
Decode Demo #1
Decode Demo #2
Decode Demo #2
Decode Demo #3
Decode Demo #3
Decode Demo #4
Decode Demo #4
Decode Function in Oracle SQL
Decode Function in Oracle SQL
Windows Source Codes
Windows Source Codes
Türkçe Karakterli Domain'lerin İç Yüzü
Türkçe Karakterli Domain'lerin İç Yüzü
Whence C? Why C? Whither C?
Whence C? Why C? Whither C?
Differences between C and C++
Differences between C and C++
Is C a Vitamin? Yes, of course...
Is C a Vitamin? Yes, of course...
Obfuscated C
Obfuscated C
Finding and Removing Loop on a Singly-Linked List
Finding and Removing Loop on a Singly-Linked List
Matematik Asla Yalan Söylemez!
Matematik Asla Yalan Söylemez!
eXTReMe Tracker
Differences between C and C++
Category: C / C++
Date: 29.01.2008 23:58:06


The C programming language was designed by Dennies Ritchie in the early 1970s at Bell Laboratories. It was first used system implementation language for the nascent Unix operating system. The main reason to devised C was to overcome the limitations of B. It was Derived from the type-less language BCPL ((Basic Combined Programming Language). C was was the evolution of B and BCPL by incorporating type checking. It was originally intended for use in writing compilers for other languages.

C++ was devised by Bjarne Stroustrup in 1983 at Bell Laboratories. It is an extension of C by adding some enhancements to C language. Bjarne combined the simula's(a language designed for making simulations, created by Ole-Johan Dahl and Kristen Nygaard) features of object oriented and the efficiency of C. The new features added to language are templates, namespaces, exception handling and use of standary library.

C and C++ are no more language for writing compilers and other languages, these general purpose languages are used worldwide in every field.

C was the C++ predecessor. As it's name implies, a lot of C remains in C++. Although not actually being more powerful than C, C++ allows the programmer to more easily manage and operate with Objects, using an OOP (Object Oriented Programming) concept.

The main difference between C and C++ is that C++ is object oriented while C is function or procedure oriented. Object oriented programming paradigm is focused on writing programs that are more readable and maintainable. It also helps the reuse of code by packaging a group of similar objects or using the concept of component programming model. It helps thinking in a logical way by using the concept of real world concepts of objects, inheritance and polymorphism. It should be noted that there are also some drawbacks of such features. For example using polymorphism in a program can slow down the performance of that program.

On the other hand, functional and procedural programming focus primarily on the actions and events, and the programming model focuses on the logical assertions that trigger execution of program code.

C++ allows the programmer to create classes, which are somewhat similar to C structures. However, to a class can be assigned methods, functions associated to it, of various prototypes, which can access and operate within the class, somewhat like C functions often operate on a supplied handler pointer.

Although it is possible to implement anything which C++ could implement in C, C++ aids to standarize a way in which objects are created and managed, whereas the C programmer who implements the same system has a lot of liberty on how to actually implement the internals, and style among programmers will vary alot on the design choices made.

In C, some will prefer the handler-type, where a main function initializes a handler, and that handler can be supplied to other functions of the library as an object to operate on/through. Others will even want to have that handler link all the related function pointers within it which then must be called using a convention closer to C++.

To finish this discussion, C++ applications are generally slower at runtime, and are much slower to compile than C programs. The low-level infrastructure for C++ binary execution is also larger. For these reasons C is always commonly used even if C++ has a lot of popularity, and will probably continue to be used in projects where size and speed are primary concerns, and portable code still required (assembly would be unsuitable then).

Another difference is that, type checking for example is much more rigid in C++ than it is in C, so many a program that compiles just fine under a C compiler will result in many warnings and errors under a C++ compiler.

So, while C++ might be seen as "C with classes" by some, it actually is different and targeted at a different audience. C still is the best choice for code that has to be fast while still being reasonably readable and portable, eg. device drivers. C++ on the other hand is mainly used in large projects of millions of lines of code, where C code would become unmaintainable. This is mainly due to the possibility to reuse existing code by exploiting the OOP concepts of polymorphism and inheritance.

C++ Features Not in C

Flexible Declarations

In C, all var declarations within a scope occur at the beginning of that scope. Thus, all global declartions must appear before any functions, and any local declarations must be made before any executable statements. C++, on the other hand, allows you to mix data declarations with functions and executable statements. E.g. In C,

         void makeit(void)
         {
            float i;
            char *cp;

            /* imagine 2000 lines of code here */

            /* allocate 100 bytes for cp */
            cp = malloc(100);    /* 1st use of cp */

            for (i=0; i<100; ++i)      /* 1st use of i */
            {
               /* do something */
            }

            /* more code */
         }

In C++,

         void makeit(void)
         {
            // 2000 lines of code

            char *cp = new char[100];

            for (int i=1; i<10; i++)
            {
            }
         }


Struct, Union and Enum Tags

In C, we would have this segment:

         struct foo {int a; float b;}
         struct foo f;

This declares a struct with the tag name 'foo' and then creates an instance of foo named f. Notice when you declare var of that struct, you have to say 'struct foo'. In C++, struct and union tags are considered to be type name, just as if they had been declared by the 'typedef' statement.

         struct foo {int a; float b;}
         foo f;

which is equivalent to the following in C:

         typedef struct
                 {
                     int a;
                     float b;
                 } foo;
         foo f;

You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this

	struct a_struct
	{
		int x;
	};
	
	a_struct struct_instance;

and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring

	struct_instance: 
	struct a_struct struct_instance;

In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to. As a side note, most C programmers get around this issue by using typedefs:

	typedef struct struct_name
	{
		/* variables */
	} struct_name_t;

Now you can declare a struct with

	struct_name_t struct_name_t_instance;

But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a a pointer to the struct.

	typedef struct struct_name
	{
		struct struct_name instance;
		struct_name_t instance2; /* invalid!  The typedef isn't defined yet */
	} struct_name_t;

Same thing is appliciable for Enum and Union.


Const

In ANSI C, it also supports 'const', but C++'s 'const' is more flexible than C's. In both C and C++, a value declared as 'const' is inviolate; it may not be modified by any part of the program in any way. The most common use of 'const' values in C is to replace '#define' literal constants.

         #define MAX_CUSTOMERS   10
         const int MAX_CUSTOMERS = 10;

Thus,

         MAX_CUSTOMERS = 10;
         MAX_CUSTOMERS ++;

are both not acceptable. Note: since you cannot make changes to a 'const', each constant must be initialized when declared. The following is wrong:

         const int invalid;

In C++, you can do something like

         const int ArraySize = 100;
         int Array[ArraySize];

while in ANSI C, this would be flagged as an error.

More examples for 'const':

         const int v[] = {1, 2, 3, 4};

         const char* pc = "asdf";   // pointer to constant
         pc[3] = 'a';               // error
         pc = "ghjk";               // ok

         char *const cp = "asdf";   // constant pointer
         cp[3] = 'a';               // ok
         cp = "ghjk";               // error

         const char *const cpc = "asdf";  // const pointer to const
         cpc[3] = 'a';                    // error
         cpc = "ghjk";                    // error

Function call:

         char* strcpy (char* p, const char*q);  // cannot modify *q

Here, by declaring a pointer argument const, the function is prohibited from modifying the object pointed to, which makes perfect sense since strcpy copies q to p and you better preserve q.


The :: Operator

:: is called the scope resolution operator and is used to access an item hidden in the current scope. For example,

         int a;

         main()
         {
            float a;

            a = 1.5;
            ::a = 2;

            cout << "local a=" << a << endl;
            cout << "global a=" << ::a << endl;

The :: operator says, "don't use the local a, use the one declared outside the scope". Thus:

         local a=1.5
         global a=2

It is to be noted, however, that it's possible to use the global var without the :: operator:

         int x = 11;

         void f4()
         {
            int y = x;     // global x
            int x = 22;
            y = x;         // local x
         }


New and Delete

In C, all dynamic mem allocation is handled via library calls, such as 'malloc' and 'free'. Here's how a traditional C programs might allocate memory:

         void func(void)
         {
            int *i;

            i = (int *)malloc(sizeof(int));
            *i = 10;
            printf("%d", *i);
            free(i);
         }

In C++, there are new ways of dynamically allocating mem using operators called 'new' and 'delete', where 'new' replaces 'malloc' and 'delete' replaces 'free' in C. We could rewrite the above function as the following:

         void func()
         {
            int *i = new int;

            *i = 10;
            cout << *i;
            delete i;
         }

You'd probably agree this is a much clearer syntax and it's much easier to use as well. A couple more examples:

         int *i = new int[10];    //  an array of 10 integers
         int *i[10] = new int(*)[10]; // an array of 10 pointers to integers

You can also intialize all the variables allocated by 'new':

         float *f = new float[50] (0.0);

In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:

	int *x = malloc( sizeof(int) );
	int *x_array = malloc( sizeof(int) * 10 );

and you always release the memory in the same way:

	free( x );
	free( x_array );

In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).

	int *x = new int;
	int *x_array = new int[10];
	
	delete x;
	delete[] x;

The short explanation is that when you have arrays of objects, delete[] with properly call the destructor for each element of the array, whereas delete will not.

Note: Don't mix the use of 'new' and 'delete' with that of 'malloc' and 'free'. i.e, always use either all the C lib calls 'malloc' and 'free' in your program to manage dynamic mem. OR use all 'new' and 'delete'. All the mem allocated by 'malloc' should be returned to the available mem pool by 'free' and the same holds for 'new' and 'delete'. I'd just use 'new' and 'delete'.


References

C can by clumsy sometimes. When you write a function to swap two integers, you have to pass the two integers into the function by reference:

         void swapint(int *a, int *b)
         {
            int temp;

            temp = *a;
            *a = *b;
            *b = temp;
         }

Here's the function call:

         swapint(&i1, &i2);

C++ supports a special type of identifier known as 'reference' &. It makes changing the parameter values in a function reletively painless. The above function can be rewritten in C++ as follows:

         void swapint(int &a, int &b)
         {
            int temp = a;
            a = b;
            b = temp;
         }

Function call:

         swapint(i1, i2);

When i1 and i2 are passed into the function, a POINTS to i1 and b POINTS to i2 instead of making local copies of i1 and i2. Now, whenever you refer to a or b in the function, you actually refer to i1 and i2. So, whatever changes you make to a or b, they will be reflected on i1 and i2.


Function Overloading

In C, as in most other programming languages, every function must have a unique name. At times, it can be annoying. Imagine you want to have a function that returns the abs value of an integer.

         int abs(int i);

If you need to figure out the abs value of every possible available data type, you then have to write a function for each of the possible types:

         long labs(long l);
         double dabs(double d);

All those functions do the same thing -- return the abs value of the argument. Thus it seems silly to have a different name for each of those functions. C++ solves this by allowing you to create those functions with the same name. This is called overloading. For example, you can do the above in C++:

         int abs(int i);
         long abs(long l);
         double abs(double d);

And depending on the type of parameter you pass into the 'abs' func. C++ will select the right one. Note: What if the type of the parameter passed in is not identical to any of the available parameter types in the existing functions?

               abs('a');
               abs(3.1415F);

C++ will try to make the easiest conversion to match those parameter types in the funcion prototypes.

               abs('a');      // call int abs(int i)
               abs(3.1415F);  // call double abs(double d);

If no such conversion exists, then an error will occur.

There is a huge article (as large as to be referred as a book :) ) in here, which covers Incompatibilities and many differences Between ISO C and ISO C++, but I only covered some important ones.

For the last word, there are many features in C++ which are not exist in C, but many people already prefers C for the reasons like: the syntax is much easier to write in C, harder to compile C++ code (not harder for user but harder for computer :) ), and for whom who cares about a single byte to save from Memory or a single cycle to save from CPU, C is more efficient :) PS: I am lovin' it ;)

Links & References

Comments

No comments posted yet.



© Copyright. All rights reserved. Designed by Bilal Hatipoğlu. RSS Feed  Valid W3C XHTML 1.0 Document  Valid W3C CSS Document