Differences Between C & C++
Differences Between C & C++
Review of C
Differences between
C and C++
1
Text editor
Project
C source .c file
{name}.code
Cross-compiler
Cc11 Startup
Assembly code
Assembly source {name}start.s
source .s11 .s11
Cross-assembler Cross-assembler Cross-assembler
As11 As11 As11
Review of C (1)
Program structure
Include header files #include <filename.h>
a) constants, macros, type definitions,
structure/union definitions, etc.
b) declarations of functions and external variables
Local constants, macros, type definitions
Definitions of functions
Main function
int main()
{
…...
}
2
Review of C (2)
Functions
return_type function_name (argument declaration)
{
declarations and statements
}
1. Called by name with parameter list, e.g.,
A = HighestNumber(B, C);
2. Any function can be invoked or called by any other
3. Cannot be defined inside another function
4. If no return_type is specified, int is assumed
5. If the function is not declared before it is invoked, it may return
the wrong type
6. Auto variables come into existence when the function is called,
and disappear when the function is left
Review of C (3)
Function parameters
Parameters are passed by value
3
Review of C (4)
Declarations and definitions
Definitions set aside storage and possibly values
in the executable code
You can define only once
Declarations only specify types associated with storage
or functions
You can declare as often as necessary
int Square(int x)
{
int y; int z;
y=x*x;
return y;
}
Review of C (5)
Scope of variables
Int Numb1; global variables
4
Review of C (6)
Common errors (1)
if (A==B) {}
not if (A=B) {}
int a, b;
scanf (%d, %d”, &a, &b);
not scanf(“%d, %d”, a, b);
Review of C (7)
Common errors (2)
int Name1;
int Name1(void)
{…...
}
int Name2;
Name2 = Name1();
not Name2 = Name1;
5
Review of C (8)
Common errors (3)
int ArrayOne[5][7];
int RowIndex, ColumnIndex;
char *StringOne;
char StringTwo[20];
C programming conventions
6
Differences between
C and C++
C++ C
At the top of a function block
Anywhere in the program
or a block created by a pair of
As close to their point of use as
braces { }
possible
Before any executable statement
Example: Example:
double mean(double num[], int size) double mean(double num[], int size)
{ {
double total; int i;
double total;
for(int i=0; i<size; i++)
total += num[i]; for(i=0; i<size; i++)
return total/size; total += num[i];
} return total/size;
}
7
Differences between C and C++ (2)
Input/Output
C++ C
Allows I/O operators <<, >> Allows only standard I/O functions
scanf, printf
Example: Example:
main() main()
{ {
double x, y=3.14; double x, y=3.14;
C++ C
Allows new and delete operators Allows only standard functions
malloc(), calloc(), and free()
Example: Example:
#include <stdlib.h>
8
Differences between C and C++ (4)
Passing parameters to the function
C++ C
Allows reference type of Allows only passing parameters
parameters by value
Example: Example:
C++ C
Classes
Derived classes
Friend functions
Function overloading
Operators overloading
9
Elements of structured programming
10