C++ के Class Methods
In this page:
class ClassName {
public:
return_type method_name(parameters) {
// body
}
};
ObjectName.method_name(arguments);
Class Method क्या है?
एक class method, जिसे member function भी कहते हैं, एक class के हिस्से की तरह declared एक function है, और उस class का हर object इसे call कर सकता है, method को उस specific object के data तक automatic access देते हुए।
उदाहरण: What is a Class Method?
// Include std::cout and std::cin
#include <iostream>
// Define the type Greeter
class Greeter {
// Members below can be accessed from outside the class
public:
void greet() { std::cout << "Hello!" << std::endl; }
};
// Program execution starts in main()
int main() {
Greeter g;
g.greet();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Method Inline Define करना
एक method की body सीधे class definition के अंदर लिखी जा सकती है, जिसे inline definition कहते हैं, जो छोटे methods के लिए convenient है लेकिन class declaration को लंबा रखता है।
उदाहरण: Defining a Method Inline
#include <iostream>
class Box {
public:
int getSize() { return 10; } // defined inline, right inside the class
};
int main() {
Box b;
std::cout << b.getSize() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Class के बाहर एक Method Define करना
एक method class के अंदर declared लेकिन बाद में इसके बाहर defined हो सकता है, class नाम के बाद :: (scope resolution operator) इस्तेमाल करके यह indicate करते हुए कि definition किस class से belong करती है।
उदाहरण: Defining a Method Outside the Class
// Include std::cout and std::cin
#include <iostream>
// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
int getSize();
};
int Box::getSize() {
// Send 20 back to the caller
return 20;
}
// Program execution starts in main()
int main() {
Box b;
// Print to the console with cout
std::cout << b.getSize() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Value Return करने वाले Methods
एक method एक regular function की तरह एक value return कर सकता है, calling object की अपनी member variables से compute की गई, object को अपनी state के बारे में information provide करने देते हुए।
उदाहरण: Methods that Return a Value
// Include std::cout and std::cin
#include <iostream>
// Define the type Circle
class Circle {
// Declare radius and set it to 2.0
double radius = 2.0;
// Members below can be accessed from outside the class
public:
double area() { return 3.14159 * radius * radius; }
};
// Program execution starts in main()
int main() {
Circle c;
// Print to the console with cout
std::cout << c.area() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameters लेने वाले Methods
एक method object की member variables तक access होने के अलावा अपने खुद के parameters accept कर सकता है, इसे external input को object की internal state के साथ combine करने देते हुए।
उदाहरण: Methods that Take Parameters
// Include std::cout and std::cin
#include <iostream>
// Define the type Calculator
class Calculator {
// Members below can be accessed from outside the class
public:
int add(int a, int b) { return a + b; }
};
// Program execution starts in main()
int main() {
Calculator calc;
// Print to the console with cout
std::cout << calc.add(3, 4) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
ClassName::के बिना class के बाहर एक method define करना।- बाहर define करने से पहले class के अंदर एक method declare करना भूल जाना।
- data न बदलने वाले methods पर
constभूल जाना, ताकि उन्हें const objects पर call न किया जा सके।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: