Hey, I'm currently learning about classes and I am wondering how to implement a sub-class into a class.

I have a class atm called "Person" but I want to create a sub-class called administrators. So would it be:

Class person
{
   public:
            // class methods
            // class methods
   private:
           // object classifications 
          // object classifications
class Admin
{
        public:
                  // class methods
            // class methods
   private:
           // object classifications 
          // object classifications
};
};

Or would it be:

Class Person
{
   public:
            // class methods
            // class methods
   private:
           // object classifications 
          // object classifications
};
class Admin
{
        public: Person
   private:
           // object classifications 
          // object classifications
};

The other thing, how would I implement it? For example I have currently:

int main()
{
   Person p1;
   p1.setName("ddgdjgdgjdg");
}

But how would I set p1 to use the admin class?

Thanks for any help

Recommended Answers

All 3 Replies

it would be like

class Person
{
/*members*/
public:
        void setName();
/*and more*/
};

class Admin:public Person
{
/*more members*/
};


/*u can use them as*/
Admin adm;
adm.setName("Testname");

Thank you :) So in theory, this should work:
Classes:

class Person 
{ 
    public:
           Person(); 
           Person(int theID, char theName[], int theAge, char theAddress[], char thePurchases[], int theRegistrationDate, int theCredit);
           char* getName();
           char* getAddress();
           int getAge();
           int getID();
           char* getPurchases();
           int getRegistrationDate();
           int getStoreCredit();
           
           void setID(int theID);
           void setName(char theName[]);
           void setAddress(char theAddress[]);
           void setAge(int theAge);
           void setPurchases(char thePurchases[]);
           void setRegistation(char theRegistrationDate);
           void setCredit(int theCredit);
           int checkAge();
           char addCustomer();
           char open_file();
           
    private:
            char customer_name[500];
            char customer_address[10000];
            char customer_purchase[500];
            int registration_date;
            int customer_id;
            int customer_credit;
            int age;
};
// This is the sub-class for Administrators
class Admin
{
   public: Person // accessing the public of "Person"
   	  
   	  Admin(); 
      Admin(double Thesalary, int Thehours);
      double getSalary();
      int getHours();
      
      void setSalary(double theSalary);
      void theHours(int theHours);
    
    private:
    
       double person_salary;
       int person_hours;
};

(without the functions btw)

int main()
{
    Person p1;
    Admin a1;
    char new_cust_name[255];
    char option;
    int salary;
    cout << "Enter customer name: ";
    cin >> new_cust_name;
   
    // register the values
    p1.setName(new_cust_name);
    
    cout << "Is this user an admin?";
    if(option == 'A')
    {
          cout << "Enter the salary: ";
          cin >> new_salary; 
          a1.setSalary(new_salary);
     }
return 0;
}

Thanks for any help!

imp: line #35-37 (in the 1st code block)

the syntax is not like that as u have written. Its like

class Admin:public Person
{
/*members*/
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.