![]() |
| | #1 |
| Registered User Join Date: Oct 2002
Posts: 25
| creating class, and linking files 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!! |
| JCK is offline | |
| | #2 |
| 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
};
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;
}
|
| JCK is offline | |
| | #3 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| 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. |
| Magos is offline | |
| | #4 |
| 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 |
| JCK is offline | |
| | #5 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Change this Code: #include "applicant.h" #include <iostream> #include <string> #include <iomanip> #include <fstream> Code: #include <iostream> #include <string> #include <iomanip> #include <fstream> #include "applicant.h"
__________________ 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. |
| Magos is offline | |
| | #6 |
| 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.... |
| JCK is offline | |
| | #7 |
| Registered User Join Date: Jun 2002
Posts: 230
| What did you save applicant as? it is case sensative. |
| gamer4life687 is offline | |
| | #8 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| 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>
__________________ 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. Last edited by Magos; 12-08-2002 at 11:18 AM. |
| Magos is offline | |
| | #9 |
| 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"... |
| JCK is offline | |
| | #10 |
| 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"... |
| JCK is offline | |
| | #11 | |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Quote:
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. | |
| Magos is offline | |
| | #12 |
| 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.... ? |
| JCK is offline | |
| | #13 | |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Quote:
__________________ 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. | |
| Magos is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linking error with static template class members | cunnus88 | C++ Programming | 6 | 04-02-2009 12:31 PM |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| Derived class linking error | Enahs | C++ Programming | 3 | 11-12-2005 10:18 PM |
| Polymorphism Theory Question - Polymorphic Class Definition. | ventolin | C++ Programming | 3 | 10-31-2005 12:05 PM |
| Linking 3 files in Borland C 3.11 | Brain Damage | C++ Programming | 5 | 05-29-2005 02:53 AM |