Class and Inheritance Example

Learning a programming language is hard enough without having to wrangle your computer into compiling and executing your code. From beginners to experts, the tips here will make life in C++ easier.
Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

Class and Inheritance Example

Post by darknkreepy3# »

Code: Select all

#include <cstdlib>
#include <iostream>

/*
understanding class and inheritance. by Kristoffe Brodeur. ©2010 All Rights Reserved.
06-10-2010
*/
using namespace std;

class Employee
     {
	public:
	     //default constructor
		Employee();
		
		/*
		parameterized constructor
		a char is an array of characters, one to however many...
		[] means array sentName has ??? I don't know how many characters, just fill it in when it comes here
		*/
		Employee(char sentName[]);
		
		//methods (functions etc...)
		// whatAmI myName (whatAreYouSendingMe)
		void showInfo(void);
		
		//variables
		/*
		a string is a basically limitless expandable character array, so it grows with your input to it
		c++ does this invisibly for us
		char string[]={'a','b','r','a','h','a','m',' ','l','i','n','c','o','l','n','\0'};
		'\0' meaning NULL end of string (invisible to us)
		*/
		string eName;	   	  
		
     };
     
void Employee::showInfo(void)
     {
     cout << "EMPLOYEE CLASS CALLS (showInfo)\n"
	 << "Name:" << eName << endl;
     }
     
Employee::Employee()
     {
     cout << "\nDefault constructor called\n";
	}     
	
Employee::Employee(char sentName[])
	{
    cout << "\nParameterized construcor called\n";
	cout << "variable char array sentName is:" << sentName << endl;
	/*
	copy a character array (sentName) into a string(eName)? no problem
	c++ understands and copies it all over in a loop 'under the hood' i.e. 'behind closed doors'
	*/
	eName=sentName;
	cout << "and to test to see if the copy worked... " << eName << endl;
	}
					
/*
---------------------------------------------------------------------
derived class Manager : Employee 
---------------------------------------------------------------------
*/

/*
public here means, I want to be an Employee AND have my own functions, variables
I ALSO want to access the PUBLIC things in EMPLOYEE if I want to.
*/
class Manager:public Employee
     {
	private:
          //variables, methods(only the class can see, even an instance in main can't call it and see it, cout, etc
		string privateKeyCode; 
		   
	public:
	     //default constructor
          Manager();
          
          //parameterized constructor
          /*
          In a big company like The Gap, Radio Shack, Target...
          a MANAGER can have a keyCode to a safe that holds the store money that an EMPLOYEE shouldn't
          so, when we make a MANAGER, let's ADD 'keyCode' as well as 'sentName'
          
          notice also that you ONLY need to send the vars in Employee to the call Employee(****) not keyCode, etc
          the original class only needed one variable, the new one Manager has keyCode and uses it only for iself.
          share only what Employee needs to instantiate its 'like' or 'similar' constructor you're calling
          */
		Manager(char sentName[],char keyCode[]):Employee(sentName)
            {
			cout << "DERIVED parameterized constructor called\n";
			cout << "As a manager I have a keyCode only I can see.";
			privateKeyCode=keyCode;
			}
			
		string policeAccessToCode(string password)
		    {
			string retStr="";
			//
			if(password=="donkeykong")
			     {
	             retStr=privateKeyCode;
				 }	   
			else
			     {
				 retStr="\naccess denied. invalid password.\n"; 	   
				 } 
			return retStr;	
			}
		
				
     };
     
Manager::Manager()
     {
     cout << "\nDerived class called\n";
	}


int main(int argc, char *argv[])
{
    cout << "-----Testing Employee class\n";
    Employee e1; 
    /*
    Double quotes do not send character strings, single quotes do. 
    BUT! If you are sending an array of characters to a variable type 'char' like 'char sentName[]'
    it is stating, I need a string of characters, so go BACK to double quotes to send inline "mary" :)
    */
    Employee e2("mary");
    e2.showInfo();
    
    cout << "-----Testing Manager class\n";
    Manager m1;
    Manager m2("william","202345");
    m2.showInfo();
    /*
    this won't work. luckily, a private variable, method, function, is totally hidden once defined :)
    but...if you wrote a function INSIDE of 'Manager' that was public which returned the 'privateKeyCode' you could see it.
    */
    
    //cout << m2.privateKeyCode << endl;
    cout << "\n**Accessing keyCode**\n"<< m2.policeAccessToCode("popeye123");
    cout << "\n**Accessing keyCode**\n"<< m2.policeAccessToCode("donkeykong");
    system("PAUSE");
    return EXIT_SUCCESS;
}
Post Reply