Home > other >  c : how can I get access to private private attributes in base class from the subclass
c : how can I get access to private private attributes in base class from the subclass

Time:10-24

I want to create a base class named Form and a subclass named ShrubberyCreationForm the problem is I have to set values to the base class using a subclass.

I found a solution to solve it in the constructor but I can't find a way for it for the copy constructor and assignment operator.

base class:

class Form
{
    private:
        std::string             _Name;
        bool                    _isSigned;
        unsigned int            _reqGradeToSign;
        unsigned int            _reqGradeToExecute;
    public:
        Form();
        Form( std::string Name, unsigned int reqGradeToSign, unsigned int reqGradeToExecute );
        Form( const Form & src );
        ~Form();
        class GradeTooLowException: public std::exception
        {
            virtual const char * what() const throw();
        };
        class GradeTooHighException: public std::exception
        {
            virtual const char * what() const throw();
        };

        Form &                  operator = ( const Form & rhs );
        std::string             getName();
        bool                    getisSigned();
        unsigned int            getReqGradeToSign();
        unsigned int            getReqGradeToExecute();
        void                    beSigned( Bureaucrat & brc );
};

subclass

class ShrubberyCreationForm: public Form
{
    public:
        ShrubberyCreationForm();
        ShrubberyCreationForm( std::string Name );
        ShrubberyCreationForm( const ShrubberyCreationForm & src );
        ~ShrubberyCreationForm();
        ShrubberyCreationForm & operator = ( const ShrubberyCreationForm & rhs );
};

the subclass CPP:

#include "ShrubberyCreationForm.hpp"

ShrubberyCreationForm::ShrubberyCreationForm(): Form() {}

ShrubberyCreationForm::ShrubberyCreationForm( std::string Name ): Form(Name, 145, 137)
{

}

ShrubberyCreationForm::~ShrubberyCreationForm()
{
}

// The problem here I don't know how to assign the values to the new object
ShrubberyCreationForm::ShrubberyCreationForm( const ShrubberyCreationForm & src )
{
}


ShrubberyCreationForm & ShrubberyCreationForm::operator=( const ShrubberyCreationForm & rhs )
{
    
}

CodePudding user response:

The usual way to do this in C is to define a copy constructor in your superclass, that's responsible for copying/assigning to itself. Then, your subclass's copy constructor and assignment operator invoke it to handle the superclass. So, for example, the copy constructor would look like:

ShrubberyCreationForm::ShrubberyCreationForm( const ShrubberyCreationForm & src )
  : Form{src}
{
}

Looks like you already defined the copy constructor and the assignment operator in the superclass, so this is the only missing piece. For the assignment operator this should be:

Form::operator=(rhs);
  •  Tags:  
  • c
  • Related