The major difference between C and C++ is that C is a procedural programming language and does not support classes and objects, while C++ is a combination of both procedural and object oriented programming language; therefore C++ can be called a hybrid language.
C++ is Multi-Paradigm ( not pure OOP, supports both procedural and object oriented) while C follows procedural style programming.
In C data security is less, but in C++ you can use modifiers for your class members to make it inaccessible from outside.
C follows top-down approach ( solution is created in step by step manner, like each step is processed into details as we proceed ) but C++ follows a bottom-up approach ( where base elements are established first and are linked to make complex solutions ).
C++ supports function overloading while C does not support it.
C++ allows use of functions in structures, but C does not permit that.
C++ supports reference variables ( two variables can point to same memory location ). C does not support this.
C does not have a built in exception handling framework, though we can emulate it with other mechanism. C++ directly supports exception handling, which makes life of developer easy.
C is a procedural programming language and does not support classes and objects.
C++ is a combination of both procedural and object-oriented programming language. C++ can be called a hybrid language.
C++ is much more than c with classes. There are many other concepts inside of C++ like templates, function & operator overloading, exceptions & many others already mentioned here. This makes C++ very powerful & flexible, but also hard to learn. It's not that the single concepts are difficult to understand, but the sum of them & how they are playing together. Take a look on boost to see what everything is possible to do with C++. And I guess it took ages to understand what happens under the hood, which is very clear in the case of C.
C++ is Multi-Paradigm ( not pure OOP, supports both procedural and object oriented) while C follows procedural style programming.
In C data security is less, but in C++ you can use modifiers for your class members to make it inaccessible from outside.
C follows top-down approach ( solution is created in step by step manner, like each step is processed into details as we proceed ) but C++ follows a bottom-up approach ( where base elements are established first and are linked to make complex solutions ).
C++ supports function overloading while C does not support it.
C++ allows use of functions in structures, but C does not permit that.
C++ supports reference variables ( two variables can point to same memory location ). C does not support this.
C does not have a built in exception handling framework, though we can emulate it with other mechanism. C++ directly supports exception handling, which makes life of developer easy.
for more info visit this site
In C++, struct and class are exactly the same things, except for that struct defaults to public visibility and class defaults to private visibility.
Some important differences between the C and C++ structures:
- Member functions inside structure: Structures in C cannot have member functions inside structure but Structures in C++ can have member functions along with data members.
- Direct Initialization: We cannot directly initialize structure data members in C but we can do it in C++.
- C
- C++
// C program to demonstrate that direct
// member initialization is not possible in C
#include <stdio.h>
struct Record {
int x = 7;
};
// Driver Program
int main()
{
struct Record s;
printf("%d", s.x);
return 0;
}
/* Output : Compiler Error
6:8: Error: Expected ':', ', ', ';', '}' or
'__attribute__' before '=' token
int x = 7;
^
In function 'main': */
Output:
7 - Using struct keyword: In C, we need to use struct to declare a struct variable. In C++, struct is not necessary. For example, let there be a structure for Record. In C, we must use “struct Record” for Record variables. In C++, we need not use struct and using ‘Record‘ only would work.
- Static Members: C structures cannot have static members but is allowed in C++.
- C
- C++
// C program with structure static member
struct Record {
static int x;
};
// Driver program
int main()
{
return 0;
}
/* 6:5: Error: Expected specifier-qualifier-list
before 'static'
static int x;
^*/
This will generate an error in C but no error in C++. - Constructor creation in structure: Structures in C cannot have constructor inside structure but Structures in C++ can have Constructor creation.
- C
- C++
// C program to demonstrate that Constructor is not allowed
#include <stdio.h>
struct Student {
int roll;
Student(int x)
{
roll = x;
}
};
// Driver Program
int main()
{
struct Student s(2);
printf("%d", s.x);
return 0;
}
/* Output : Compiler Error
[Error] expected specifier-qualifier-list
before 'Student'
[Error] expected declaration specifiers or
'...' before numeric constant
[Error] 's' undeclared (first use
5555555555in this function)
In function 'main': */
Output:2 - sizeof operator: This operator will generate 0 for an empty structure in C whereas 1 for an empty structure in C++.// C program to illustrate empty structure
#include <stdio.h>
// empty structure
struct Record {
};
// Driver program
int main()
{
struct Record s;
printf("%dn", sizeof(s));
return 0;
}Output in C:
0Output in C++:
1 - Data Hiding: C structures do not allow concept of Data hiding but is permitted in C++ as C++ is an object oriented language whereas C is not.
- Access Modifiers: C structures do not have access modifiers as these modifiers are not supported by the language. C++ structures can have this concept as it is inbuilt in the language.