Describe ‘this’ pointer with an example.

1 Answer

Answer :

‘this’ pointer: 

C++ uses a unique keyword called „this‟ to represent an object that invokes a member function. This unique pointer is automatically passed to a member function when it is invoked. „this‟ is a pointer that always point to the object for which the member function was called. 

For example, the function call A.max ( ) will set the pointer „this‟ to the address of the object A. Then suppose we call B.max ( ), the pointer „this‟ will store address of object B. 

Example:

#include<iostream.h>

class sample

{

int a;

public:

void setdata(int x)

{

this ->a=x;

}

void putdata()

{

cout<<this ->a;

}

};

void main()

{

sample s;

s.setdata(100);

s.putdata( );

}

In the above example, this pointer is used to represent object s when setdata ( ) and putdata ( ) functions are called. 

Related questions

Description : Write a program to declare a class ‘book’ containing data members as ‘title’, ‘author-name’, ‘publication’, ‘price’. Accept and display the information for one object using pointer to that object.

Last Answer : #include<iostream.h> #include<conio.h> class book { char author_name[20]; char title[20]; char publication[20]; float price; public: void Accept(); void Display(); }; void ... title \t author_name \t publication \t price\n ; p-> Display(); getch(); }

Description : Describe memory allocation for objects.

Last Answer : The memory space for object is allocated when they are declared and not when the class is specified. The member functions are created and placed in memory space only once when they are ... are essential because the member variables will hold different data values for different objects.

Description : Explain the friend function with proper example.

Last Answer : Friend function: The private members of a class cannot be accessed from outside the class but in some situations two classes may need access of each other s private data. So a common function can be declared which can be made friend ... ; } friend void swap(A,B); }; void swap(A a,B b) { cout

Description : Explain virtual base class with suitable example.

Last Answer : Consider a situation where all three kinds of inheritance, namely, multilevel, multiple, hierarchical inheritance, are involved. This illustrated in fig a. the child has two direct base classes, parent1 & ... Grandparent { }; class Child: public Parent1,public Parent2 { };

Description : What is a class? Give its example. 

Last Answer : Class is a user defined data type that combines data and functions together. It is a collection of objects of similar type. Example: class Student { int rollno; char name[10]; public: void getdata( ); void putdata( ); };

Description : With suitable example, describe effect of ++ and - - operators used with pointer in pointer arithmetic. 

Last Answer : ++ Operator: - It is referred as increment operator that increments the value of variable. If ++ operator is used with pointer variable, then pointer variable points to next memory address that means pointer increment with respect to size ... int a[5]={10,20,30,40,50},*ptr; ptr=a[0]; for(i=0;i

Description : State and describe use of pointer operator and address operator. Give one example of each. 

Last Answer : Pointer operator:- * It is used to declare a pointer variable. Also used as "value at" operator to read value stored inside the address pointed by pointer. Example: int *ptr; Address operator:-& ... address of a variable can be stored in pointer variable. Example: int a,*ptr; ptr=&a;

Description : Describe pointer to object with an example. 

Last Answer : When address of an object of a class is stored into the pointer variable of the same class type then it is pointer to object. This pointer can be used to access the data member and member functions of same class. ... (); // Invoking getdata()using pointer to object p1. ptr->display(); }

Description : With suitable example, describe use of this pointer.

Last Answer : 1. C++ uses a unique keyword called this to represent an object that invokes a member function. 2. This unique pointer is automatically passed to a member function when it is invoked. 3. this is a pointer that always ... ; public: void setdata(int x) { this ->a=x; } void putdata() { cout

Description : Write a program that copies contents of one file into another file.

Last Answer : Assuming input file to be copied file1.txt contents are “Hello Friends…” and file where the contents need to copy is file2.txt already created  #include #include #include #include #include void main() { clrscr(); ifstream fs; ofstream ft; char ch, fname1[20], fname2[20]; cout

Description : Write a program to implement the following hierarchy using suitable member functions. Refer Figure No.2.

Last Answer : # include <iostream.h> #include<conio.h> class Student { int roll_no; char name[10]; public: void read_studentData() { cout<< Enter student s roll no and name \n ; cin> ... { result r; clrscr(); r.read_result(); r.display_result(); getch(); }

Description : Write a program to overload the ‘—’ unary operator to negate the values.

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class Number { int x,y; public: Number (int a, int b) { a =x; b =y; } void display() { cout<< ... ; -N1; cout<<"\n After negation:"; N1. display (); getch (); }

Description : Write a program to count the number of lines in file.

Last Answer : #include<iostream.h> #include<fstream.h> #include<conio.h> void main() { ifstream file; char ch; int n=0; clrscr(); file.open("abc.txt"); while(file) { file. ... \n Number of lines in a file are:"<<n; file.close(); getch(); }

Description : Write a program to sort an 1-d array in ascending order.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int arr[20]; int i, j, temp,n; clrscr(); cout<<"\n Enter the array size:"; cin>>n; cout<<"\n Enter ... 0;i<n;i++) {  cout<< \n <<arr[i]; } getch(); }

Description : What is parameterized constructor?

Last Answer : A constructor that accepts parameters is called as parameterized constructor. In some applications, it may be necessary to initialize the various data members of different objects with different values when they are ... an argument. Member function put ( ) displays the value of data member m .

Description : What are the rules for virtual function?

Last Answer : Rules for virtual function: 1. The virtual functions must be members of some class. 2. They cannot be static members. 3. They are accessed by using object pointers. 4. A virtual function can ... virtual function is defined in the base class, it need not be necessarily redefined in the derived class.

Description : What is inheritance? Give different types of inheritance.

Last Answer : Inheritance: The mechanism of deriving new class from an old/existing class is called inheritance. OR Inheritance is the process by which objects of one class acquired the properties of ... is a combination of single, multiple, multilevel and hierarchical inheritance. Diagram:

Description : State the rules for writing destructor function.

Last Answer : Rules for writing destructor function are: 1) A destructor is a special member function which should destroy the objects that have been created by constructor. 2) Name of destructor and name of the class should ... should not be classified in any types. 7) A class can have at most one destructor. 

Description : Write the applications of object oriented programming.

Last Answer : Applications of object oriented programming are: 1) Real time systems 2) Simulation and modeling 3) Object-oriented databases 4) Hypertext, hypermedia and expertext 5) AI and expert systems ... and parallel programming 7) Decision support and office automation systems 8) CIM/CAM/CAD systems

