Skip to main content

C++ Functions


Declaration & Assignments
C++ Functions
As we all know functions play an important role in programming. Driving programs into functions is one of the major principles of programming. In C, they are called functions but in C++ they are termed as member functions. In this tutorial you will learn about the member function and its concept used in C++.

What are Member Functions in C++?

Member functions are C++ functions that have their declarations inside the class definition and these member functions work on the data member of the class. Member function definition can be written inside or outside the definition of the class. If the definition of member function is inside the class definition, then it can e defined directly, but if it is defined outside the class then a special operator name scope resolution operator (::) is used along with the name of the class and the function name.
Example:
class Sq {
public:
int a;
int square(); // Declaring function square with no argument and having return type 'int'.
};

int Sq::square()
{
return a * a;
}
In this case, if you define the member function outside the class definition, then you must declare the function inside class definition and then define it outside using the scope resolution operator.
or
Example:
class Sq {
public:
int a;
int square()
{
return a * a; //returns square of a number
}
};
In the above program, the function is defined inside the class and so you do not need to declare it first  and can directly define the function.

main() Function of C++

The main() function is called when the program starts after initialization of the non-local objects with static storage duration. It is the primary entry point of any C++ program that is executed in hosted environment. In C++, the main() function is written after the class definition. The main function of C++ has several special properties. These are:
  1. main() function cannot be used anywhere within the program
    1. In particular cannot be called recursively
    2. Its address cannot be takes for reuse
  2. main() function cannot be predefined and cannot be overloaded.
  3. main() function cannot be declared as static, inline or constexpr
  4. The return type of the main() function cannot be deduced (i.e. auto main() {… is not allowed in C++).
The main() function for both the function definition (discussed above) will be same. You have to create objects of class inside the main() and using that object along with the dot (.) operator member functions will be called.
Syntax:
return_type main()
{
class_name
object - declaration;
object_name
.memberfunction1_name();
object_name
.memberfunction2_name();
........
}
Example:
int main()
{
Sq S1;
S1
.a = 6;
S1
.square();
cout
<< " Square of the number is :"<< S1.square();
}

Types of Member Functions in C++

As we are now familiar with what are member function and how they are declared and defined, how they are used in a C++ program to handle data member and member functions, and how they are called from the main(); it is time to know what are the different types of member function provided by C++.
Listed below are the special types of member functions that can be used within the class.
  • Simple member function
  • Static Member function
  • Const function
  • Inline function
  • Friend function

Simple Member Functions

As discussed earlier, these are simple functions of C++ with or without return type and with or without parameters. The basic structure of a simple member function is:
Syntax:
return_type functionName(parameter_list)
{
// function body;
}

Static Member Functions

The keyword ‘static‘ is used with such member functions. Static is mainly used to hold its positions. This functions work for the whole class rather than for a particular object of the class.
Example:
class X {
public:
static void k(){};
};
int main()
{
G
::k(); // calling member function directly with class name
}
The static member functions cannot access ordinary data members and member functions, but can only access the static data members and static member functions of a class.

Const Member Function

Const keyword makes variable constant, which means once defined, their value cannot be changed. The basic syntax of const member function is:
Example:
void fun() const{}

Inline Function

When a function is declared as inline, the compiler places a copy of the code of that specific function at each point where the function is called at compile time.

Friend Function


Functions are declared as friend using the keyword ‘friend’ to give private access to non-class functions. Programmers can declare a global function as friend, or a member function of other class as friend.

Comments

For Programs Click Here

Popular posts from this blog

Python Lists

Python Lists Dealing with data in a structured format is quiet generous, and this is possible if those data are set accordingly in a specific manner. So, Python provides these data structures named ‘lists’ and ‘tuples’ that are used to organize data in  single set. Python has 6 built-in sequences and among them the most famous are “lists and tuples”. The lists are containers that hold a number of other objects in a given order. It usually puts into practice the sequence protocol and allows programmers to add or remove objects from that sequence. Each element of the sequence is assigned a number i.e. he index and the first index is 0 (zero). This versatile data-type of Python is written in a sequence of list separated by commas between expressions. Creating Lists To build a list, just put a number of expressions in square brackets. The syntax is: Syntax: lst1 = [ ] # lst1 is the name of the list lst2 = [ expression1 , …. , expression_N ] Example: #!/usr/bin/python ls...

C++ Qualifiers and Storage Classes

C++ Qualifiers and Storage Classes Qualifiers and storage class are smaller but important programming concept that helps to make the quality of a variable more accurate for using that variable within the program. In this chapter you will learn about how qualifiers are used with variables and what the roles of different storage classes in C++ are. What is a Qualifier? A qualifier is a token added to a variable which adds an extra  quality , such as specifying volatility or constant-ness to a variable. They act like an adjective for a variable. With or without a qualifier, the variable itself still occupies the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers just specify something about how it may be accessed or where it is stored. Three qualifiers are there in C++. These are: const : This is used to define that the type is constant. volatile : his is used to define that the type is volatile. mutable:  applies to non...

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...