creating class, and linking files

This is a discussion on creating class, and linking files within the C++ Programming forums, part of the General Programming Boards category; Hello Everytone, I have made this far in this beginning class thanks to all your help.... And now I'm having ...

  1. #1
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25

    creating class, and linking files

    Hello Everytone,

    I have made this far in this beginning class thanks to all your help.... And now I'm having a BIG problem working on my last project......
    I'm trying to make a cliend code file that will link to a class that contains few functions (specifications), and I also need to make a implementation file for the class. I think I got some general ideas as how specification and implementation work, however, would someone please give me some beginner pointers as how those things work together, such as some skeletal codes.

    In addition, I'm using the Microsoft visuall ++ software to create all the files. I read the book that says the integrate environment (such as MS C++) will link all files automatically. However, mine didn't seem to work. I've used #include "class.h" on the heading for the client code and implementation, what else did I miss?

    Thanks so much again everyone!!

  2. #2
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25

    my class and client code

    Here is my class code and client code:


    Code:
    //Specification of the class "Applicant"
    
    
    class Applicant
    {
    public:
    	float Calculate_BasicPremium (string, int, string);
    	//calculate the basic premium based on the applicant's gender, age, and coverage type
    	
    	//Preconditions: 
    	//valid data are provided, such as the applicant's first and last name, gender,
    	//age, type of coverage
    
    	//Postconditions:
    	//The basic premium is calculated based on the data provided
    
    	float Discount_Premium (float basic_premium, string club_member, string smoker);
    	//discount the basic premium based on the applicant's health status, and smoking 
    	//status
    
    	//Preconditions:
    	//The Calculate_BasicPremium function has been executed, and a valid value has been
    	//returned, plus that the applicant's health club and smoking status are known
    
    	//Postconditions:
    	//Discount premium is calculated based on the information given above
    
    	float Adjust_Premium (float discount_premium, int zip_code);
    	//adjust the premium based on the zip code 
    	
    	//Preconditions:
    	//Both Calculate_BasicPremium and Discount_Premium functions have been executed, and 
    	//the applicant's zip code is provided
    
    	//Postconditions:
    	//Applicant's final premium is calculated
    
    	Applicant();				//Constructor
    
    private:
    	string first_name;				//declare string variable first name
    	string last_name;				//declare string variable last name
    	string gender;					//declare string variable gender
    	int age;						//declare int variable age
    	string type_coverage;			//declare string variable type of coverage
    	string club_member;				//declare string variable health club
    									//member
    	string smoker;					//declare string variable smoker
    	int zip_code;					//declare int variable zip code
    	float basic_premium;			//declare float variable basic premium
    	float discount_premium;			//declare float variable premium
    	float adjust_premium;			//declare float variable adjust premium
    	float premium;					//declare float variable variable premium
    };
    Here is the client code:

    Code:
    #include "applicant.h"
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    
    
    using namespace std;
    
    
    int main()
    {
      string first_name;            //declare string variable first name
      string last_name;             //declare string variable last name
      string gender;                //declare string variable gender
      int age;                      //declare int variable age
      string type_coverage;         //declare string variable type of coverage
      string club_member;           //declare string variable health club
                                    //member
      string smoker;                //declare string variable smoker
      int zip_code;                 //declare int variable zip code
      float basic_premium;			//declare float variable basic premium
      float discount_premium;		//declare float variable premium
      float adjust_premium;			//declare float variable adjust premium
      float premium;				//declare float variable variable premium
      ifstream inFile;              //declare file streams
      ofstream outFile;
    
      inFile.open("C:\\Windows\\C++\\project5\\proj5.in.txt");      //opens the input file
      outFile.open("C:\\Windows\\C++\\project5\\proj5.out.txt");    //opens the output file
    
      if (!inFile)
      {
        cout << "Input file failed!" << endl;
        return 1;                   //terminates the program if input fails
      }    
    
      inFile >> first_name >> last_name >> gender >> age >> type_coverage >>
      club_member >> smoker >> zip_code;    //assigns input values from inFile
    
      //prints subject line
      outFile << "===================================" << endl;  
      outFile << "           PREMIUM QUOTES          " << endl;
      outFile << "===================================" << endl;
    
      outFile << "Name" << "\t" << "\t" << "Coverage Type" << "\t" << "Zip" <<
      "\t" << "Premium" << endl << endl;    //prints headline                               
    
      while (inFile)                        //looping in capturing &
                                            //processing input data
      {
    	  basic_premium = Applicant.Calculate_BasicPremium (gender, age, type_coverage);
    	  discount_premium = Applicant.Discount_Premium (basic_premium, club_member, smoker);
    	  premium = Applicant.Adjust_Premium (discount_premium, zip_code);
    
    	  outFile << first_name << "\t" << last_name << "\t" << type_coverage
          << "\t" << "\t" << zip_code << "\t" << "$" << premium << endl;
          //output premium to file output                                     
    
          inFile >> first_name >> last_name >> gender >> age >> type_coverage >>
          club_member >> smoker >> zip_code;  //assigns input values to inFile
      }
    
      if(!outFile)
      {
        cout << "Output file failed!" << endl;
        return 1;                   //terminates the program if output fials
      }
      return 0;
    }

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, what errors are you getting?
    I noticed that you include Applicant.h before string.h, meaning the compiler can't recognice strings in your class declaration.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    Ah....HA! yes, I'm getting a whole bunch of errors about syntax error..... that's why.... it is funny my teacher didn't talk anything about that and the book also didn't mention it....

    but what is string.h?

    Thanks so much!!

    here is just a small sampling of all the errors I got from compiling the client code.....

    c:\windows\c++\project5\applicant.h(17) : error C2061: syntax error : identifier 'string'
    cerror : missing ';' before identifier 'first_name'
    c:\windows\c++\project5\applicant.h(51) : error C2501: 'string' : missing storage-class or type specifiers

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Change this
    Code:
    #include "applicant.h"
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    Into this
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include "applicant.h"
    and see if it works better.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    Magos,

    Thanks so much for the help!! I changed the headings but it still gave me the same errors....
    do you think it could the capitalization problem?? I use "applicant.h" as the file name, however, when I declared the class, I used "Applicant"... and everywhere else....


  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    What did you save applicant as? it is case sensative.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    No, capitalization doesn't matter when including files, only when declaring variables.

    <edit>This reply was pointed at JCK </edit>

    I never see you use the class anywhere. I don't think it should matter though, since it seems like the compiler doesn't recoginze the string datatype.

    <edit2>And why do you create all those variables again in main? You don't think you have an error in your planning?</edit2>
    Last edited by Magos; 12-08-2002 at 11:18 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    I used the class in the while loop in the main function.....
    the class then calls another file "applicant.cpp" which contains the details of the function. I didn't post it because it is too long.....
    Do you think perhaps there is problem in that file?

    I erased all the variables in the main function, but still getting error that say "syntax error, idendifier string"...

  10. #10
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    I used the class in the while loop in the main function.....
    the class then calls another file "applicant.cpp" which contains the details of the function. I didn't post it because it is too long.....
    Do you think perhaps there is problem in that file?

    I erased all the variables in the main function, but still getting error that say "syntax error, idendifier string"...

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    class Applicant
    {
    ...
    };

    basic_premium = Applicant.Calculate_BasicPremium (gender, age, type_coverage);
    You can't call the methods directly from the class (unless they're static). You have to create an instance first.

    Code:
    myclass.h
    
    class MYCLASS
    {
       public:
          void MyMethod();
    };
    Code:
    myclass.cpp
    
    MYCLASS::MyMethod()
    {
       ...
    }
    Code:
    myapplication.cpp
    
    int main()
    {
       MYCLASS MyClass;
    
       MyClass.MyMethod();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    JCK
    JCK is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    Thanks so much Magos, although I'm still a little confused.....
    so, what's a constructor then.... ?

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by JCK
    Thanks so much Magos, although I'm still a little confused.....
    so, what's a constructor then.... ?
    A piece of code that is run when the object is created.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 01:31 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Derived class linking error
    By Enahs in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2005, 10:18 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Linking 3 files in Borland C 3.11
    By Brain Damage in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2005, 03:53 AM

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