0% found this document useful (0 votes)
5 views4 pages

Day 11 Functions in Class

The document explains the concept of functions in a class, detailing their syntax and providing an example of a class with a member function. It also introduces the scope resolution operator (::) and demonstrates its use in accessing variables and functions in different scopes, including a namespace. The provided code examples illustrate how to define and call member functions and how to use the scope resolution operator to access global and namespace variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Day 11 Functions in Class

The document explains the concept of functions in a class, detailing their syntax and providing an example of a class with a member function. It also introduces the scope resolution operator (::) and demonstrates its use in accessing variables and functions in different scopes, including a namespace. The provided code examples illustrate how to define and call member functions and how to use the scope resolution operator to access global and namespace variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Day 11 : Functions in class

FUNCTION IN CLASS:
A function in a class refers to a member function or method that is defined within
a class. These functions are associated with an object of the class and can access
and modify the class's data members and perform specific operations related to the
class.

Syntax:

class ClassName {

public:

return_type functionName(parameters) {

// Function body

// Statements and operations

};

Explanation:
ClassName: The name of the class to which the function belongs.
return_type: The data type of the value returned by the function (use void if the
function does not return any value).
functionName: The name of the function.
parameters: The input parameters that the function may take (if any).
Function body: The set of statements that make up the function's functionality.

PROGRAM:

#include <iostream>

class MyClass {

Day 11 : Functions in class 1


public:
void sayHello() {
std::cout << "Hello from MyClass!" << std::endl;
}
};

int main() {
MyClass obj;
obj.sayHello();
return 0;
}

OUTPUT:
Hello from MyClass!

EXPLANATION:
In this example, we define a class MyClass with a public member function sayHello().
The sayHello() function is a member of MyClass and is declared within the class
definition.
The sayHello() function simply prints the message "Hello from MyClass!" to the
console using std::cout.

In the main() function, we create an object of the class MyClass called obj:
We call the sayHello() function on the obj object using the dot operator (.) to access the
member functions of the class. This results in printing the message "Hello from
MyClass!" to the console.

SCOPE RESOLUTION OPERATOR(::):


The scope resolution operator is used to access variables, functions, or classes
that are defined in different scopes.

Syntax:

namespace_name::variable_name;

Day 11 : Functions in class 2


namespace_name::function_name();

class_name::static_member;

PROGRAM:

#include <iostream>

// Define a namespace called MyNamespace


namespace MyNamespace {
int x = 5; // Define an integer variable x inside MyNamespac

// Define a function display() inside MyNamespace


void display() {
std::cout << "Value of x in MyNamespace: " << x << std:
}
}

int x = 10; // Define a global integer variable x

int main() {
int x = 15; // Define a local integer variable x within main

// Print the value of local variable x


std::cout << "Value of x in main: " << x << std::endl;

// Print the value of global variable x using the scope reso


std::cout << "Value of global x: " << ::x << std::endl;

// Call the display() function defined in MyNamespace to pri


MyNamespace::display();

OUTPUT:

Value of x in main: 15
Value of global x: 10

Day 11 : Functions in class 3


Value of x in MyNamespace: 5

EXPLANATION:
In this example, we have a namespace called MyNamespace, and it contains a
variable x and a function display(). We also have a global variable x outside of the
namespace, and a local variable x inside the main() function.
1. int x = 5; - This x variable is defined inside the MyNamespace namespace.

2. void display() - This function is defined inside the MyNamespace namespace and
displays the value of the variable x inside the namespace.

3. int x = 10; - This x variable is defined in the global scope, outside of any function
or namespace.

4. In the main() function:

5. int x = 15; - This x variable is defined inside the main() function, and it shadows
the global variable x.

6. std::cout << "Value of x in main: " << x << std::endl; - This line prints the value of
the local variable x, which is 15.
7. std::cout << "Value of global x: " << ::x << std::endl; - The scope resolution operator
:: is used here to access the global variable x outside the main() function, and it
prints the value of the global variable x, which is 10.

8. MyNamespace::display(); - The scope resolution operator is used here to call the


display() function defined inside the MyNamespace namespace. It displays the
value of the variable x defined inside the namespace, which is 5.

Day 11 : Functions in class 4

You might also like