Interview Question C Vs C++ – What is difference between C and C++ language? For this question, we need answer 3 major differences with explanations.
Answer:
- C is the procedural language whereas C++ is Object Oriented. Also, we can say, C is function driven program whereas C++ is object driven.
- C lacks of features like extendibility (Inheritance) and simplicity (polymorphism) etc.
- In C data is not secured where as in C++ is secure.
Detail of above difference between C and C++
Point -1: C is procedural language/function driven. C++ is Object Oriented.
In C, starting from main(), step by step execution happens and all the functions are called in top to bottom approach. We create a list of functions with different header files if we want to keep group of functions in a separate files and include them in main program for executions. That why we called c as a function driven program.
void robot(){
printf("a=%d",a);
}
void human(){
printf("a=%d",a);
}
int main()
{
func1();
func2();
return 0;
}
C++ is Object oriented Program. In C++, functions are called using class objects. And hence called object oriented program.
class Robot{
public:
void display(){
cout<< "Robot";
}
};
class Human:Robot{
public:
void display(){
cout<< "Human";
}
};
int main()
{
//create class object
Robot r;
//call function using object
r.display();
Robot r;
r.display();
return 0;
}
Point -2: C lacks of features like Inheritance and polymorphism etc.
C does not support classes and objects. So, Inheritance or extendibility or polymorphism is not possible in C but in C++.
Inheritance:
//Inheritance
//Car is a base class and Maruti is a derived class
// that inherit public/protected members and methods of base class Car
class Car{
};
//Extend the car class
class Maruti:Car{
};
Polymorphism:
/Run time polymorphism – same function name in both base
//and derived class.
//Car is a base class contains virtual function run()
//and Maruti is a derived class that override the same
//function
class Car{
public:
virtual void run(){
cout<<" base class car";
}
};
class Maruti:Car{
public:
virtual void run(){
cout<<" derived class car";
}
};
Point-3: In C, data is not secured where as in C++ it is secure.
Data is not secured in C. For example, In below C program, variable int a can be accessed by any function e.g. func1 & func2. We cannot block access of variable a in a specific function.
int a = 5;
void robot(){
printf("a=%d",a);
}
void human(){
printf("a=%d",a);
}
int main()
{
func1();
func2();
return 0;
}
Data can be protected in C++. For example, using access specifier private, protected or public etc. or provide the access of private data using public function etc.
In below C++ program example, if we don’t want Robot class to share variable a with derived class Human, we can control it with private specifier and if we want to give access to derived class Human only not outside of class for example in main() we can make variable a protected. If we want to give access to all we can make it public etc.
class Robot{
private:
int a;
public:
Robot(){
this->a = 5;
}
void display(){
cout<< a;
}
};
class Human:Robot{
public:
//private variable a of base class cannot
//be accessbile. If want to access it, make it protected
//in base class.
void display(){
cout<< a;
}
};
int main()
{
Robot r;
r.display();
Human h;
h.display();
return 0;
}
More Points on C Vs C++:
Above points are major differences between C and C++ language that we should answer in an interview. There could be more major points.
However if you want to go for feature and syntax c and c++ differences, you could dive deeper for . for example –
- Cin, cout is available in c++ not in C
- No virtual function are available in c but in C++
- Operator overloading is not possible in c
- New and delete operators are available in C++ not in C
- Exception handling is supported in C++ only
- C does not support namespace.
Etc.