Thread: Accessing to data members of a class issues!

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    Accessing to data members of a class issues!

    Code:
    #include <iostream>
    using namespace std;
    class ObPass {
    private:
       char name[40];  
       int n;
       char ch;
    public:
       void disp(ObPass objectpass){
          cout << "Enter Name: " << endl;  
          cin >> name;
          cout << "Enter Integer: " << endl;  
          cin >> n; 
          cout<<"Enter Character: "<<endl;
          cin >> ch;  
          cout << objectpass.name << objectpass.n  << objectpass.ch << endl;
       }
    };
    int main() {
       ObPass object;
       object.disp(object); 
       return 0;
    }
    In cin statements, I am not putting in like objectpass.name, objectpass.n, objectpass.ch
    So there is noting displaying.
    If I put in properly in cin like objectpass.name, objectpass.n, objectpass.ch then it display correctly.

    Now if I don't put in like objectpass.name, objectpass.n, objectpass.ch in cin rather just name, n, ch and same don't put in objectpass.name, objectpass.n, objectpass.ch in cout too. Then, display is correct.

    My question is if I don't put in like objectpass.name, objectpass.n, objectpass.ch in cin, rather only put in name, n, ch, will the data store some other place?
    In short, cin >>objectpass.name and cin>>name are two different variables?
    How?
    Last edited by rm82co; 06-05-2019 at 10:01 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,495
    So why don't you just do this?
    cout << name << n << ch << endl;

    objectpass is a waste of space.
    It is a COPY of the object in main, so it isn't updated when you read into the actual member variables.
    And if you did modify say objectpass.name, that change is lost when the function returns.

    Try it like this.
    Code:
    #include <iostream>
    using namespace std;
    class ObPass {
    private:
       char name[40];  
       int n;
       char ch;
    public:
       void disp(){
          cout << "Enter Name: " << endl;  
          cin >> name;
          cout << "Enter Integer: " << endl;  
          cin >> n; 
          cout<<"Enter Character: "<<endl;
          cin >> ch;  
          cout << name << n  << ch << endl;
       }
    };
    int main() {
       ObPass object;
       object.disp(); 
       return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    209
    @Salem's point is spot on, but I thought I'd focus on:

    In short, cin >>objectpass.name and cin>>name are two different variables?
    Yes.

    objectpass is the parameter passed into the function, and it has it's OWN name, n and ch. The function is a member function, it is running on it's OWN instance of name, n, ch, and so the two are different instances (two separate instantiations of the same class).

    Even this:

    Code:
    ObPass a;
    
    a.disp( a ); // as odd as this is
    Would STILL be two instances inside disp, because the function passes by value - which means a is copied to call disp, and thus a second, separate instance.

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by Niccolo View Post
    @Salem's point is spot on, but I thought I'd focus on:



    Yes.

    objectpass is the parameter passed into the function, and it has it's OWN name, n and ch. The function is a member function, it is running on it's OWN instance of name, n, ch, and so the two are different instances (two separate instantiations of the same class).

    Even this:

    Code:
    ObPass a;
    
    a.disp( a ); // as odd as this is
    Would STILL be two instances inside disp, because the function passes by value - which means a is copied to call disp, and thus a second, separate instance.
    You're best teacher. I could get the point
    Thanks you both replied

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-20-2013, 09:25 PM
  2. Accessing protected class members
    By Korhedron in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2010, 08:12 AM
  3. Accessing private Data members
    By Emeighty in forum C++ Programming
    Replies: 17
    Last Post: 08-14-2008, 11:16 PM
  4. accessing Class members
    By fizisyen in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2004, 06:18 PM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM

Tags for this Thread