Don't Understand Class

This is a discussion on Don't Understand Class within the C++ Programming forums, part of the General Programming Boards category; I remember doing very simple classes in c++ before but I'm having a lot of trouble understanding this use of ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Don't Understand Class

    I remember doing very simple classes in c++ before but I'm having a lot of trouble understanding this use of class. One that reads from file into an array. I would ask my teacher but he just makes it sound ever more confusing and the book that I'm using doesn't even cover how to use a class this way.

    Its suppose to read a city and the average temperature for each city.

    Here's a piece of the code he wants us to use:

    PHP Code:
    #ifndef _ASSIGNMENT_8_CPP_
    #define _ASSIGNMENT_8_CPP_

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <cstdlib>

    using namespace std;

    class 
    DATA {
    private:
        
    char city[20][25];
        
    int temps[20];
        
    int HIGH
        
    int LOW
        
    float AVE
        
    char HIGH_city[25]; 
        
    char LOW_city[25];
    public: 
        
    DATA(); 
        
    void FileRead(char fname[25]); 
        
    /* I was taught to do it like "void FileRead(char);". Why is it that way?*/
        
    void FindMax(); 
        
    void FindMin(); 
        
    void FindAve(); 
        
    void ShowData();
        
    void Print();
    };

    DATA::DATA()
    {
             
    //Isn't there suppose to be some code in here?
    }

    void DATA::FileRead(char fname[25])//Don't understand why "fname" is an array of char
    {
        
    ifstream infile(fname);
        
    int i 0size 0;
        
    char temp[32];
        
        while(!
    infile.eof())
        {
            
    infile.getline(temp30' ');
            
    strcpy(city[i],temp);//Gives me error: " deprecated conversion from string constant to ‘char*’ "
            
            
    infile.getline(temp30);
            
    temps[i] = atoi(temp);
            
            
    i++;
            
    size++;
        }
          
    /*How am I suppose to use this outside of this function?*/

        
    infile.close();

    Last edited by Sherina; 12-03-2009 at 02:16 PM. Reason: Clarification

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    What do you have trouble understanding?
    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.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    A class called DATA?

    That's about the worst thing I've ever seen. That's like a deliberate slap in the face of OOP (and good software engineering in general).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    what brewbuck said...

    and also, in an application you include that class, then you declare an instance of the object to access the member.

    ... and you don't *have to put code in to constructor's/destructor's, just if it applies or if you need to, it's something you decide based on what you're doing.

    Code:
    #include "ASSIGNMENT 8.CPP" // ?
    
    int main()
    {
    	DATA *lolData = new DATA();
    	lolData->FileRead("lotsofdata.txt");
    }
    ... you've also left out an #ENDIF at the end there on your class file, and you should probably use a header file for 'awesome practice' compliance to impress your teacher.
    Last edited by since; 12-03-2009 at 02:19 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    The FileRead function is flawed in that it will process the last line of the file twice due to its use of eof to control the looping. Let your instructor know I said that. The eof flag that is tested is only set after an attempt to read past the end of the file fails. When you've successfully reached the last line of the file and processed the data within, eof is still false even though you are physically at the end of the file. Because of this you'll end up going back into the loop and attempting to do the two getline calls which will then fail and set eof to true however at this point you are already in the loop processing bad data.

    Code:
    DATA::DATA()
    {
             //Isn't there suppose to be some code in here?
    }
    I don't know... maybe? There doesn't necessarily have to be unless you want the constructor to explicitly do certain "stuff".
    "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
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by brewbuck View Post
    A class called DATA?

    That's about the worst thing I've ever seen. That's like a deliberate slap in the face of OOP (and good software engineering in general).
    Reminds me, a lot of people still use "Data" as a suffix to a lot of their classes. VertexData, PhysicsData, TextureData, ObjectData, MatrixData, etc. DATA is pretty bad, but besides the caps, not that big a deal especially since it's just schoolwork and not a real personal project.

    http://cboard.cprogramming.com/game-...tml#post522672
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Thank You all so much for your help. However I still don't know how to use the data I got in the FileRead function in all the other functions.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The data is read in to private members, so if you want to do something with the data other than what is provided through the public functions, you'll need to modify the class and add your own functions.

Popular pages Recent additions subscribe to a feed

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