Thread: A Lil Help With Inheritance...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    A Lil Help With Inheritance...

    Ok I Got a lil project and am seeking a bit of help with my header files (interfaces). I just want some help to clear up the headers before i jump on the implementation...Thanks in advance for your help and here is the code.
    Code:
    #ifndef Insurance_header
    #define Insurance_header
    //Parent Class
    class MainIns{
    	int pol_num;//policy number
    	string pol_hol;//policy holder
    	int eff_date;//effective date of policy
    	int exp_date;//expiry date of policy
    	float ins_prem;//premimum for the policy
    public:
    	MainIns();//default constructor, used to instantiate the object
    	~MainIns();//Destructor
    		//Accessors
    		int getPolNum();
    		string getPolHol();
    		int getEffDate();
    		int getExpDate();
    		float getInsPrem();
    		
    			//Mutators
    			void setPolNum(int);
    			void setPolHol(string);
    			void setEffDate(int);
    			void setExpDate(int);
    			void setInsPrem(float);
    };
    
    #endif
    Code:
    #ifndef Life_Insurance_Header
    #define Life_Insurance_Header
    #include "Life_Insurance_Header.h"
    
    class LifeIns{
    	string per_name;
    	int per_age;
    	string fl_address;
    	string sl_address;
    	string occupation;
    	friend MainIns insurance(MainIns &);
    
    public:
    	LifeIns();//constructor
    	~LifeIns();//destructor
    		//Accessors
    		string getPerName();
    		int getPerAge();
    		string getFLAdress();
    		string getSLAdress();
    		string getOccupation();
    		
    		//Function to initialize attributes of MainIns for LifeIns
    		MainIns insurance (MainIns ins);
    			
    			//Mutators
    			setPerName(string);
    			setPerAge(int);
    			setFLAdress(string);
    			setSLAdress(string);
    			setOccupation(string);
    	
    
    
    
    
    #endif

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Hmm, inheritance is there to share and extend interfaces between objects. So instead of a Life Insurance type containing a Insurance type, you would borrow from and extend your Insurance type's interface. In that way, you are implementing Life Insurance in terms of the more general type.

    So say I had:

    Code:
    class Insurance {
      protected:
        int dateExpired;
        int dateEffective;
        string policyNumber;
        string policyHolder;
    
      public:
        Insurance();
        vitual ~Insurance();
        // etc...
    };
    With piblic inheritence we can take this general object, it's methods and it's data members, and make it something more useful.

    Code:
    class LifeInsurance: public Insurance {
      // Life Insurance only stuff here...
      public:
        LifeInsurance(); // build an Insurance object first, Life insurance specific stuff built on top of that
      // the interface here...
    };
    That's the basic idea. Life insurance would inherit from the base class the methods you implemented earlier and in Life Insurance, we could add data, like their occupation, and methods to use or expose that data to the rest of the program.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Quote Originally Posted by whiteflags View Post

    Code:
    class LifeInsurance: public Insurance {
      // Life Insurance only stuff here...
      public:
        LifeInsurance(); // build an Insurance object first, Life insurance specific stuff built on top of that
      // the interface here...
    };
    That's the basic idea. Life insurance would inherit from the base class the methods you implemented earlier and in Life Insurance, we could add data, like their occupation, and methods to use or expose that data to the rest of the program.
    ok that code there was giving me a thinking but i kinda understand it now. The question asked for like a type of policy so i thought the different polices would be the general class and inherit everything from the main insurance class, but what u put up awhile ago put my idea on the back bench....Thanks. When i start having problems with the implementation I post here too ok.

  4. #4
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    Code:
    int pol_num;//policy number
    string pol_hol;//policy holder
    int eff_date;//effective date of policy
    int exp_date;//expiry date of policy
    float ins_prem;//premimum for the policy
    Code:
    string per_name;
    int per_age;
    string fl_address;
    string sl_address;
    string occupation;
    may be you can put them into struct.... so it will be much more elegant...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #ifndef Life_Insurance_Header
    #define Life_Insurance_Header
    #include "Life_Insurance_Header.h"
    This looks a bit wrong. Either the include-guards are wrong, or you meant to include something else? Because to me, it looks like you are including the very same header file you were just in...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Quote Originally Posted by matsp View Post
    Code:
    #ifndef Life_Insurance_Header
    #define Life_Insurance_Header
    #include "Life_Insurance_Header.h"
    This looks a bit wrong. Either the include-guards are wrong, or you meant to include something else? Because to me, it looks like you are including the very same header file you were just in...

    --
    Mats
    i fixed that, but this what am doing is a work in progress for an oop class am doing at school. Here is the update to what i have now.
    Code:
    #ifndef Life_Insurance_Header
    #define Life_Insurance_Header
    #include "Insurance_header.h"
    class LifeInsurance: public MainIns{
    private:
    	string per_name;
    	int per_age;
    	string fl_address;
    	string sl_address;
    	string occupation;
    	int val_per;// estimate value of an object
    
    public:
    	LifeIns();//constructor
    	~LifeIns();//destructor
    		//Accessors
    		string getPerName();
    		int getPerAge();
    		string getFLAdress();
    		string getSLAdress();
    		string getOccupation();
    		float getValPer();
    
    		//Mutators
    		void setPerName(string);
    		void setPerAge(int);
    		void setFLAdress(string);
    		void setSLAdress(string);
    		void setOccupation(string);
    		void setValPer(float);
    		//bool DoYouSmoke (LifeInsurance &);//Accepts an object and returns true or false if the policy holder is a smoker or not
    		//void AccDeathDismenCov (LifeInsurance &);//Accepts an object checks if the client needs accidental death and dismenberment coverage
    		float calPremium(float);//Function that accepts the value of the person to be insured to calculate the premium of the person
    
    
    
    };
    		
    
    #endif
    Code:
    #ifndef MotorVehicle_Insurance_header
    #define MotorVehicle_Insurance_header
    #include "Insurance_Header.h"
    
    
    class MotVehicle_Ins: public MainIns {
    private:
    	string veh_mod;
    	string veh_make;
    	int year;
    	int license_num;
    	int chasis_num;
    	float est_veh_val;
    	string fuel_type;
    	string body_type;
    	int cc_rating;
    	int seat_cap;
    	string owner;
    	
    
    public:
    		MotVehicle_Ins();
    		~MotVehicle_Ins();	
    	
    		//Accessors
    		string getVehMod();
    		string getVehMake();
    		int getYear();
    		int getChasisNum();
    		int getLicenseNum();
    		float getEstVehVal();
    		string getFuelType();
    		string getBodyType();
    		int getCCRating();
    		int getSeatCap();
    		string getOwner();
    
    		//Mutators
    		void setVehMod(string);
    		void setVehMake(string);
    		void setYear(int);
    		void setChasisNum(int);
    		void setLicenseNum(int);
    		void setEstVehVal(float);
    		void setFuelType(string);
    		void setBodyType(string);
    		void setCCRating(int);
    		void setSeatCap(int);
    		void setOwner(string);
    
    		
    
    		//bool AccidentPrevYear(MotVehicle_Ins &);
    		//bool BurgularAlarm(MotVehicle_Ins &);
    		float CalculatePrem (float);//Accepts the value of the car then calculates the premium depending on certain criteria then returns a float
    
    
    
    };
    #endif
    Code:
    #ifndef Insurance_header
    #define Insurance_header
    #include "Date.h"
    #include <string>
    //Parent Class
    class MainIns: public Date {
    private:
    	int pol_num;//policy number
    	//string pol_hol;//policy holder
    	int eff_date;//effective date of policy
    	int exp_date;//expiry date of policy
    	float ins_prem;//premimum for the policy
    public:
    	MainIns();//default constructor, used to instantiate the object
    	~MainIns();//Destructor
    		//Accessors
    		int getPolNum();
    		//string getPolHol();
    		int getEffDate();
    		int getExpDate();
    		float getInsPrem();
    		
    			//Mutators
    			void setPolNum(int);
    			//void setPolHol(string);
    			void setEffDate(int);
    			void setExpDate(int);
    			void setInsPrem(float);
    };
    
    #endif
    Code:
    #ifndef Insurance_header
    #define Insurance_header
    #include "Date.h"
    #include <string>
    //Parent Class
    class MainIns: public Date {
    private:
    	int pol_num;//policy number
    	//string pol_hol;//policy holder
    	int eff_date;//effective date of policy
    	int exp_date;//expiry date of policy
    	float ins_prem;//premimum for the policy
    public:
    	MainIns();//default constructor, used to instantiate the object
    	~MainIns();//Destructor
    		//Accessors
    		int getPolNum();
    		//string getPolHol();
    		int getEffDate();
    		int getExpDate();
    		float getInsPrem();
    		
    			//Mutators
    			void setPolNum(int);
    			//void setPolHol(string);
    			void setEffDate(int);
    			void setExpDate(int);
    			void setInsPrem(float);
    };
    
    #endif
    Code:
    #include "Date.h"
    #include<iostream>
    using namespace std;
    Date :: Date()
    {
    	day=1;
    	month=1;
    	year=1;
    }
    Date :: Date(int d, int m, int y)
    {
    		day= d;
    		month= m;
    		year= y;
    	
    }
    
    void Date:: setDate1(int d, int m, int y)
    {
    	day= d;
    	month= m;
    	year= y;
    }
    
    int Date :: getDay()const
    {
    	if (day>=1 && day<=31){
    	return day;
    	}//end if
    }// ends fucntion
    
    int Date :: getMonth() const
    {
    	if(month>=1 && month<=12)
    	{
    		return month;
    	}//end if
    }//ends function
    
    int Date :: getYear() const
    {
    	return year;
    }
    Remember in the main insurance i had effect date attributes and expiry date, instead of using those i inserted a date interface where i can use that to intialize those dates. I cant wait to finish this so i can feel pleased within my self as a beginner programmer.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in header file you should not use using directive - so you should use fullyqualified types like

    std::string instead of just string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Quote Originally Posted by vart View Post
    in header file you should not use using directive - so you should use fullyqualified types like

    std::string instead of just string
    the code that i used the directive in was an implentation file for the date class. thanks for the string advice, thats why i was getting an error while debugging, thats y i also placed comments and any code segment with "string" going or returning from a function thanks.
    why is it when declaring a string this has to be used before you put the variable name ";"?
    Last edited by frassunit; 02-22-2009 at 04:47 PM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Ok, once more I have come to a buck with some syntax and i think i have tried everything i kno to correct it so here goes the code and thanks for the help you guys been giving me so far.
    Code:
    #include<iostream>
    #include"Vehicle_Header.h"
    using std::string;
    using namespace std;
    
    Vehicle:: Vehicle(){
    
    }
    Vehicle:: Vehicle(Vehicle&){
    
    }
    Vehicle:: ~Vehicle(){
    
    }
    string Vehicle:: getVehMod(){
    		
    	return veh_mod;
    
    }
    
    string Vehicle:: getVehMake(){
    
    	return veh_make;
    
    }
    
    int Vehicle:: getYear(){
    
    	return  year;
    
    }
    	  
    
    int Vehicle:: getChassisNum(){
    
    	return chassis_num;
    
    }
    
    
    int Vehicle:: getLicenseNum(){
    
    		return license_num;
    
    }
    float Vehicle:: getEstVehVal(){
    
    	return veh_val;
    
    }
    
    
    string Vehicle:: getFuelType(){
    
    	return fuel_type;
    
    }
    	
    string Vehicle:: getBodyType(){
    
    	return body_type;
    
    }
    
    int Vehicle:: getCCRating(){
    
    	return cc_rating;
    	
    }
    
    int Vehicle:: getSeatCap(){
    
    	return seat_cap;
    
    }
    
    string Vehicle:: getOwner(){
    
    	return  owner;
    
    }
    
    void Vehicle:: setVehMod(string model){
    		
    	string veh_mod= model;
    
    }
    	
    	
    	
    	
    
    	
    	
    	
    
    	void Vehicle:: setVehMake(string make){
    
    	
    		string veh_make= make;
    
    	}
    	void Vehicle:: setYear(int veh_year){
    		
    		int year= veh_year;
    
    	}
    	void Vehicle:: setChassisNum(int chassis){
    
    		int chassis_num= chassis;
    
    	}
    		  void Vehicle:: setLicenseNum(int license){
    
    			  int license_num= license;
    
    	  }
    		  void Vehicle:: setEstVehVal(float value){
    
    			   veh_val= value;
    
    		  }
    
    		  void Vehicle:: setFuelType(string fuel){
    
    			  string fuel_type= fuel;
    
    		  }
    
    
    		  void Vehicle:: setBodyType(string body){
    
    			  string body_type= body;
    
    		  }
    		  void Vehicle:: setCCRating(int rating){
    			  
    				int cc_rating= rating;
    
    		  }
    		  void Vehicle:: setSeatCap(int capacity){
    
    			  int seat_cap= capacity;
    
    		  }
    
    
    		  void Vehicle:: setOwner(string own){
    
    			  string owner= own;
    
    		  }
    
    	  //void Vehicle:: ShowClientDetails(Client&)
    		  bool Vehicle:: Burlar_Alarm(void){
    			
    			  	
    	bool bur_alarm;
    	
    		  }
    	
    	
    bool Vehicle:: Any_Accident(void){
    		string accident;
    		string no_accident;
    		cout<<"\nHas The Vehicle Been through any accidents?"<<"\nYes/No:";
    		cin>> accident;
    			if (accident= "yes" && "Yes"){
    				return 1;
    			}
    			else 
    				return 0;
    			
    
    		
    }
    here are the errors: vehicle_implimentation.cpp(163) : error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
    2. binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
    Last edited by frassunit; 03-02-2009 at 08:44 PM. Reason: forgot to put on the errors i was getting...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is there an include for <string> somewhere?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    ok i got rid of 2 of the problems , i only have a condional error now. i think its my if statement that is the problem. gonna continue fishing around it to see if i can get rid of the problem...

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (accident= "yes" && "Yes"){
    Is definitely not right (I thought I edited that into the previous post, but apparently not). I'm not sure that is what the "conditional" is about, quite possibly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    error C2677: binary '||' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)

    when i get this error what am i facing?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by frassunit View Post
    error C2677: binary '||' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)

    when i get this error what am i facing?
    Can you post a line or three around that?

    It sounds like you are doing || with one side as a string - which would be a rather strangt thing to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Code:
    bool Vehicle:: Any_Accident(void){
    		string accident;
    		
    		cout<<"\nHas The Vehicle Been through any accidents?"<<"\nYes/No:";
    		cin>> accident;
    			if (accident== "yes" || accident== "Yes"){
    
    
    				return 1;
    			}
    			else 
    				return 0;
    			
    
    		
    }
    OK the error was gone when i used two equal signs cuz i forgot that what am doing is something similar to strcmp...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM