1 / 12

C++ Reference Variables:

C++ Reference Variables:. References must be initialized. int x; int& foo = x; // foo is now a reference to x so this sets x to 56 foo = 56; cout << x <<endl;.

serge
Télécharger la présentation

C++ Reference Variables:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Reference Variables:

  2. References must be initialized. int x; int& foo = x; // foo is now a reference to x so this sets x to 56 foo = 56; cout << x <<endl;

  3. References save copying of objects when passed as arguments to functions. So we get efficiency gain with references.  This is possible with pointers also.

  4. References vs Pointers It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references. Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers. References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid. Note that for this reason, containers of references are not allowed. References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created.

  5. References are better than pointers a) Safer b) Easier to use

  6. When do we pass arguments by reference or pointer?

  7. An example of reference #include<iostream>using namespace std;void swap (int& first, int& second){        int temp = first;        first = second;        second = temp;}int main(){  int a = 2;  int b = 3;  swap( a, b );  cout<<a<<" "<<b;  getchar();  return 0;}

  8. Functions should not return reference to local variables // Invalid codeint& getLocalVariable(){        int x;        return x;} What if we make x static?

  9. Scope Resolution Operator #include<iostream>using namespace std; int x = 10;void fun(){    int x = 2;   {        int x = 1;        cout << ::x << endl;     }}int main(){  fun();  getchar();  return 0;} Output: 10 Similarly scope resolution operator can be used to access global functions

  10. A Simple C++ Class Example

  11. #include<iostream> using namespace std; class Point { private: int x; int y; Public: // Constructor Point(int i, int j) { x = i; y = j; } // Methods int getX() { return x; } int getY() { return y; } };

  12. // A function that takes two point objects as parameter and returns a new // point object Point addPoints(Point &p1, Point &p2) { Point t(p1.getX()+p2.getX(), p1.getY()+p2.getY()); return t; } int main() { Point obj1(12, 13); Point obj2(8, 10); Point obj3 = addPoints(obj1, obj2); cout<<"x coordinate of obj3 = "<<obj3.getX()<<"\n"; cout<<"y coordinate of obj3= "<<obj3.getY(); return 0; } Output: x coordinate of obj3 = 20 y coordinate of obj3= 23

More Related