C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-08-2002, 09:34 AM   #1
JCK
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!!
JCK is offline   Reply With Quote
Old 12-08-2002, 09:43 AM   #2
JCK
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;
}
JCK is offline   Reply With Quote
Old 12-08-2002, 10:12 AM   #3
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 12-08-2002, 10:38 AM   #4
JCK
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   Reply With Quote
Old 12-08-2002, 10:57 AM   #5
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 12-08-2002, 11:06 AM   #6
JCK
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   Reply With Quote
Old 12-08-2002, 11:12 AM   #7
Registered User
 
Join Date: Jun 2002
Posts: 230
What did you save applicant as? it is case sensative.
gamer4life687 is offline   Reply With Quote
Old 12-08-2002, 11:14 AM   #8
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 12-08-2002, 11:47 AM   #9
JCK
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   Reply With Quote
Old 12-08-2002, 12:00 PM   #10
JCK
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   Reply With Quote
Old 12-08-2002, 12:10 PM   #11
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
Quote:
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.
Magos is offline   Reply With Quote
Old 12-08-2002, 12:29 PM   #12
JCK
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   Reply With Quote
Old 12-08-2002, 02:45 PM   #13
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
Quote:
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.
Magos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:34 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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