Skip to main content

Static variables....

STATIC VARIABLES
Static variables are very crucial in cpp language and also for building logics for a company. Today, we will see a code first and then we will discuss the static variables.
#include<iostream>
#include<string>
using namespace std;
class people
{
       string address;//privately called//
       string name;//privately called//
public:
       static int id;// prototype of declaring a static variable//
       people(string name, string address)//Parameterized constructor which sets the //value for name and address//
       {
             ++id;// increment of that static variable as a new person comes to join..//
             this->name = name;//set the value for name//
             this->address = address;// set the value for address//
       }
       void show()//this will show the full identity(name, address, id) of that person//
       {
             cout << "ID : " << id << endl;
             cout << "Name : " << name << endl;
             cout << "Address : " << address << endl;
       }
};
int people::id = 0;(initialize the static member)

int main()
{
       people p("Fahim", "Bangladesh");(first person)
       p.show();//(shows identity of first person) //
       people p1("Preety", "Bangladesh");//(second person)
       p1.show();//(shows identity of second person)//
       people p2("Umar Faruque Imran", "Bangladesh");//(third person)//    
p2.show()//(shows identity of third person)//
people p3("Chisty", "Bangladesh");//(forth person)//
p3.show();//(shows identity of forth person)//

       return 0;
}
Actually, the code is counting the number of people joining a company. The company store the people by his or her name, country and id. Id is such kind of variable which increases one when a new one enters that company. Like: when one new person comes to join then his id will be 1. Then if other person come to join. Then his or her id will be 2. Then 3….4..5.
Mechanism:-  When we declare a static member, it means that member has the accessibility of the whole program. It can be increased from any class and from anywhere. But one thing we have to consider. That is, we have to declare the static variable publicly.  When an object is created of that class, then the constructor is called. As the constructor is called, the static variable has a pre-increment. Then when we call void show(). This print the  full identity of that person…

Happy Coding……😊😊
  

Comments

Popular posts from this blog

Remote Dictionary Server

  Today I want to discuss about REDIS Cache. In a simple way, it’s like making web apps more effective. What they say ? From Redis  official website , “Redis is an open-source, in-memory data structure store, used as a database, cache and message broker. It supports data structures . I t can be thought as a NOSql DB which store data as a key value pair in the system memory. Now, let’s talk about caching. Difference Between MongoDB And Redis MongoDB is a disk based database optimized for operational simplicity. It has a huge data volumes. And Redis is an in-memory, persistent  data Structure store that is popular for performing operations with maximum effectiveness. Caching Caching is a process where the data are stored in caches so that those can be retrieved faster. It is for making user experience better. Redis Cache As Redis is an in-memory data structure store  where data access operations are performed faster. That’s why it’s the perfect method for caching. The ...

Some Important learning sites for Dynamic programming

Now, We will discuss some of the most useful sites for learning Dynamic Programming. Here, I have mentioned some of those . :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: medium.freecodecamp.org -  Demystifying Dynamic Programming iarcs.org.in -  Dynamic Programming - Tiling topcoder.com -  Dynamic Programming – From Novice to Advanced illinois.edu -  Dynamic Programming  ;(Exercises are recommended) codechef.com -  Dynamic Programming geeksforgeeks.org -  Dynamic Programming  ;(Contains a lot of practice sessions) MIT OCW (Contains some Advanced topics as well) Dynamic Programming I Dynamic Programming II Dynamic Programming III Dynamic Programming IV Happy Learning...Thanks 😉😉😊😊  

Params in C#

C# HAS A FEATURE. IT CAN SPECIFY A METHOD PARAMETER WHICH WILL TAKE A VARIABLE NUMBER OF  ARGUMENTS. WE USED TO DO THOSE THINGS BY USING POINTER IN C++ C# MAKES OUR LIFE EASIER. ALL WE NEED IS A KEYWORD. AND THAT IS "PARAMS" LET'S HAVE THE FUN REMEMBER, THE PARAMS PARAMETER MUST BE A SINGLE ARRAY.   public class MyClass         {                          public static void amarmethod(params int[] amararrayerkichuelements)             {                                 for (int i = 0; i < amararrayerkichuelements.Length; i++)                 {                     Console.Write(amararrayerkichuelements[i] + " ");                 } ...