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

C++ के Class Methods

एक class method (member function) एक class के अंदर declared एक function है जो उस class के objects पर operate करता है, या तो class body के अंदर inline defined या scope resolution operator इस्तेमाल करके अलग से।
Syntax
cpp
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?

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

एक Method Inline Define करना

एक method की body सीधे class definition के अंदर लिखी जा सकती है, जिसे inline definition कहते हैं, जो छोटे methods के लिए convenient है लेकिन class declaration को लंबा रखता है।

उदाहरण: Defining a Method Inline

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

Class के बाहर एक Method Define करना

एक method class के अंदर declared लेकिन बाद में इसके बाहर defined हो सकता है, class नाम के बाद :: (scope resolution operator) इस्तेमाल करके यह indicate करते हुए कि definition किस class से belong करती है।

उदाहरण: Defining a Method Outside the Class

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

एक Value Return करने वाले Methods

एक method एक regular function की तरह एक value return कर सकता है, calling object की अपनी member variables से compute की गई, object को अपनी state के बारे में information provide करने देते हुए।

उदाहरण: Methods that Return a Value

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

Parameters लेने वाले Methods

एक method object की member variables तक access होने के अलावा अपने खुद के parameters accept कर सकता है, इसे external input को object की internal state के साथ combine करने देते हुए।

उदाहरण: Methods that Take Parameters

cpp
// 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;
}
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. ClassName:: के बिना class के बाहर एक method define करना।
  2. बाहर define करने से पहले class के अंदर एक method declare करना भूल जाना।
  3. data न बदलने वाले methods पर const भूल जाना, ताकि उन्हें const objects पर call न किया जा सके।

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.