Static Keyword in C++ Part – 1

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// No of Object 
// static
#include<iostream>
using namespace std;
class Test
{

  public:
      static int count;
  Test()
  {
     count++;
  }
  void display()
  {
     cout<<count;
  }
};
int Test::count=0;
int main()
{
    system("cls");
    Test T1,T2,T3,T4,T5,T6,T7,T8,T9,T10;
    T1.display();

    return 0;
}

Program 2

#include<iostream>
using namespace std;
class Student
{
      private:
      int rno;
      string name;
      string course;
     public:
     static string clgname;
     Student(int rno,string name,string course)
     {
           this->rno=rno;
           this->name=name;
           this->course=course;
     }
     void display();
};
string Student::clgname="DataFlair";
void Student::display()
{
      cout<<"RNo: "<<this->rno<<endl;
      cout<<"Name: "<<this->name<<endl;
      cout<<"Course: "<<this->course<<endl;
      cout<<"College: "<<Student::clgname<<endl;
}
int main()
{
    system("cls");
    Student S1(101,"Vikas","BBA");
    Student S2(102,"Aakash","BTech");
    Student S3(103,"Niliesh","MTech");
    S1.display();
    cout<<"\n---------------------------------\n";
    S2.display();
    cout<<"\n---------------------------------\n";
    S3.display();
    return 0;

}

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *