← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 1 of 17

C++ और C के बीच अंतर

C++, C से grow हुई लेकिन objects, references, और overloading जैसी नई चीज़ें add कीं, एक car के नए model की तरह। यह lesson इन्हें compare करता है।

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. C++ में objects के लिए malloc और free इस्तेमाल करना, जब new और delete constructors और destructors call करते हैं।
  2. यह मान लेना कि C और C++ code हमेशा same तरीके से compile होगा, जैसे void* conversions के साथ।
  3. printf इस्तेमाल करना जब cout ज़्यादा type-safe है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.