Site icon DataFlair

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;
}

 

Exit mobile version