Description : Write any four benefits of OOP. 

Last Answer : Benefits of OOP: 1. We can eliminate redundant code and extend the use of existing classes. 2. We can build programs from the standard working modules that communicate with one another, ... interface descriptions with external systems much simpler. 10. Software complexity can be easily managed.

Description : Write a program to implement single inheritance from the following Refer Figure No.1

Last Answer : #include #include class employee { protected: int emp_id; char name[10]; }; class emp_info:public employee { int basic_salary; public: void getdata() { coutemp_id; coutname; coutbasic_salary; } void putdata() { cout

Description : Give syntax and use of fclose ( ) function.

Last Answer : Syntax:  int fclose(FILE* stream); Use: This function is used to close a file stream. The data that is buffered but not written is flushed to the OS and all unread buffered data is discarded.

Description : Write two properties of static member function

Last Answer : i) A static member function can have access to only other static data members and functions declared in the same class. ii) A static member function can be called using the class name with a scope resolution operator instead of object name as follows: class_name::function_name;

Description : Explain use of scope resolution operator.

Last Answer : It is used to uncover a hidden variable. Scope resolution operator allows access to the global version of a variable. The scope resolution operator is used to refer variable of class anywhere in program. ... outside of class. Return_type class_name:: function_name( ) { Function body }

Description : What is multilevel inheritance? Draw the diagram to show multilevel inheritance. using classes with data member and member function. 

Last Answer : When a class is derived from another derived class then it is called as multilevel inheritance.

Description : State the difference between OOP and POP.

Last Answer : OBJECT ORIENTED PROGRAMMING (OOP) PROCEDURE ORIENTED PROGRAMMING (POP) Focus is on data rather than procedure. Focus is on doing things (procedure). Programs are divided into multiple ... approach is used in C++ language. Procedure oriented approach is used in C language.

Description : What Is A Stack Pointer Register, Describe Briefly.?

Last Answer : Answer :The Stack pointer is a sixteen bit register used to point at the stack. In read write memory the locations at which temporary data and return addresses are stored is known as the stack. ... to avoid program crashes a program should always be written at one end and initialized at the other.

Description : Describe the functions of stack pointer and program counter of 8085. 

Last Answer : Stack pointer: 1. It is a 16 bit register which is used to store the address of topmost filled memory location of stack memory. 2. SP always points current top of stack. 3. If data is ... of memory and in 8085 the address of memory is 16 bit. Hence program counter is 16 bit register.

Description : State the syntax to declare pointer variable with example.

Last Answer : General syntax to declare pointer. datatype *var_name; Eg: int var = 20;

Description : State four arithmetic operations perform on pointer with example.

Last Answer : The pointer arithmetic is done as per the data type of the pointer. The basic operations on pointers are Increment: It is used to increment the pointer. Each time a pointer is incremented, it points to the ... address 1004, Then ptr-2 shows 1004-(2*2) = 1000 as decremented location for an int. 

Description : Explain how to pass pointer to function with example.

