Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem understanding a functionality of constructor.

problem understanding a functionality of constructor.

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    i was going through a article about friend function and then in i came across this code:

    #include
    using namespace std;

    class Distance
    {
    private:
    int meter;
    public:
    Distance(): meter(0) { }
    //friend function
    friend int addFive(Distance);
    };

    // friend function definition
    int addFive(Distance d)
    {
    //accessing private data from non-member function
    d.meter += 5;
    return d.meter;
    }

    int main()
    {
    Distance D;
    cout<<"Distance: "<< addFive(D);
    return 0;
    }

    in the above code what is

    Quote:

    Distance(): meter(0) { }

    and how is a object of class distance is able to access a private member ?

    Quote:

    int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }

    Thank you.

    J L C 3 Replies Last reply
    0
    • T Tarun Jha

      i was going through a article about friend function and then in i came across this code:

      #include
      using namespace std;

      class Distance
      {
      private:
      int meter;
      public:
      Distance(): meter(0) { }
      //friend function
      friend int addFive(Distance);
      };

      // friend function definition
      int addFive(Distance d)
      {
      //accessing private data from non-member function
      d.meter += 5;
      return d.meter;
      }

      int main()
      {
      Distance D;
      cout<<"Distance: "<< addFive(D);
      return 0;
      }

      in the above code what is

      Quote:

      Distance(): meter(0) { }

      and how is a object of class distance is able to access a private member ?

      Quote:

      int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }

      Thank you.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The first uses the expression list of the constructor to initialise a member by passing to the member's constructor. See Constructors and member initializer lists - cppreference.com[^] for the various possible methods about member initialisation besides assigning in the compound statement (the final block enclosed by braces). The access to private and protected members is granted by the friend declaration - cppreference.com[^].

      1 Reply Last reply
      0
      • T Tarun Jha

        i was going through a article about friend function and then in i came across this code:

        #include
        using namespace std;

        class Distance
        {
        private:
        int meter;
        public:
        Distance(): meter(0) { }
        //friend function
        friend int addFive(Distance);
        };

        // friend function definition
        int addFive(Distance d)
        {
        //accessing private data from non-member function
        d.meter += 5;
        return d.meter;
        }

        int main()
        {
        Distance D;
        cout<<"Distance: "<< addFive(D);
        return 0;
        }

        in the above code what is

        Quote:

        Distance(): meter(0) { }

        and how is a object of class distance is able to access a private member ?

        Quote:

        int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }

        Thank you.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You should get hold of a good C++ book. Probably the best would be the one written by Bjarne Stroustrup[^]

        1 Reply Last reply
        0
        • T Tarun Jha

          i was going through a article about friend function and then in i came across this code:

          #include
          using namespace std;

          class Distance
          {
          private:
          int meter;
          public:
          Distance(): meter(0) { }
          //friend function
          friend int addFive(Distance);
          };

          // friend function definition
          int addFive(Distance d)
          {
          //accessing private data from non-member function
          d.meter += 5;
          return d.meter;
          }

          int main()
          {
          Distance D;
          cout<<"Distance: "<< addFive(D);
          return 0;
          }

          in the above code what is

          Quote:

          Distance(): meter(0) { }

          and how is a object of class distance is able to access a private member ?

          Quote:

          int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }

          Thank you.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Welcome to C++. :-D I mean:

          • constructors are the very basics of an OOP language (you must be sure to grasp them before even attempting to code).
          • accessing an otherwise unaccessible member is the very purpose of the friend declaration.
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups