← Back to C++ Course | Chapter 8: OOP Core | Lesson 2 of 14

C++ के Classes और Objects

एक class एक blueprint है, एक cookie cutter की तरह, और एक object इससे बना एक cookie है। हर object के अपने details हैं लेकिन same design follow करता है।
Syntax
cpp
class ClassName {
public:
  data_type member_variable;
  return_type method_name() { }
};

ClassName object_name;
object_name.member_variable = value;

एक Class Define करना

एक class एक user-defined blueprint है जो member variables (data store करने के लिए) और member functions (operations perform करने के लिए) को एक single custom type नाम के तहत group करता है।

एक बार defined, एक class नए variables declare करते समय किसी भी built-in type जितनी ही usable बन जाती है।

उदाहरण: Defining a Class

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Define the type Person
class Person {
// Members below can be accessed from outside the class
public:
	std::string name;
	void greet() { std::cout << "Hi, I'm " << name << std::endl; }
};

// Program execution starts in main()
int main() {
	Person p;
	p.name = "Alex";
	p.greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Objects बनाना

एक object एक class का एक concrete instance है, और आप एक normal variable की तरह इसे stack पर बना सकते हैं, या new operator इस्तेमाल करके heap पर dynamically।

हर object को class के member variables की अपनी copy मिलती है, हर दूसरे instance से independent।

उदाहरण: Creating Objects

cpp
#include <iostream>
#include <string>

class Person {
public:
	std::string name = "Guest";
};

int main() {
	Person p1;                 // on the stack
	Person *p2 = new Person();  // on the heap
	std::cout << p1.name << " " << p2->name << std::endl;
	delete p2;
	return 0;
}

Class Methods

Methods को छोटे functions पर convenience के लिए सीधे class body के अंदर define किया जा सकता है, या class के अंदर declared और scope resolution operator (::) इस्तेमाल करके बाहर defined ताकि लंबे implementations class declaration से बाहर और पढ़ना आसान रहें।

उदाहरण: Class Methods

cpp
#include <iostream>

class Box {
public:
	int getWidth() { return 5; } // defined inline
	int getHeight();             // declared here, defined below
};

int Box::getHeight() {
	return 10;
}

int main() {
	Box b;
	std::cout << b.getWidth() << " " << b.getHeight() << std::endl;
	return 0;
}

Member Initialization

आप class definition के अंदर member variables को सीधे default values assign कर सकते हैं, जो guarantee करता है कि हर newly created object predictable, valid state में शुरू हो भले ही constructor explicitly हर field set न करे।

उदाहरण: Member Initialization

cpp
#include <iostream>

class Account {
public:
	double balance = 0.0; // default value guarantees consistent starting state
};

int main() {
	Account acc;
	std::cout << acc.balance << std::endl;
	return 0;
}

Objects के Arrays

आप built-in types के arrays की तरह ही objects के arrays बना सकते हैं, जो आपको एक साथ कई object instances manage करने देता है — उदाहरण के लिए हर एक को एक अलग named variable declare करने की बजाय एक loop में Vehicle objects के एक fleet पर iterate करना।

उदाहरण: Arrays of Objects

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:
	int x = 0;
};

// Program execution starts in main()
int main() {
	Point points[3];
	points[1].x = 5;
	// Print to the console with cout
	std::cout << points[1].x << 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. एक class के closing brace के बाद semicolon भूल जाना, जो confusing compile errors cause करता है।
  2. class के बाहर से एक private member access करना, जो एक compile error है।
  3. एक uninitialized object pointer पर एक method call करना, जो undefined behavior है।

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.