Last Answer : When pointer (addresses) is passed to the function as an argument instead of value then function is called as call by reference. Example: #include #include int add(int *); void main() { int *ptr,pos=0; clrscr(); printf( ... ; getch(); } int add(int *p) { int i=0; int sum=0; for(i=1;i

Description : Explain pointer to derived class with example.

Last Answer : Pointers can be used to point to the base class objects and objects of derived class. Pointers to objects of base class are compatible with pointers to objects of a derived class. Single pointer variable can be made to point ... { int a; public:  void get() { couta; }  void put() { cout

Description : Explain pointer arithmetic with example.

Last Answer : C++ allows pointers to perform the following arithmetic operations: a. A pointer can be incremented (++) or decremented (--) b. Any integer can be added or subtracted from a pointer. c. One pointer can be subtracted ... -: 56 value of ptr +2: 22 value of ptr-1: 75 value of ptr+3: 90

Description : I have internal and external stitches, 10 internal 8 external, I get them out in 13 days. How long until I can play basketball? The cut is on my hand btween my thumb and pointer finger?

Last Answer : You should not resume playing basketball until after your stitches are taken out. How long it will take before you can play again after they are taken out depends on a variety of factors. Your doctor ... more information about your wound. In general, however, you'll need to wait at least a week.

Description : Is your pointer or your ring finger longer?

Last Answer : My ring finger is quite a bit longer. I’m male.

Description : I smashed my pointer finger yesterday on a rock and it is swelling and what can I do to stop the swelling?

Last Answer : You can try some ice, but ice is best within a few hours of the hit. Don’t put the ice directly on it. You can put some ice and some water in a ziploc bag and rest it on your finger so it is cold. Can you move it without increasing the pain? Are you sure it isn’t broken?

Description : Is using a laser pointer to play with a cat cruel?

Last Answer : answer:No. but we can give the kitty an award for trying if it will make ya feel better

Description : What is usually the cause of a mouse pointer freezing up?

Last Answer : Have you tried another connection? Sometimes a USB port can go bad.

Description : Mouse pointer unaligned in Half Life 2 Deathmatch on CrossOver Games?

Last Answer : You should put in a support ticket to our tech support crew. jon parshall COO www.codeweavers.com

Description : My dog swallowed a laser pointer! Will he be okay?

Last Answer : Laser pointers do not have nuclear radiation - they do have batteries. You should contact your local emergency clinic. They will probably recommend that you induce vomiting under medical supervision ... but if your dog stops eating, developes vomiting or diarrhea, seek medical attention immediately.

Description : Cheap green laser pointer?

Last Answer : That one looks/ appears to be good quality. But I’m gonna tell you right now usually a blue, green, or onange laser costs alot because of the chemicals and other BS. So idk how long that one will last you. But for about $35 I’d say who cares. GO FOR IT!

Description : In what situations is the pointer nub on some laptops preferable to the trackpad? or to the mouse?

Last Answer : some people just like to play with and use the laptop nipple.

Description : When you start to select a range of cells you will see a thick white cross shaped pointer called a?

Last Answer : Hi

Description : Who makes and where can you get the best laser pointer?

Last Answer : Amazon.com has a wide variety of laser pointers.

Description : Following an experiment, Jamie estimates that his probability of making a three-pointer with a basketball is 0.8. If he shoots 160 attempts from the three-point line, how many times can he expect to make a three-point basket A. 32 B. 80 C. 128 D. 160?

Last Answer : 128

Description : When the pointer is positioned on a _____ it is shaped like a hand. 1) Grammar error 2) Hyperlink 3) Screen tip 4) Spelling error 5) Formatting error

Last Answer : Answer :2

Description : As shown in the illustration, which of the listed actions of the synchroscope pointer is occurring? EL-0002 A. Revolving slowly in the slow direction. B. Revolving rapidly in the slow direction. C. ... stationary at the 12 o'clock position since the leading edges of the A phases are in phase.

Last Answer : Answer: C

Description : An open occurring within the field rheostat of an AC generator can be detected by short circuiting its terminals and observing a ____________. A. negative deflection of the wattmeter pointer B. ... of the wattmeter pointer C. buildup of alternator voltage D. low, but constant alternator voltage

Last Answer : Answer: C

Description : When a megohmmeter is being used on a alternating current machine, the meter pointer will dip toward 'zero' and then gradually rise to the true resistance value if the motor insulation is _____________. A. grounded B. good C. shorted D. dirty

Last Answer : Answer: B

Description : A switchboard ammeter indicates a reading slightly above 'zero' when the leads are disconnected, this is caused by _____________. A. mechanical misalignment of the meter pointer B. a poor ground for the meter case C. static electricity in the air D. capacitors inside the meter storing charges

Last Answer : Answer: A