Assertion this pointer Assertion in C Function checks

  • Slides: 9
Download presentation
Assertion, this pointer

Assertion, this pointer

Assertion in C++ Function checks if its argument value If assert failure then a

Assertion in C++ Function checks if its argument value If assert failure then a message is written to the standard error device and terminating the program execution. Include the header file assert. h 2

Assertion in C++ void main() { int a=10, *p=NULL; assert(p!=NULL); cout<<“value of a=“<<*p; }

Assertion in C++ void main() { int a=10, *p=NULL; assert(p!=NULL); cout<<“value of a=“<<*p; } Here program terminate because assertion failed (p=NULL) 3

Assertion in C++ void main() { int a=10, *p=&a; assert(p!=NULL); cout<<“value of a=“<<*p; }

Assertion in C++ void main() { int a=10, *p=&a; assert(p!=NULL); cout<<“value of a=“<<*p; } Here program will print 10 (assertion is true, p not equal to NULL) 4

Assertion in C++ void main() { int a=10; assert(a+2==12); cout<<“correct”; } Here program will

Assertion in C++ void main() { int a=10; assert(a+2==12); cout<<“correct”; } Here program will print correct (assertion is true, because 10+2=12 is true) 5

this pointer 6

this pointer 6

this pointer The this keyword is used to represent an object that invokes the

this pointer The this keyword is used to represent an object that invokes the member function. It points to the object for which this function was called. It is automatically passed to a member function when it is called. e. g. When call the function A. func(), this will be set to the pointer this to the address of the object A. 7

this pointer class A { int a, b; public: void set(int x) {a=b=x; }

this pointer class A { int a, b; public: void set(int x) {a=b=x; } A getdata() {return(*this); } void show() {cout<<"a="<<a<<“ b=“<<b; } }; void main() { A obj, obj 1; obj. set(10); obj 1=obj. getdata(); obj 1. show(); } 8

this pointer 9

this pointer 9