Thread: About coding

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    About coding

    Hi Guys,

    I wanted to know that if we have a class of student as:


    Code:
    class student
    
    {
    
    private:
    
    int roll_no;
    char name;
    int age;
    
    public:
    int getroll_no();
    char getname();
    
    };
    main()
    
    {
    
    char student :: getname()
    int   student :: getroll_no()
    
    }
    -------------------------------------------------------------------
    If we use the codes in bold is it possible that we can access the private data members of the class using the functions in public?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    71
    Yes
    SetName(<value>) //Set the value
    GetName() //return that value, wich was previously set
    those functions work to acces private data

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Actually, no. Well, not the way you've tried to do it, anyways.

    You're going to have to be clearer on a few things.

    Firstly, you have defined student's name member as a single char. There aren't many students, regardless of nationality, who's name is a single character. Either make the name an array of char or (better) make the name of type std::string (which represents a set of characters internally). std::string is a type in the standard header named <string>.

    Second, it is not possible to just declare member functions in main() and use them - compilers will object. They need to be defined somewhere else. More importantly, non-static member functions can only be used on an instance of the class. They cannot be called standalone. So
    Code:
    //  your definition of the student class
    
    int student::getroll_no() {return roll_no;}    // implement the getroll_no() member function
    
    int main()
    {
        student person;
        int number = person.getroll_no();    // this will work:  person is an instance of student
    
        student::getroll_no();    // this will not compile as it is not operating on an instance of student
    
        person.student::getroll_no();      // this will also work
    }
    Mind you, given that your code provides no means to set the name and roll_no methods of a student (for example, a user-defined constructor) there is no point in having the member functions to get their values.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,
    Thanks for your reply, so in the function:
    student::getroll_no();
    You said that it doesnot belong to the instance of the student,what do you mean by instance?
    But I have defined this function getroll_no() in the class of student.

    int number = person.getroll_no(); ---> Does this function gets parameters and stores them in the "int number"?

    Last edited by student111; 06-26-2012 at 09:15 AM. Reason: correcting

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by student111
    so in the function:
    student::getroll_no();
    You said that it doesnot belong to the instance of the student
    grumpy stated that "this will not compile as it is not operating on an instance of student".

    Quote Originally Posted by student111
    what do you mean by instance?
    An instance of the class is an object of the class.

    Quote Originally Posted by student111
    But I have defined this function getroll_no() in the class of student.
    Yes. However, it is a non-static member function, so it must operate on an object of the student class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,
    Ok it was my mistake ,so the object should be declared before we could use the function.
    Eg
    Code:
    main()
    {
     student std1;
     std1.getname(); 
    
    }
    where "std1" is the object for the class student.

    So what do these functions do :
    char student :: getname()
    int student :: getroll_no()


  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by student111 View Post
    H
    So what do these functions do :
    char student :: getname()
    int student :: getroll_no()
    In your original code snippet, they would guarantee a compiler error. You cannot use that syntax within the body of a function.

    In the code snippet I gave (if you replace the first line with your class definition) the code will compile. That syntax is used to define those member functions outside the body of the student class.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    student111, I recommend you look at a tutorial about classes.
    Furthermore, you should post working code so that we can understand if you are merely using a legacy compiler or just posting pseudo code (and find syntax errors).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me coding
    By hakimstm1b in forum C Programming
    Replies: 4
    Last Post: 10-16-2010, 07:28 AM
  2. IDE for vlc coding
    By binnyshah in forum Windows Programming
    Replies: 2
    Last Post: 07-06-2010, 02:56 AM
  3. Coding a log in.....
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2008, 07:55 PM
  4. C Coding
    By hamsteroid in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-25-2007, 08:52 AM
  5. help with coding...
    By ahming in forum C Programming
    Replies: 6
    Last Post: 04-26-2004, 08:08 AM