Static Member Function and Variables in C++

In a C++ programming we can create static function and variable by using the “static” keyword.

Static Variables

 In C++ programming we can create a static variable by placing the “static” keyword before the variable declaration.

Once a variables declared as static, only one copy of that variable is created for the whole class.

static variable
static variable

 When a variable is declared as static, by default it is initialize to zero.

Output

Note: In the above figure we have a two object (object 1 & object 2) we object occupies a separate memory for variable(for ex: for variable 1 , object1 and object2 both will occupy the memory) .

But when a variable declared as a static then only one memory location is occupy for all object.

Static function

In C++ programming we can create a static function by placing the “static” keyword before the function declaration .

A static function can only assess static member variable and member function. 

In a C++ it is also possible to call a  static member function without object.

Static public member function:

Example: WAP to declare public member function and call them from main().

Static function can’t assess normal variable

A static function can only assess static  variable .

A static function can’t access normal function.

If we try to access normal function within the static function then we will get an error.

Output

Static Object

In a C++ programming like a static variable &  function we can also declare a static object.

The object is a composition of one or more member variable of the class.

When we declare a static object by default it initializes all variables of class with zero.

Output

Categories C++

Size of the Object in C++

In a C++ programming language,  size of any object is equal to the sum of size of all the data members of the class.

In a below program,  class contain three data type and three variable. 

Data type int occupies 4 bytes, float occupies 4 bytes, and char occupies 1 byte. their sum is  9 bytes .

In this program size of individual of object is 9 byte. We can know the size of object/class by using  sizeof() operator.

Output

Categories C++

Inline Function in C++

In a C++ language  An inline function is similar to macros. Inline function is also called as open subroutine because their code is replaced at the place of function call in the caller function. 

In a C++programming  normal  function  are known as closed  subroutine because when such function are called ,the control passed to the function.       

inline function  reduces  the overhead of accessing the member function, increases the efficiency of program and allows quick execution of function.

Example of inline function

Output

Note:
1 Inline function used when the member function contains few statement.
2  If function takes more time to execute , then it must be declared inline.                     .

   

Categories C++

Member functions in C++

In a C++,  we can use the function within the classes. These functions are called as a member function. It can be public ,private & protected.

Public member function: in a below program we have created a public function which can be accessed from outside of class.

Output

Private member function:  in a below program we have created a private function which can’t be accessed from outside of class.

Output

Description: when we compile this program we will get an error message. Because show() function cant be access from main() because it is a private member function.

How to access private member function

 We can access private member function from public member function. In the below program we have created one public function display() and one private function show() and we can call the show() from within the display().

Output

Define a member function outside of the class

In a C++ language we can define a function inside  as well as outside of the class. To define a function a function outside of the class syntax is:

Syntax:

Datatype class_name :: function name

Output

In the above program we have declare two function inside class and define outside of the class.

a private member function outside of the class

In a below program we have define a private function display() outside of the class which can’t be accessed from outside of class. If we try to access then we will get an error message.

Output

Solution of above program: We can access private member function from public member function. In the below program we have created one public function show() and one private function display() and we can call the display() from within the show().

OUTPUT

Access Private Data Member/Variable

In a C++ we can access private data member within the class. It can’t be used outside of the class.

Output

Note: we can’t access private data member outside of the class. The private data can be accessed by only member function and friend  function of that class. 

Solution of above program: The private data can be accessed by only member function and friend  function of that class. 

Output

Example. Write a C++ program to create a class and member function & find the factorial of the number given by user.

Output

Categories C++

Access Specifiers in C++

In a C++ language there are three types of access specifiers . They are: public private and protected.

Public: A public data can be accessed by out side of the code in which it is defined .

Private : A private data can not be accessed by outside of the code in which it is defined.

The private data can be accessed by only member function and friend function of that class.

Protected : A protected access specifier is similar to the private only difference is that it has a access to their derived classes.

Default : By default access specifier is private means when we have not specified access specifier it will be private.

Syntax for declaring a class:

Example

In the above example we have created a new class “student”.

Inside the student class we have created two private data member age & name[20] and two public member function input() & show().

Example :  write a program to add two number by using class and public data member.

Output:

Note: In the above program we have created a class “example” which hold public integer data x,y and inside the main function we create a object of class example name obj.

By using the object and (.) dot operator you can access public data member of the calss.

Note: we can create more than one object of the class. For example:

More than one object of the class:

in a C++ we can create one or more than one object of the class.

In the below example we have created two object of class “example”.

Output

Categories C++

Class & Object in C++

Class:  A class is a user defined data type which contains the data members and member function.

A class forms the basis for object oriented programming.

 It describes the shape and nature of the object .

Any concept that we want to implement in oop’s must be encapsulated in class.

The internal data of the class are called data members and functions are called member functions.

The variable of the class are called object or instance of the class.

A class is a template for an object and an object is an instance for a class.

Syntax for declaring a class:

Example

In the above example we have created a new class “student”.

Inside the student class we have created two private data member age & name[20] and two public member function input() & show().

Object: we can access data members and member function outside of the class by using the object.

Object is a run time entity. 

Syntax for object creation:

Class_name   object_name;

Example:    student   obj;    

Note:    Here student is a name of the class and “obj” is a name of the object.

Note: we can create more than one object of the class. Foe example:

Note: By using the object and (.) dot operator  you can access public data member of the calss.

