Day. 9
Encapsulation
• C:
• Does not support encapsulation or data hiding.
• C++:
• Supports encapsulation using classes, where data and methods can be restricted using access specifiers like private, protected, and public.
6. Compatibility:
• C:
• Programs written in C can be compiled in a C++ compiler with minor modifications.
• C++:
• While it’s backward-compatible with C to a great extent, some C++ features are not available in C.
Example:
C Code:
#include <stdio.h>
void display() {
printf("Hello, C!\n");
}
int main() {
display();
return 0;
}
C++ Code:
#include <iostream>
using namespace std;
class Greeter {
public:
void display() {
cout << "Hello, C++!" << endl;
}
};
int main() {
Greeter g;
g.display();
return 0;
}
Use Cases:
• C: System programming, embedded systems, and performance-critical applications.
• C++: Game development, GUI applications, and large-scale systems requiring object-oriented design.
تعليقات
إرسال تعليق