this pointer

closed account (1vf9z8AR)
i expect the output to be the person's name and his pay.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include<iostream>
#include<string.h>     //for strcpy()
using namespace std;
class sales
{
    char name[80];
    int pay;
public:
    void getdata(char *n,int p)
    {
        strcpy(name,n);
        pay=p;
    }
    void outdata()
    {
        cout.write(this->name,80);
        cout<<pay;
    }
};
int main()
{
    sales s1,s2;
    s1.getdata("raman",25000);
    s2.getdata("rita",5000);
}
Last edited on
sales::outdata() is never called.
closed account (1vf9z8AR)
Alien language+english language is coming.
But it is written 'this' pointer is like a atm card which gives identity and thus information which matches that identity is given.I am doing same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<string.h>
using namespace std;
class sales
{
    char name[80];
    int pay;
public:
    void getdata(char *n,int p)
    {
        strcpy(name,n);
        pay=p;
    }
    void outdata()
    {
        cout.write(this->name,80);
        cout<<pay;
    }
};
int main()
{
    sales s1,s2;
    s1.getdata("raman",25000);
    s2.getdata("rita",5000);
    s1.outdata();
    s2.outdata();
}

The program is badly formed; it would generate compile-time errors.
http://coliru.stacked-crooked.com/a/ff9eedd4ec02f78c

Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstring>

class sales
{
    std::string name ;
    int amount;

    public:

        sales( const char* n, int amt ) : name(n), amount(amt)
        { /* if( amount < 0 ) throw ... */ }

        void print() const { std::cout << name << ' ' << amount << '\n' ; }

        // all these mean the same:
        // void print() const { std::cout << sales::name << ' ' << this->amount << '\n' ; }
        // void print() const { std::cout << this->sales::name << ' ' << amount << '\n' ; }
};

int main()
{
    const sales s1( "raman", 25000 ) ;
    sales s2( "rita", 5000 ) ;

    s1.print();
    s2.print();
}
closed account (1vf9z8AR)
but i have to use this pointer.

The question is: Write a program to show the working of this pointer.
> Write a program to show the working of this pointer.

Any program which implements a non-trivial non-static member function of a class would demonstrate the working of the this pointer.


> Write a program which uses the keyword this in a non-trivial manner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

struct Person
{
    void who_is_this() const
    {
        std::cout << "this is an object of type Person at address "
                  << this << " with name '" << name << "'\n" ;
    }

    void rechristen( const std::string& name ) { this->name = name ; }

    Person* address() { return this ; }
    const Person* address() const { return this ; }

    Person& reference() { return *this ; }
    const Person& reference() const { return *this ; }

    std::string name ;
};

int main()
{
    Person author { "Samuel Langhorne Clemens" } ;
    std::cout << "std::addressof(author) == " << std::addressof(author) << '\n' ;
    std::cout << "author.address() returned " << author.address() << '\n' ;
    author.who_is_this() ;

    const Person* ptr = author.address() ;
    ptr->who_is_this() ;

    author.rechristen( "Mark Twain" ) ;
    ptr->who_is_this() ;

    const Person& alias = ptr->reference() ;
    alias.who_is_this() ;
}
.
Topic archived. No new replies allowed.