OBJECT ORIENTED PROGRAMMING CIS 3100

If inline functions are recursive example program to

Info icon This preview shows pages 17–21. Sign up to view the full content.

View Full Document Right Arrow Icon
If inline functions are recursive. -Example program to illustrate inline functions : #include <iostream.h> inline float mul(float x, float y) { return(x*y); } inline double div(double p, double q) { return(p/q) ; } int main( ) { float a=1.2 ; float b=2.3; cout<< mul(a,b)<<”\n”; cout<< div(a,b)<<”\n”; return 0; } 3. Explain Function Overloading? A single function name can be used to perform different types of tasks. The same function name can be used to handle different number and different types of arguments. This is known as function overloading or function polymorphism. #include<iostream.h> #include<conio.h> void swap(int &x,int &y) { int t; t=x; x=y; y=t; } void swap(float &p,float &q) { float t; t=p; p=q;
Image of page 17

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
q=t; } void swap(char &c1,char &c2) { char t; t=c1; c1=c2; c2=t; } void main() { int i,j; float a,b; char s1,s2; clrscr(); cout<<"\n Enter two integers : \n"; cout<<" i = "; cin>>i; cout<<"\n j = "; cin>>j; swap(i,j); cout<<"\n Enter two float numbers : \n"; cout<<" a = "; cin>>a; cout<<"\n b = "; cin>>b; swap(a,b); cout<<"\n Enter two Characters : \n"; cout<<" s1 = "; cin>>s1; cout<<"\n s2 = "; cin>>s2; swap(s1,s2); cout<<"\n After Swapping \n"; cout<<" \n Integers i = "<<i<<"\t j = "<<j; cout<<" \n Float Numbers a= "<<a<<"\t b = "<<b; cout<<" \n Characters s1 = "<<s1<<"\t s2 = "<<s2; getch(); } 4. Explain about Operator Overloading? (Nov/Dec 2011) -General form of operator function is: Return type classname :: operator (op-arglist) { Function body } Overloaded operator functions can be invoked by expression x op y for binary operators
Image of page 18
In the following program overloaded function is invoked when the expression c1+c2 is encountered. This expression is the same as operator op(x,y) (ie) operator +(c1,c2) using friend function #include<iostream.h> #include<conio.h> class complex { private: float real; float img; public: complex() { real=0.0; img=0.0; } complex(float a,float b) { real=a; img=b; } friend complex operator +(complex,complex); void display() { cout<<"\n"<<real<<"+-i"<<img<<"\n"; } }; complex operator +(complex c1,complex c2) { complex t; t.real=c1.real+c2.real; t.img=c1.img+c2.img; return(t); } //}; void main() { clrscr(); complex c1(5.5,2.5); complex c2(1.5,5.5); complex c3; c3=c1+c2; c1.display(); c2.display(); c3.display(); getch(); }
Image of page 19

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
5. Explain the various types of operators in C++ 1. :: Scope resolution operator: A variable declared in an inner block cannot be accessed outside the block. To resolve this problem the scope resolution operator is used. It can be used to uncover a hidden variable. This operator allows access to the global version of the variable. It takes the form, :: variable-name 2. ::* Pointer to member declarator : To declare a pointer to the member of the class 3. ->* Pointer to member operator : To access a member using object name and a pointer to that member .* Pointer to member operator : To access a member using a pointer to the object and a pointer to that member delete Memory release operator : When a data object is no longer needed it is destroyed to release the memory space for reuse. new Memory allocation operator : The new operator can be used to create objects of any data-type. It allocates sufficient memory to hold a data object of type data-type and returns the address of the object.
Image of page 20
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern