This pointer 

This pointer is used as a pointer to the class object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions. It is a common knowledge that C++ keeps only one copy of each member function and the data members are allocated memory for all of their instances. Various kind of instances of data is maintained by the use of this pointer.

The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare the this pointer or make assignments to it.

A static member function does not have a this pointer.

The type of the this pointer for a member function of a class type X, is X* const. If the member function is declared with the const qualifier, the type of this pointer for that member function for class X, is const X* const.

A const this pointer can by used only with const member functions. Data members of the class will be constant within that function. The function is still able to change the value, but requires a const_cast to do so:

Example 

#include <iostream>
using namespace std;

struct X
{
private:
int a;
public:
void Set_a(int a)
{

    // The 'this' pointer is used to retrieve 'xobj.a'
    // hidden by the automatic variable 'a'
this->a = a;
  }
void Print_a() { cout<< "a = " << a <<endl;
 }
};

int main()
 {
  X xobj;
int a = 5;
xobj.Set_a(a);
xobj.Print_a();
return 0;
}

Static members

A class can contain static members, either data or functions.
Static data members of a class are also known as “class variables”, because there is only one unique value for all the objects of that same class. Their content is not different from one object of this class to another.
For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example:

Example

#include <iostream>
usingnamespacestd;

classCDummy {
public:
staticint n;
CDummy () { n++; };
~CDummy () { n--; };
};

intCDummy::n=0;

int main () {
CDummy a;
CDummyb[5];
CDummy * c = newCDummy;
cout<<a.n<<endl;
delete c;
cout<<CDummy::n <<endl;
return 0;
}

Destructors

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

A destructor is a member function with the same name as its class prefixed by a ~ (tilde).

Example

class X {
public:
// Constructor for class X
X();
// Destructor for class X
~X();
};

A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared constvolatileconst volatile orstatic. A destructor can be declared virtual or pure virtual.

If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.

The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor’s class type. Suppose a class A has an implicitly declared destructor. The following is equivalent to the function the compiler would implicitly define for A:

A::~A(){ }

Acknowledgement:

Most of the contents above have been taken directly from here solely for the purpose of education.

Exercises

Task 1:

Define a class that represents an Employee (id, name, daily_salary etc) and has a function to take his daily salary and calculate his monthly earnings. Then create 5 objects of Employee class and prepare a monthly income chart which will display all the employees and their earnings. After showing the employees and their monthly salary, release the memory you reserved for objects of your class.
(Use this pointer and destructor)

Task 2:

Create a class called Invoice which should include four pieces of information as data members — a part number (char array), a part description (char array), a quantity of the item being purchased (type int) and a price per item (type int). Provide a member function named getInvoiceAmount() that calculates the invoice amount (i.e. multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity or price is not positive, it should be set to 0. The class Invoice should have a private data member named total_invoices. All objects of class share this data member. Create 4 objects of the class Invoice in main function. Every time an object is created the total_invoices is incremented every time and shown on the screen. (Hint: use static variable).

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.