C++学习10——父类子类的关系

//std::string类的全名,由于没有加using namespace std;所以要写全名
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;
protected:
    std::string m_name;
    std::string m_ssn;
};
Employee::Employee(const string& name,const string& ssn):m_name(name),m_ssn(ssn){
    //构造函数
}
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{
    out<<msg<<endl;
    print(out);//调用上一个print()函数,避免代码复制,尽量使用已有的代码写新的代码
}
//print()函数重载
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;
};
Manager::Manager(const string& name,const string& ssn,const string& title=""):Employee(name,ssn),m_title(title){   
}
inline void Manager::print(std::ostream& out) const{
    Employee::print(out);
    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");
    Manager bill("Bill Smith","666-55-1234","Important Person");
    string name=bill.get_name();//okay Manager inherits Employee
    //string title=bob.get_title();//Error--bob is an Employee!
    cout<<bill.title_name()<<'\n'<<endl;
    bill.print(cout);
    bob.print(cout);
    bob.print(cout,"Employee:");
    //bill.print(cout,"Employee:");//error hidden!
}
#include<iostream>
using namespace std;
class A{
public:
    A(ii):i(ii){cout<<"A::A()"<<endl;}
    ~A(){cout<<"A::A()"<<endl;}
    void print(){cout<<"A::print()"<<i<<endl;}
    void print(int i){cout<<i;print();}
    void set(int ii){i=ii;}
private:
    int i;
};
class B:public A{
public:
    B():A(15){
        cout<<"B::B()"<<endl;       
    }//父类的构造函数调用必须在initialize中,通过initialize给父类送参数,如果不传参数给父类,父类会调用默认构造函数。如果没有这句构造函数,会报错。初始化的顺序:声明的顺序,而非写在initialize中的顺序(父类首先被构造,之后成员变量按照写在列表里的顺序构造初始化)
    ~B(){cout<<"B::~B()"<<endl;}
    //析构的顺序与构造相反
    void print(){cout<<"B::print()"<<endl;}
    void f(){
        set(20);
        print();
    }
};
int main()
{
    B b;//B中包含A的所有东西,b初始化时,A的构造函数会被调用
    b.set(10);
    b.print();
    b.f();
    b.print(200);//报错,子类将父类中的函数隐藏
    return 0;
}
//先构造A,再构造B
//先析构B,再析构A
  1. 所有的成员变量都必须在initialize中

  2. 父类的初始化(构造函数调用)必须在initialize中

如果父类中有重载函数,在子类中有与其中之一相同的函数,则父类中的函数隐藏。

子类中print()函数与父类中print()函数无关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,父类子类之间的继承关系可以通过类的定义和构造函数来实现。子类可以继承父类的成员变量和成员函数,并且可以添加自己特有的成员变量和成员函数。 下面是一个示例,展示了一个父类Animal和一个子类Dog的定义和继承关系: ```cpp class Animal { protected: int age; string name; public: Animal(int age, string name) { this->age = age; this->name = name; } void eat() { cout << "The animal is eating." << endl; } }; class Dog : public Animal { public: Dog(int age, string name) : Animal(age, name) { } void bark() { cout << "The dog is barking." << endl; } }; ``` 在这个例子中,Animal是父类,Dog是子类子类Dog继承了父类Animal的age和name成员变量,并且可以调用父类的成员函数eat。子类Dog还添加了自己特有的成员函数bark。 通过这种继承关系,我们可以创建一个Dog对象,并调用它的成员函数和继承自父类的成员函数,如下所示: ```cpp Dog dog(3, "Buddy"); dog.eat(); // 输出:The animal is eating. dog.bark(); // 输出:The dog is barking. ``` 在这个例子中,我们创建了一个名为dog的Dog对象,它的年龄是3,名字是"Buddy"。我们可以调用dog的成员函数eat和bark,分别输出相应的信息。 总结起来,父类子类之间的继承关系可以通过类的定义和构造函数来实现,子类可以继承父类的成员变量和成员函数,并且可以添加自己特有的成员变量和成员函数。 #### 引用[.reference_title] - *1* [C++父类子类](https://blog.csdn.net/weixin_30386713/article/details/95402634)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python父类以及子类](https://blog.csdn.net/cyy1104/article/details/126947794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值