class Employee
{
public:
Employee(const std::string& name, const std::string& ssn);
const std::string& get_name() const;
void print(std::ostream& out) const;
void print(std::ostream& out, const std::string& msg) const;
//The benefit of making member variables protected is that subclasses can call m_name,m_ssn directly.
// The downside is that you don't know what the subclasses will do with m_name and m_ssn.
//It is recommended that member variables be private.
protected:
std::string m_name;
std::string m_ssn;
};
//Constructor
Employee::Employee(const string& name, const string& ssn):m_name(name),m_ssn(ssn)
{
//initializer list sets up the values.
}
inline const std::string& Employee::get_name() const
{
return m_name;
}
inline void Employee::print(std::ostream& out) const
{
out << m_name << endl;
out << m_ssn << endl;
}
inline void Employee::print(std::ostream& out, const std::string& msg) const //Two print functions have the same name, different parameters, and overloaded functions
{
out << msg << endl;
print(out); // The compiler decides which function to call depending on the number and type of parameters.
//Called the first print function instead of copying it once. Make the best use of the code you wrote before. Avoid copying code.
}
class Manager:public Employee
{
public:
Manager(const std::string& name, const std::string& ssn, const std::string& title);
const std::string title_name() const;
const std::string& get_title() const;
void print(std::ostream& out) const;
private:
std::string m_title;
};
//Initialization List
Manager::Manager(const string& name, const string& ssn, const string& title=""):Employee(name,ssn),m_title(title)
{
}
inline void Manager::print(std::ostream& out)
{
Employee::print(out); //Avoid writing parsers when refactoring code rewriting functions.
out << m_title << endl;
}
inline const std::string& Manager::get_title() const
{
return m_title;
}
inline const std::string Manager::title_name() const
{
return string(m_title + ": " + m_name);
}
int main()
{
Employee bob("Bob Jones", "555-44-0000");
//Parent Employee, where the protected member variable m_name = "bob Jones" and m_ssn = "555-44-0000" of the bob object in Employee (member variables are objects and member functions are classes)
Manager bill("Bill Smith", "666-55-1234", "Important Person");
//Private member variable m_title = "Important Person" in object bill of subclass Manager, m_name = "Bill Smith", m_ssn = "555-44-0000".
string name = bill.get_name();
//The subclass calls the member function of the parent class, name = "Bill Smith".
//string title = bob.get_title(); //This line has errors and cannot be called like this, and the parent cannot call something of the subclass.
cout << bill.title_name() << '\n' << endl;
// "Important Person : Bill Smith
bill.print(cout); //"Bob Jones" "555-44-0000"
//"Important Person.
bob.print(cout); //"Bob Jones" "555-44-0000"
bob.print(cout, "Employee:"); //Employee: "Bob Jones" "555-44-0000"
//Bill.print (cout,'Employee:'); //Error in this line
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
A(int ii):i(ii)
{
cout << "A::A()" << endl;
}
`A()
{
cout << "A::`A()" << endl;
}
void print()
{
cout << "A::f()" << endl;
}
void set(int ii)
{
i = ii;
}
private:
int i;
};
class B:public A
{
public:
B():A(15) {} //Constructor for B
void f()
{
set(20);
print();
}
};
int main()
{
B b; //Program error, Reason B, required to call parameterless constructor of parent class A. Not in class A.
//Initialize the subclass, and the parent is part of the subclass, so the parent also needs to be initialized. The subclass does not know that the parent is initialized, so it needs to be initialized by the parent itself. At this time, A does not give a default constructor, so you need to tell how to call A's constructor.
//Solution: Add B's constructor B():A(15) first initializes A(15), then calls A's constructor, and then calls B's constructor (initialization list)
b.set(10);
b.print();
b.f();
return 0;
}
Order of initialization list
c++
class father
{
public:
father(int x, float y) { }
private:
int x;
float y;
};
class son: public father
{
public:
son:father(10, 10.0, 5); //In the declared order, the parent class is first, and the list of subclasses is initialized. x=10, y=10.0, m=5.
private:
int m;
};
//When destructing, clean up the subclasses before the parent classes.
Note: If you pass parameters to the parent class without an initialized list, the parent class's parameterless constructor will be called.
As long as C++ does this:
When there is a member function print(), print(int i) in the parent class
There is also a member function print() in the subclass
Subclass B, b.print() hides both the member functions print() and print(int i) of the parent class.