Thread: Class inheritance

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    Class inheritance

    Does this statement make sense in regards to the following program? Be gentle

    (Derived class employee)’s header takes an argument to (base class person) which gives it access to (base class person)’s properties.

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class Person
    {
    protected:
    	string firstName;
    	string lastName;
    
    public:
    	Person(void)
    	{
    		cout << "In the Person constructor (a derived class)" << endl;
    		cout << "Enter a first name: ";
    		cin >> firstName;
    		cout << "Enter a last name: ";
    		cin >> lastName;
    	}
    
    	string getFirstName(void)
    	{
    		return firstName;
    	}
    
    	string getLastName(void)
    	{
    		return lastName;
        }
    };
    
    class Employee : public Person
    {
    protected:
    	float salary;
    
    public:
    	Employee()
        {
    		cout << "In the Employee constructor (a derived class)" << endl;
    		cout << "Enter a salary (no commas): ";
    		cin >> salary;
        }
    	
    	float getSalary(void)
        {
            return salary;
    	}
    };
    
    
    int main (void)
    {
    	Employee Number_one; // calls constructor of Person, then constructor of Employee
    	Employee Number_two; // calls constructor of Person, then constructor of Employee
    	
    	cout << endl << endl << endl;
    	
    	cout << "Retrieving Number_one's first name and last name from class Person:\n";
    	cout << "   "<< Number_one.getFirstName() << " " << Number_one.getLastName() << endl;
    	cout << "\nRetrieving Number_one's salary from class Employee:\n";
        cout << "   " << Number_one.getSalary() << endl;
    
    	cout << endl << endl << endl;
    
    	cout << "Retrieving Number_two's first name and last name from class Person:\n";
    	cout << "   " << Number_two.getFirstName() << " " << Number_two.getLastName() << endl;
    	cout << "\nRetrieving Number_two's salary from class Employee:\n";
        cout << "   " << Number_two.getSalary() << endl;
    
    	system("PAUSE");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    (Derived class employee)’s header takes an argument to (base class person) which gives it access to (base class person)’s properties.
    "Header" is not meaningful, and "properties" is incomplete as well as unconventional terminology. How about "Employee is a more specialized form of Person"?

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    A few criticisms of your code.

    Code:
    string getFirstName(void)
    This isn't C. In C++, if you have a function that takes no arguments, it's just string getFirstName() without the void.

    Also, try to learn const-correctness early on. For instance, your getFirstName-method is a simple getter. You could make the method const, promising that invoking that method on an instance of your Person class will not change any of the data members of the class (which is always a sensible promise for getter methods).

    Code:
    string getFirstName() const // <-- note the const
    Lastly, it is generally not a good idea to make data members protected. It's better to keep them private and provide protected accessor methods via which derived classes can modify those data members if needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Inheritance
    By dhuan in forum C# Programming
    Replies: 8
    Last Post: 10-27-2010, 04:56 PM
  2. Class Inheritance
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2008, 04:37 AM
  3. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  4. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 04:31 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM