Why can I not update the default values of a derived class through a constructor? [closed]

I am trying to wrap my head around inheritance.

I’ve created a BasePerson class that has some protected elements, and those elements have default values. I’m trying to “update” those default values in a derived class with other values, and call them from outside the class in the main.cpp file.

If I’ve understood inheritance properly, I should be able call the derived class from the main file and have the “updated” elements displayed, but I’ve made an error somewhere as only the default values from the base class are shown.

I’ve gotten the code to the point where, if I want, I can update the values from the main.cpp file, but this isn’t what I’m trying to do.

#include <iostream>
#include <string>
#include "BasePerson.h"
using namespace std;

#include <string>
#include <iostream>
using namespace std;

class BasePerson
{
protected :
    string st_firstName;
    string st_lastName;

public :
    double d_wagePerHour;

    BasePerson()
    {
        st_firstName = "Default First Name";
        st_lastName = "Default Last Name";
        d_wagePerHour = 40.12;
    }

    BasePerson(string param_firstName, string param_lastName, double param_wage)
    {
        st_firstName = param_firstName;
        st_lastName = param_lastName;
        d_wagePerHour = param_wage;
    }

    //Print-all Function
    void fn_printAllInfo()
    {
        cout << "First Name: " << st_firstName << "n" << "Last Name: " << st_lastName << "n" << "Wage Per Hour: " << d_wagePerHour << endl;
    }
};

class Employee : public BasePerson
{
private :
    string st_career;

public :
    Employee()
    {
       st_career = "Default Career";
    }

    Employee(string param_firstName, string param_lastName, double param_wage, string param_career) : BasePerson(param_firstName, param_lastName, param_wage)
    {
        st_firstName = "Bob";
        st_lastName = "Vila";
    }
};

/*
    Notes:

        - Anything private will be inaccessible from the derived class and outside
        - Anything protected will be accessible from the derived class and inaccessible from the outside
        - Public is public.
*/

int main()
{
    BasePerson person1;
    person1.fn_printAllInfo();

    Employee emp1;
    emp1.fn_printAllInfo();

    return 0;
}

10

You aren’t currently setting the values in the Employee default constructor. If you want the default constructor to set all three values try this:

Employee()
{
    st_career = "Default Career";
    st_firstName = "Bob";
    st_lastName = "Vila";
    
}

Employee(string param_firstName, string param_lastName, double param_wage, string param_career) 
: BasePerson(param_firstName, param_lastName, param_wage)
{
    st_career = param_career;
}

You should also look into initializing all of the member variables in the member initializer list:

Employee() : BasePerson("Bob", "Vila", 40.12), st_career("Default Career") {}

First of all, I’d caution that I don’t see many of what I consider good uses for inheritance.Although this does make some actual use of it, I think the case for it is still fairly weak.

Good uses for inheritance nearly always involve the base class declaring at least one virtual function, and the derived class providing an implementation of that virtual function. I’ve added a (semi-)useful virtual function to demonstrate at least the general idea.

Second, it seems to me that you’re overloading constructors in a situation where it’s probably simpler and more straightforward to just provide default parameter values instead.

Third, when a ctor is going to initialize a value, it’s generally better for it to do so in an initializer list rather than doing an assignment in the body of the ctor. Sometimes (e.g., initializing a reference) this is strictly required; most other times it’s still preferable. I’d say at least 90% of the time that I write a ctor at all, its body is empty.

Fourth, by default data should be private. If you think you have a good reason for protected data, write it down and think about it first. Then if you’ve thought it through, and you’re 100% certain that the circumstances justify making it protected in this particular, highly unusual case, then you can go ahead and make it private.

Fifth, to print an object of some class, I’d provide an overload of operator<< to do that.

Sixth, formatting code for readability is quite important.

class BasePerson
{
private:
    string st_firstName;
    string st_lastName;
    double d_wagePerHour;
public:
    BasePerson(string param_firstName = "Default First Name", 
                      string param_lastName = "Default Last Name", 
                      double param_wage=40.12)
    : st_firstName(param_firstName),
      st_lastName(param_lastName),
      d_wagePerHour(param_wage)
    {
    }

    virtual std::ostream &print(std::ostream &os) const { 
        return os << "First Name: " << st_firstName << "n" 
                  << "Last Name: " << st_lastName << "n" 
                  << "Wage Per Hour: " << d_wagePerHour << "n";
    }
    friend std::ostream &operator<<(std::ostream &os, BasePerson const &b) { 
        return b.print(os);
    }
};

class Employee : public BasePerson {
    string st_career;
public:
    Employee(string const &firstName = "Bob",
             string const &lastName = "Villa",
             double wage = 40.12,
             string career = "Default Career")
       : BasePerson(firstName, lastName, wage),
         st_career(career)
    {}

    virtual std::ostream &print(std::ostream &os) const override { 
        BasePerson::print(os);
        return os << "Career: " << st_career;
    }
};

int main()
{
    BasePerson person1;
    std::cout << person1 << "n";

    Employee emp1;
    std::cout << emp1 << "n";

    return 0;
}

Live on Godbolt

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật