Thread: 2 classes merge into 1? (newbie)

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    2 classes merge into 1? (newbie)

    im writing a program that gets employee info.
    employee number
    compensation
    employee type
    date of hire

    i have 1 class for the employee info:
    Code:
    class employee
    {
    
    private:
    	int number;
    	float comp;
    	enum etype	{ laborer, secretary, manager };
    
    public:
    
    	void input()
    	{
    		char n;
    
    		cout << "Enter in employee number :  ";	cin >> number;
    		cout << "Enter employee comp:  ";		cin >> comp;
    		cout << "Enter in employee type(1 = laborer,2 = secretary,3 = manager):  "; cin >> etype;
    	}
    
    	void display()
    	{
    		cout << "emp#:  " <<  number << endl;
    		cout << "comp$:  " << comp << endl;
    		
    		switch(etype)
    		{
    		case '1':	cout << "type:  laborer"; break;
    		case '2':	cout << "type:  secretary"; break;
    		case '3':	cout << "type:  manager"; break;
    		}
    		cout << endl;
    	}
    };
    and another class for a date of hire:
    Code:
    class date
    {
    
    private:
    	int day;
    	int month;
    	int year;
    
    public:
    	void getdate()
    	{
    		char n;
    		cout << "enter in a date (mm/dd/yy):  ";
    		cin >> month >> n >> day >> n >>  year;
    	}
    
    	void showdate()
    	{
    		cout << month << "/" << day << "/" << year;
    		cout << endl;
    	}
    };
    my question is..would it be logical to just merge these together into one class? can i have a class within a class?

    Code:
    class emp
    {
            private:
                 ......
            public:
                 ......
            class date
            {
                 ..and so on
            };
    };
    if so, how do i define an instance. please keep in mind that the farthest ive gone in c++ is loops & decisions, functions, and classes. nothing else. im just beginning. thanks in advance for response and sorry for the lengthy code.
    Last edited by xion; 05-10-2004 at 03:48 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    you can have the emplyee class have a date variable:
    Code:
    class emp
    {
    private:
                 ......
                 date hireDate;
    public:
                 ......
    };
    Why drink and drive when you can smoke and fly?

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    so define the date class outside the emp class and create an instance of the date class in the emp? ill give that a try.

    edit: thanks it worked. i needed to define the date class before the emp class. this exercise required that i use enumerations but i really dont see the point in using them? much more complicated with them so i left it out.

    here it is:
    Code:
    /////////////////////////////////
    class date
    {
    
    private:
    	int day;
    	int month;
    	int year;
    
    public:
    	void getdate()
    	{
    		char n;
    		cout << "enter in a date (mm/dd/yy):  ";
    		cin >> month >> n >> day >> n >>  year;
    	}
    
    	void showdate()
    	{
    		cout << month << "/" << day << "/" << year;
    		cout << endl;
    	}
    };
    ////////////////////////////////
    ////////////////////////////////
    class employee
    {
    
    private:
    	int number;
    	float comp;
    	//enum etype { laborer, secretary, manager };
    	//etype type
    	int i;
    	date hireDate;
    
    public:
    	void input()
    	{
    
    		cout << "Enter in employee number :  ";	cin >> number;
    		cout << "Enter employee comp:  ";		cin >> comp;
    		cout << "Enter in employee type(1 = laborer,2 = secretary,3 = manager):  "; cin >> i;	//cin >> type
    		hireDate.getdate();		
    	}
    
    	void display()	
    	{
    		cout << "emp#:  " <<  number << endl;
    		cout << "comp$:  " << comp << endl;
    		hireDate.showdate();		
    		
    		switch(i)	//switch(type)
    		{
    		case '1':	cout << "type:  laborer"; break;
    		case '2':	cout << "type:  secretary"; break;
    		case '3':	cout << "type:  manager"; break;
    		}
    		cout << endl;
    	}
    };
    the comments are what it was before but it kept giving me an error. can someone explain to me the reasons of using an enum type in this situation? ive read the book but it doesnt seem logical to use them. and if its logical to use them how would i implement them?
    Last edited by xion; 05-10-2004 at 05:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with winAPI in classes (newbie question)
    By Spyril in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2007, 03:21 PM
  2. c++ classes newbie
    By johnnyboi in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 10:17 PM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. On declaring classes. (Newbie)
    By suzakugaiden in forum C++ Programming
    Replies: 26
    Last Post: 01-31-2005, 06:15 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM