C++ के Classes और Objects
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Objects बनाना
एक object एक class का एक concrete instance है, और आप एक normal variable की तरह इसे stack पर बना सकते हैं, या new operator इस्तेमाल करके heap पर dynamically।
हर object को class के member variables की अपनी copy मिलती है, हर दूसरे instance से independent।
उदाहरण: Creating Objects
#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;
}
Login to try C/C++/Java/PHP code in the editor
Class Methods
Methods को छोटे functions पर convenience के लिए सीधे class body के अंदर define किया जा सकता है, या class के अंदर declared और scope resolution operator (::) इस्तेमाल करके बाहर defined ताकि लंबे implementations class declaration से बाहर और पढ़ना आसान रहें।
उदाहरण: Class Methods
#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;
}
Login to try C/C++/Java/PHP code in the editor
Member Initialization
आप class definition के अंदर member variables को सीधे default values assign कर सकते हैं, जो guarantee करता है कि हर newly created object predictable, valid state में शुरू हो भले ही constructor explicitly हर field set न करे।
उदाहरण: Member Initialization
#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;
}
Login to try C/C++/Java/PHP code in the editor
Objects के Arrays
आप built-in types के arrays की तरह ही objects के arrays बना सकते हैं, जो आपको एक साथ कई object instances manage करने देता है — उदाहरण के लिए हर एक को एक अलग named variable declare करने की बजाय एक loop में Vehicle objects के एक fleet पर iterate करना।
उदाहरण: Arrays of Objects
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक class के closing brace के बाद semicolon भूल जाना, जो confusing compile errors cause करता है।
- class के बाहर से एक private member access करना, जो एक compile error है।
- एक uninitialized object pointer पर एक method call करना, जो undefined behavior है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: