this Pointer in C++

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

Program 1

#include<iostream>
using namespace std;
class Test
{
    private:
    int n;    // instance variable
    public:
    
    Test(int n) // local variable
    {
        this->n=n; 
    }
    void display()
    {
        cout<<this->n;
    }
};
int main()
{
    system("cls");
    Test T(500);
    T.display();
    return 0;
}

Program 2

#include<iostream>
using namespace std;
class Student
{
    private:
    int rno;
    string name;
    double per;
  public:  
   Student(int rno,string name,double per)
   {
       this->rno=rno;
       this->name=name;
       this->per=per;
   }
   void display()
   {
       cout<<"RNo: "<<this->rno<<endl;
       cout<<"Name: "<<this->name<<endl;
       cout<<"Per: "<<this->per<<endl;
   }

};
int main()
{
    system("cls");
     Student S(101,"Vivek",90.55);
     S.display();

    return 0;
}

 

Your opinion matters
Please write your valuable feedback about DataFlair 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 *