Need help understanding Structure and instantiate

This is a discussion on Need help understanding Structure and instantiate within the C++ Programming forums, part of the General Programming Boards category; Hi all, I went through my book's chapter about structures and it talks instantiate. I'm trying to make sure I ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Need help understanding Structure and instantiate

    Hi all,

    I went through my book's chapter about structures and it talks instantiate. I'm trying to make sure I understand what this means since it's used in a lot of places but I can find an exact definition of what it consists in.

    A structure could be:
    Code:
    struct employee{
    char name [20]   // array
    int ID
    float hours
    }
    then I instantiate in Main with something like:
    Code:
     employe employeeschedule;
    So can anyone tell me if I understand this concept correctly?
    Thanks.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    As formally defined by a Google search of "define: instantiate"

    Quote Originally Posted by Google
    To create or generate a particular instance or object of a data type, often a template class. For example, an instance box1 of class box could be instantiated with the declaration: box box1;
    So, yes, you're right about what instantiation is.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you are correct, except you need to put semicolons ; at the end of each line.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks jverkoey , I did google but didn't get that one ... or maybe my eyes aren't opened enough...
    Thanks Ancient Dragon I promis not to forget anymore...

    Thanks again guys!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    There are also other ways to instantiate objects. Basically any time you create a new instance, you are instantiating. For example, you could create an instance without a variable name that you immediately pass to a function. If that doesn't make sense, don't worry, just remember that creating a variable like you did is the most common example of instantiating, but it is not the only one.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks Daved! I will try to remeber what you said.
    Actually I truied to make sense of this by doing a program and I would like you guys to let me know if this is a correct way of doinf it. I'll admit that I'm getting 1 error I don't really understand what it means. If any of you can help I'd really appriciate this.
    Code:
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    struct StudentRecord{
    	char Name[20];
    	int ID;
    	float GPA;
    } ClassStudent;
    
    void printrecord (StudentRecord ClassStudent);
    int main()
    {
    ClassStudent.Name = "SuperProgrammer    ";
    ClassStudent.ID = 1234;
    ClassStudent.GPA = 4.0;
    printrecord (ClassStudent);
    
      cout << "Enter Name: ";
      cin.getline(ClassStudent.Name,20);
      cout << "Enter ID: ";
      cin.getline(ClassStudent.ID)
      cout << "Enter GPA: ";
      cin.getline(ClassStudent.GPA)
      printrecord(ClassStudent);
      return 0;
    }
    
    void printrecord (StudentRecord ClassStudent)
    {
      cout << “The Student’s Name is: “<<ClassStudent.Name<<“ ID:” <<ClassStudent.ID<< ClassStudent.GPA<<“ GPA:”<<endl;
    }
    Here is the error code I get:
    Code:
    test01.cpp(23) : error C2440: '=' : cannot convert from 'const char [20]' to 'char [20]'
    Thanks for your help.

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    It's in ClassStudent.Name = "Super Programmer". I would change the Name member in the struct to char* Name; see if that works.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks durban.
    I just tried what you said and it's not working either.
    Now I'm getting a bunch of errors I have never seen before (ok I'm new at all this) like:
    Code:
     error C2228: left of '.Name' must have class/struct/union

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Or strcpy. Another nifty thing of C++ structs, is that they are nearly identical to classes, and can have constructors.

    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    struct StudentRecord {
    	char Name[20];
    	int ID;
    	float GPA;
    } ClassStudent;
    
    void printrecord (StudentRecord ClassStudent);
    int main()
    {
    	strcpy(ClassStudent.Name, "SuperProgrammer");
    	ClassStudent.ID = 1234;
    	ClassStudent.GPA = 4.0;
    	printrecord (ClassStudent);
    
    	cout << "Enter Name: ";
    	cin.getline(ClassStudent.Name, 20);
    	cout << "Enter ID: ";
    	cin >> ClassStudent.ID;
    	cout << "Enter GPA: ";
    	cin >> ClassStudent.GPA;
    	printrecord(ClassStudent);
      return 0;
    }
    
    void printrecord (StudentRecord ClassStudent)
    {
    	cout << "The Student’s Name is: "<< ClassStudent.Name 
    		 << "\nID: " << ClassStudent.ID 
    		 << "\nGPA: "<< ClassStudent.GPA << endl;
    }
    I would also, as always, advise the use of std::string's of STL, reap the benefits of C++.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks so much Tonto!
    I will remember that I need to use strcpy and strings for characters.
    It's working now just fine.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Another nifty thing of C++ structs, is that they are nearly identical to classes, and can have constructors.
    Say it with me now:
    structs are classes
    structs are classes
    structs are classes
    structs are classes
    structs are classes

    The only difference between a struct and a class is that struct defaults public and class defaults private.

    Code:
    struct {
    private:
    is the same as
    Code:
    class {
    and
    Code:
    class {
    public:
    is the same as
    Code:
    struct {

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The best solution to your error is to use the C++ string class instead of char[20] or char*. Those are C style strings, and C++ has a tool that is much better for the job. If you #include <string> (without the .h) and add using std::string, then you can define Name as: string Name; and you can use = to assign a value to it naturally. You will also have to change your call to getline (there is a different version for strings, but the good part is you don't have to worry about the number - it can handle any size): getline (cin, ClassStudent.Name);

    BTW, your program instantiates StudentRecord three times. Once is a global variable called ClassStudent on line 14. The other two are when you call printrecord. Since printrecord takes its parameter by value, a copy of the data is made and passed to the function, so both times you call it you instantiate a new object that is a copy of the one you passed in.

  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> structs are classes ... The only difference ...

    But there is that little difference; I hate mentioning it every time

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Sorry I couldn't check in earlier!

    Ok Tonto I'm reapeating after you:

    Say it with me now:
    structs are classes
    structs are classes
    structs are classes
    structs are classes
    structs are classes

    The only difference between a struct and a class is that struct defaults public and class defaults private.

    Daved, Thanks so much For this information. You help me learn a lot on C++ and it's really great. What you say about string. you're absolutely right by saying that I went with C style string since I know C it comes easier. I'll correct the program to do a "more correct" C++ program. I think it's iportant when learning to do things right.
    Thanks so much guys.

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    I've got a kind of other problem, with some similarities.

    I'd like to make an array of TLabel (I'm working in C++ Builder) to be able to reference to them as Label[1], Label[2], etc, so their properties can be edited in loops. I think that I'll have to define a new class, which has a TLabel part, and something else.

    How should I do it? I tried the
    Code:
     class{
              Label TLabel; ....
           }
    but I get error messages all the time. I had no better idea. Anybody could help pls?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abstract class (IDispatch) in a C structure
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2008, 07:38 AM
  2. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

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