Question about Classes and Structure

This is a discussion on Question about Classes and Structure within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having problem with Classes, I have made a small program of Classes which is : Code: main() ...

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

    Question about Classes and Structure

    Hi,

    I am having problem with Classes, I have made a small program of Classes which is :




    Code:
    main()
    
    
    Class Student ------------------(1)
    
    {
    
    public :
    Student(): //constructor
    
    defined some functions here.
    
    private:
    
    int age;
    int grade;
    char name;
    char address;
    
    
    };
    
    
    
    
    {
    
    Student mystudent; ------------(2)
    
    /* manipulate the data members
    
    {
    mystudent.age;
    mystudent.address;
    mystudent.name;
    mystudent.grade;
    
    }
    ------------------------------------------

    I have made this small program of Class, I have also defined the data members of the data but when I compile the program, it gives me error messege.Am I geting this error because the class is defined private?
    How can I make the data members in (2) to access the Class in (1)?

    I hope my questions are clear
    Last edited by student111; 01-12-2012 at 05:19 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    The entire purpose of declaring members private (preceding them by the private keyword and a colon in the class definition, as you have done) is to make them inaccessible to arbitrary code - hence the error messages.

    The only code that is allowed to access a private member of class Student is a member function of class Student or friends. If a function is declared as a friend, then that function can access private members. If another class is declared as a friend, then all member functions of that class can access private members.
    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, Sunshine, and read this, this, and this before posting again.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Also, the keyword is class not Class. Capitalisation is important.

    There's a lot wrong with your code. Take a look at this. Lots of the bugs are fixed, and I inserted some comments about various parts of the code...
    Code:
    #include <iostream>
    
    class Student {
    public: 
      Student(){} 
      
    private: 
      int age; 
      int grade; // should this be char? 
      char name; 
      // Should probably be char name[LENGTH] where LENGTH 
      // is the length of the string you want defined, or seeing how 
      // you're using C++ you might as well use std::string name 
      //after #including <string>
      char address; // Same comments as per name
    };
    
    // Main returns int
    int main() {
      Student myStudent; 
      // Well, as it is defined this is all you can do with this :-) You need to add some functionality
      
    
      return 0; 
    }
    However, if all you want is to store age, grade, name and address you might consider using a struct instead of a class. As it is your class doesn't do anything. You'll need to define and implement functionality, like getters/setters if you want.
    Last edited by twomers; 01-12-2012 at 05:58 AM. Reason: typo

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi

    So do we have to define the class outside of the main() function?

    Is it case sensitive, do we have to use class instead of Class?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If you define the class inside a function then it is only available for use inside the function you've defined it in. By making the definition global, all functions that need to make use of the class can do so. The individual instances of said class should be kept as local as possible but the definition should be global.

    The language is case sensitive, it must be class (all lowercase).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    reply

    Well,

    I want to do more with these data,thats why I wanted to use "Class" instead of "Struct".

    I want to have a small database sort of thing so that any user who wants to access the info of a perticular student can do it.


    Student info:


    Name:

    Address:

    Roll No:

    Class:

    In the case of class, these info is private but I wanted to know that how can we manipulate these data members in Classes?

    I have already defined functions "getter" and "setter" in the public of the class.



    I hope I am clear

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,

    I have got some other questions, how can I use constructor to initialize these data members inside the class?

    I have simply defined the constructor as "Student()".

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    It might be preferable to have a constructor so you can initialize all the members in one go.
    Also replace your "char" with "std::string". A "char" is not a string.
    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.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,

    I also wanted to know for what do we use "getter" and "setter" function?

    Thanks

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    To protect yourself from code changes. If the means of getting a value changes in the future (say that you suddenly start storing your data in a database), then you only have to change the getter function. The rest of your code that uses the getter function will keep on working like before.
    Otherwise you have to change every place that gets that value.
    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. Question about classes, and which way is best
    By Swarvy in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2008, 03:31 PM
  2. question about classes
    By tsut in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2003, 11:24 PM
  3. Another question on classes
    By hpy_gilmore8 in forum C++ Programming
    Replies: 26
    Last Post: 05-24-2003, 10:11 AM
  4. classes question
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-12-2003, 09:56 PM
  5. Question on Classes
    By phil001 in forum C++ Programming
    Replies: 5
    Last Post: 05-13-2002, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21