I have abstract class Father and inherited class Son.
Father has attributes name and properties.
Son has inherited name, properties and own school attribut.

Father.h:

#include <iostream>
#include <string>
#include <vector>
#include "Father.h"

class Father {
protected:
    std::string m_name;
    std::vector<std::string> m_properties;

public:
    virtual int calculateSalary() = 0;
};

Father.cpp:

#include "Father.h"

Son.h:

class Son: public Father{

    std::string m_school;

    //std::string m_name;  //No need to declare inherited classes again
    //std::vector<std::string> m_properties; //No need to declare inherited classes again


public:
    Son(std::string school, std::string name, std::vector<std::string> properties);
    //Son(std::string school):Father(std::string name, std::vector<std::string> properties);
    int calculateSalary();
};

Son.cpp:

#include "Son.h"

Son::Son(std::string school, std::string name, std::vector<std::string> properties){
    m_school = school;
    m_name = name;
    m_properties = properties;
}

int Son::calculateSalary() {
    // Implement the calculateSalary function for Son
    return 0; // Placeholder return value
}

This is code without any error. I would like to ask about constructor in Son.cpp. I have this code here in this time:

Son::Son(std::string school, std::string name, std::vector<std::string> properties){
    m_school = school;
    m_name = name;
    m_properties = properties;
}

but I know second code type which would looks like this:

Son::Son(std::string school, std::string name, std::vector<std::string> properties)
        : Father(name, properties), m_school(school) {}

in this example... I would like to ask when I will use the code which I have in Son.cpp in this time and when I would use the second code type which I mentioned?
EDIT: I am able to use second type of code in case class Father.cpp is implemented:

Father::Father(std::string name, std::vector<std::string> properties)

but I talked about abstract class (which not have implementation). So I am not able to use second type of code. So is the first one correct? Thank you for reply

0

There are 0 answers