C++ और C के बीच अंतर
In this page:
Procedural बनाम Object-Oriented
C एक procedural language है जो functions के इर्द-गिर्द built है जो इनके बीच explicitly pass की गई data structures पर operate करते हैं।
C++ object-oriented programming से C की syntax extend करता है, आपको data और इस पर operate करने वाले functions को एक single class के अंदर साथ bundle करने देते हुए।
उदाहरण: Procedural vs. Object-Oriented
// Include std::cout and std::cin
#include <iostream>
// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
void drive() { std::cout << "Driving" << std::endl; }
};
// Program execution starts in main()
int main() {
Car car;
car.drive();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Standard Input Output
C stdio.h से printf() और scanf() जैसे library functions के जरिए input और output perform करता है, जो format specifiers (%d, %s) पर depend करते हैं जिन्हें compiler type-check नहीं कर सकता।
C++ का iostream cout और cin introduce करता है, जो सही type automatically infer करने और compile time पर type mismatches catch करने के लिए operator overloading इस्तेमाल करते हैं।
उदाहरण: Standard Input Output
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print to the console with cout
std::cout << age << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Memory Management
C heap memory को malloc() और free() library functions से manage करता है, जो raw, untyped memory के साथ काम करते हैं और कभी constructors या destructors call नहीं करते।
C++ के new और delete operators language-level constructs हैं जो memory allocate करते हैं और automatically relevant object का constructor या destructor invoke करते हैं।
उदाहरण: Dynamic Memory Management
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
Point() { std::cout << "Constructed" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Point *p = new Point();
// Free the heap memory with delete
delete p;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Overloading
C में, एक दिए गए scope में हर function का नाम unique होना चाहिए, क्योंकि compiler calls purely नाम से resolve करता है।
C++ function overloading allow करता है -- कई functions जो एक नाम share करते हैं लेकिन parameter types या count में अलग हैं -- compiler को आपके pass किए arguments के आधार पर सही वाला choose करने देते हुए।
उदाहरण: Function Overloading
// Include std::cout and std::cin
#include <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }
// Program execution starts in main()
int main() {
show(5);
show(5.5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reference Variables
C++ reference variables introduce करता है, जो pointer syntax की ज़रूरत के बिना किसी दूसरे variable के permanent alias की तरह act करते हैं।
यह आपको बड़े objects functions को pass करने और इन्हें in place modify करने देता है, एक deep copy के overhead और C की required explicit pointer dereferencing की syntax दोनों से बचते हुए।
उदाहरण: Reference Variables
// Include std::cout and std::cin
#include <iostream>
void increment(int &x) {
x += 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
increment(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- C++ में objects के लिए
mallocऔरfreeइस्तेमाल करना, जबnewऔरdeleteconstructors और destructors call करते हैं। - यह मान लेना कि C और C++ code हमेशा same तरीके से compile होगा, जैसे
void*conversions के साथ। printfइस्तेमाल करना जबcoutज़्यादा type-safe है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced