C is a procedure oriented language. It follows the Top down approach (i.e. analyzing the problem and dealing with it) whereas C++ is an object-oriented programming language and follows the bottom up approach (i.e. breaking the problem into parts, dealing with those parts and hence solving the problem).
The main focus while programming in C is on functions whereas in C++, it is on classes and objects. Before going deeper into classes, we will make some programs in C++ which are equivalent to those in C.
There are some syntactical differences between C and C++ that one must know to construct a basic C++ program:
1. Input Output library:
- C:
stdio.h
- C++ :
iostream.h
2. Input Output statements/streams:
- C:
scanf(" ")
and
printf(" ")
- C++:
cin>>
and
cout<<
(respectively)
3. No need of mentioning the type of variable while giving input or printing output, it is detected automatically.
- C:
int integer; scanf(“%d”,&integer); printf("%d”,integer);
- C++:
int integer cin>>integer; cout<<integer;
4. The access specifiers(discussed in detail later):
- C:
Only public(not mentioned explixitly)
- C++:
private by default, public/protected (by mentioning explicitly)
5. It is necessary to provide a return type of a function unlike C.
- C:
test();
is a valid prototype
- C++
test();
is INVALID whereas
void type();
is VALID