where do i define a struct in my code using an array class

This is a discussion on where do i define a struct in my code using an array class within the C++ Programming forums, part of the General Programming Boards category; is it any different when using a class in my code my previous code i define my struct like this ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    where do i define a struct in my code using an array class

    is it any different when using a class in my code

    my previous code i define my struct like this

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    struct{
               int type 1;
               string type2;
               double type 3;
               string type 4;
    };
    
    void print(){
                    //code to print in here 
    
    };
    
    int main(){
    
    print();
    
    
    }
    but i have to use a class now

    so my code looks something like this

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    
    class Array_Class
    {
        public:
            
            void Print();
            
        private://behavior
    
    
            int count;   //do I define the struct here 
            int *A;
    
    
    
    
    };
    
    int main(){
    
    print();
    
    }
    or do i still define it the same way at the top of my code.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The only difference is that in a struct all members are public by default.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    In object oriented programming, the implementation of the class, the data of the class, should be private so that access to it and modifications to objects can be checked. The interface, the functions which provide access to data and modify data, should be public. Whether you declare a struct inside a class is up to you.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Your "previous example", as you've shown it, isn't valid code. The code you have now is just as good, in the sense that it won't compile either.

    In C++, a class is the same as a struct, except that default access is private in a class and public in a struct.

    If this doesn't seem helpful that is because you haven't really asked a question that is intelligible to anyone other than you. Read this link.
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by c++noob145 View Post
    do I define the struct here ... or do i still define it the same way at the top of my code.
    The answer to that question is "No". You don't make a struct as well. The class is instead of a struct.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. define string to struct var
    By siperi in forum C Programming
    Replies: 4
    Last Post: 10-22-2010, 09:28 AM
  2. calling struct class within array
    By eastmus in forum C Programming
    Replies: 6
    Last Post: 12-31-2008, 08:23 AM
  3. class problem (init. struct, itierator for 2d array)
    By avgprogamerjoe in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2007, 09:00 PM
  4. #define within struct definition?
    By fanoliv in forum C Programming
    Replies: 11
    Last Post: 06-23-2006, 07:24 PM
  5. struct and define problem
    By hurry up in forum C Programming
    Replies: 1
    Last Post: 03-19-2003, 11:37 AM

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