Categories C++

Pointers in C++

In  a  C++ programming language, variable is used to hole the value & this variable stored in a memory. 

Each variable has a address to identify where these variables actually stored in a memory.  

 In a C++  programming language, pointer is a variable which can hold the address of another variable.  

 Accessing Address of Variable

In a system every memory location has its address & we can access the address using ampersand (&) operator.

This ampersand (&) operator  denotes an address in memory. 

Ex:   int  i = 20;
      char c = ‘a’;

In the above example, there are two variable i & c.

Integer variable “i” stores the value 10 at memory location 1024(for example) and character variable “c” stores the value ‘a’ at memory location 1026.

cout<<“\n value of  variable i=  “<<  i ;
cout<<“\n value of  variable c=  “<< c ;

Output

value of  variable i= 20
value of  variable c= a

If we want to print the address of variable ‘i and c’ then we need ampersand (&) operator  .

   cout<<“\n Address of  variable i =  “<< &i ;
   cout<<“\n Address of  variable c= “<< &c ;

Output:           Address of  variable i =   1024
                        Address of  variable c=   1026

Note: When you run this program, address are printed might be different.Example: Write a program to print the address of the variable.

#include using namespace std;

Output

Note: Each and every time when you run this program these variables stored in a different memory location.

Pointer Variable

pointer  variable is hold the address of another variable.

Output

Value at address

In the above example, there are two variable, first is normal variable ‘i’ & pointer variable ‘ptr’.

Integer variable “i” stores the value 10 at memory location 1024(for example) and pointer variable “ptr” stores the address of variable  ‘i’ at memory location 3045.

Example:

#include

Output:

value of  variable i=  10

Note: Pointer variable “ptr” prints the address of variable and “*ptr” prints the value of i. *variable name (*ptr) is also called value at address.

Example:

Output

Note: When you run this program, address are printed might be different.

Pointer to Pointer

In a C language, a pointer is an address. Normally, a pointer variable contains the address of a another variable. 

A pointer to a pointer is a chain of pointers. 

Or

Pointer to pointer means one pointer variable holds the address of another pointer variable.

Example:

Output

NULL Pointers

A pointer that is assigned NULL is called a null pointer. We must initialize NULL pointer during variable declaration.

A null pointer refers to pointer variable that does not point to a valid address.

Generally NULL pointer is used when you do not have an exact address to be assigned.

Output

Categories C++

Recursion in C++

In a C programming language, a function that calls itself is known as a recursive function.

And, this technique is called as recursion. 

Recursive functions are very useful to solve many problems such as  Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

How recursion works?

Example: Write a program in a C++ language to find the factorial using recursion.

Output

Example: Write a program in a C language to print the Fibonacci series using recursion.

Fibonacci series are 0, 1, 1, 2, 3, 5, 8, …,. 

In fibonacci series, except for the first two terms of the series, every other term is the sum of the previous two terms, for example, 5 = 2 + 3.

Output

Categories C++

Unions in C++

In a C++ programming language, union is a special user defined data type that allows to store different data types in the same memory location.

“union”  uses the same memory location for multiple-purpose.

 Or

In a union data type, single variable, i.e., same memory location, can be used to store multiple types of data.

In a C++ programming union is similar to the structure but major difference is that it requires less memory than structure.

Apart from this difference, you can define a union with many variables, but only one member can contain  single value at any given time.

Defining a Union

We can define union with “union” keyword. “union” defines a new user defined data type with one or more than one member.

Syntax of union definition:

  In the union’s definition at the end & before the final semicolon, you can specify one or more union variables but it is optional.

Example:

Above define a union type named book having three members i, f, and c.

In the above example variables of Book is book1 and book2. These variables can store an integer, a floating-point number, or a string of characters.

During the union definition, memory occupied by a union variable will be large enough to hold the largest member of the union.

In the above example, user defined data type Book will occupy 30 bytes of memory space because this is the maximum space which can be occupied by a character string. 

Example: Write a program to store and print the following information such as name, roll number, class and section of the student using union.

Output

In the above example, we can see that the values of name, rollno, and Class of union got corrupted because the final value assigned to the variable has occupied the memory location and this is the reason that the value of Section is getting printed very well.

Note: In union, one variable can contain a single value at any given time which is the main purpose of having unions −

Correct Implementation of Above program

Output

Difference between structure and union

Output

Categories C++

Structures in C++

There are various types of data type such as int, char float etc.

For example:


int  i;      here variable i can store only data of integer type
char c;   here variable c can store only data of character type
float  f; here variable f can store only data of floting-point type

Variable of each data type can store only single type of data.

But,  if we want to store different  type of data in a single variable then we use structure variable.

“structure” is user defined data type that allows to store data items of different data type.

  Defining a Structure

We can create a structure, by using the struct keyword.

The struct keyword defines a user defined new data type that can hold more than one member of different data type.

Format of structure is as follows –

In a C programming Structures are used to represent a record.

Suppose you want to store the information of  a school student such as : −

  • Name
  • Class
  • Section
  • Roll Number

Example: 

Here s1 and s2 are the structure variable.

Another way of creating a structure variable is:

Example: Write a program to store and print the following information such as name, roll number, class and section of the student using structure.

Output

Example: Write a program to store and print the following information such as name, roll number, class and section of 2 students using structure.

Output

Categories C++