Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create a default constructor c++?

I’m currently learning how to code object oriented and I’ve created 2 classes (Applicant and OfferLetter) and trying to inherit those 2 classes into a third class (Software Engineer). I’ve gotten it structured for the most part however im getting an error code on my compiler that says "No default constructor exists for the class "OfferLetter". What can i do to fix this error?

#include <iostream>
using namespace std;

class Applicant{
    private:
        string Name;
        string JobTitle;
        string Degree;
        int YearsOfExpiremce;
        int Age;

    public: 
        //Name 
        void setName(string name){
            Name = name;
        }
        string getName(){
            return Name;
        }
        //JobTitle
        void setJobTitle(string jobtitle){
            JobTitle = jobtitle;
        }
        string getJobTitle(){
            return JobTitle;
        }
        //Degree
        void setDegree(string degree){
            Degree = degree;
        }
        string getDegree(){
            return Degree;
        }
        //Expirence
        void setExpirence(int expirence){
            YearsOfExpiremce = expirence;
        }
        int getExpirence(){
            return YearsOfExpiremce;
        }
        //Age
        void setAge(int age){
            Age = age;
        }
        int getAge(){
            return Age;
        }
            //Constructor
            Applicant(string name, string jobtitle, string degree, int expirence, int age){
                Name = name;
                JobTitle = jobtitle;
                Degree = degree;
                YearsOfExpiremce = expirence;
                Age = age;
            }
};

class OfferLetter{
    private:
        int BaseSalary;
        int SignOnBonus;
        int Stocks;
        int Incentives;

    public:
        //Base Salary
        void setSalary(int salary){
            BaseSalary = salary;
        }
        int getSalary(){
            return BaseSalary;
        }
        //Sign-On Bonus
        void setBonus(int bonus){
            SignOnBonus = bonus;
        }
        int getBonus(){
            return SignOnBonus;
        }
        //Stocks
        void setStocks(int stocks){
            Stocks = stocks;
        }
        int getStocks(){
            return Stocks;
        }
        //Incentives
        void setIncentives(int incentives){
            Incentives = incentives;
        }
        int getIncentives(){
            return Incentives;
        }
            //Constructor
            OfferLetter(int salary, int bonus, int stocks, int incentives){
                BaseSalary = salary;
                SignOnBonus = bonus;
                Stocks = stocks;
                Incentives = incentives;
            }
};

class SoftwareEngineer: public Applicant, public OfferLetter{
    public:
        string ProgrammingLanguage;
        string JobDescription;
       
        SoftwareEngineer(string name, string jobtitle, string degree, int expirence, int age, int salary, int bonus,
                        int stocks, int incentives, string language, string jd)

            :Applicant(name, jobtitle, degree, expirence, age)
            :OfferLetter(salary, bonus, stocks, incentives)
            
        {
            ProgrammingLanguage = language;
            JobDescription = jd;
        }

        void DisplaySEOfferLetter(){
            cout << "Congratulations on your job offer, Here is your total compensation. " << endl;
            cout << "Please respond to us in the next 5 days to confirm your Job Offer." << endl;
            cout << endl;
            cout << "Total Compensation " << endl;
            cout << "-------------------" << endl;
            cout << "Role: Software Engineer " << endl;
            cout << "Job Description: $" << endl;
            cout << "Base Salary  : $" << endl;
            cout << "Sign-On Bonus: $" << endl;
            cout << "Stocks       : $" << endl;
            cout << "Incentives   : $" << endl;
            cout << endl;
        }
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I think the problem is not the default constructor missing, but that you have a syntax error in your code:

SoftwareEngineer(string name, string jobtitle, string degree,
                 int expirence, int age, int salary, int bonus,
                 int stocks, int incentives, string language, string jd)
    :Applicant(name, jobtitle, degree, expirence, age)
    :OfferLetter(salary, bonus, stocks, incentives)
{
}

You attempt to create two constructor initializer lists, which is wrong. A constructor have a single comma-separated list:

SoftwareEngineer(string name, string jobtitle, string degree,
                 int expirence, int age, int salary, int bonus,
                 int stocks, int incentives, string language, string jd)
    : Applicant(name, jobtitle, degree, expirence, age),  // Note comma here
      OfferLetter(salary, bonus, stocks, incentives)      // No colon on this line
{
}

Note that you can initialize member variables using the initializer list as well, and it’s recommended to use that instead of assignment inside the constructor body:

SoftwareEngineer(string name, string jobtitle, string degree,
                 int expirence, int age, int salary, int bonus,
                 int stocks, int incentives, string language, string jd)
    : Applicant(name, jobtitle, degree, expirence, age),  // Note comma here
      OfferLetter(salary, bonus, stocks, incentives),      // No colon on this line
      ProgrammingLanguage(language),  // Added initialization
      JobDescription(jd)              // of member variables
{
    // Now this can be totally empty
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading