快乐过年1

#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
class Worker :public Person
{
public:
protected:
private:
};
class Teacher :public Worker
{
public:
protected:
private:
};
int main()
{
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
}
#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
//    工人类
class Worker :public Person
{
public:
    Worker(string name = "", string sex = "", int salary = 4000) :Person(name, sex), salary(salary)
    {
        cout << "Worker(string,string,int)" << endl;
    }
    ~Worker()
    {
        cout << "~Worker()" << endl;
    }
    void work()
    {
        cout << this->name << "工人正在工作中..." << endl;
    }
protected:
    int salary;
private:
};
class Teacher :public Worker
{
public:
protected:
private:
};
int main()
{
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
}
#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
//    工人类
class Worker :public Person
{
public:
    Worker(string name = "", string sex = "", int salary = 4000) :Person(name, sex), salary(salary)
    {
        cout << "Worker(string,string,int)" << endl;
    }
    ~Worker()
    {
        cout << "~Worker()" << endl;
    }
    void work()
    {
        cout << this->name << "工人正在工作中..." << endl;
    }
protected:
    int salary;
private:
};
class Teacher :public Worker
{
public:
    Teacher(string name = "", string sex = "", int salary = 4000, string course = "C++")
    {
        cout << "Teacher(string,string,int,string)" << endl;
    }
    ~Teacher()
    {
        cout << "~Teacher()" << endl;
    }
    void work()
    {
        cout << this->name << "老师正在上课中......" << endl;
    }
protected:
private:
    string course;//    课程
};
int main()
{
    //    计算基类和派生类的所占内存大小
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
    Teacher worker("徐子宸", "男", 8000, "计算机");    //    创建派生类对象
    worker.show();
    worker.work();
    worker.Worker::work();
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
//    工人类
class Worker :public Person
{
public:
    Worker(string name = "", string sex = "", int salary = 4000) :Person(name, sex), salary(salary)
    {
        cout << "Worker(string,string,int)" << endl;
    }
    ~Worker()
    {
        cout << "~Worker()" << endl;
    }
    void work()
    {
        cout << this->name << "工人正在工作中..." << endl;
    }
protected:
    int salary;
private:
};
class Teacher :public Worker
{
public:
    Teacher(string name = "", string sex = "", int salary = 4000, string course = "C++")
        :Worker(name,sex,salary),course(course)
    {
        cout << "Teacher(string,string,int,string)" << endl;
    }
    ~Teacher()
    {
        cout << "~Teacher()" << endl;
    }
    void work()
    {
        cout << this->name << "老师正在上课中......" << endl;
    }
protected:
private:
    string course;//    课程
};
int main()
{
    //    计算基类和派生类的所占内存大小
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
    Teacher worker("徐子宸", "男", 8000, "计算机");    //    创建派生类对象
    worker.show();
    worker.work();
    worker.Worker::work();
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
//    工人类
class Worker :public Person
{
public:
    Worker(string name = "", string sex = "", int salary = 4000) :Person(name, sex), salary(salary)
    {
        cout << "Worker(string,string,int)" << endl;
    }
    ~Worker()
    {
        cout << "~Worker()" << endl;
    }
    void work()
    {
        cout << this->name << "工人正在工作中..." << endl;
    }
protected:
    int salary;
private:
};
class Teacher :protected Worker
{
public:
    Teacher(string name = "", string sex = "", int salary = 4000, string course = "C++")
        :Worker(name,sex,salary),course(course)
    {
        cout << "Teacher(string,string,int,string)" << endl;
    }
    ~Teacher()
    {
        cout << "~Teacher()" << endl;
    }
    void work()
    {
        cout << this->name << "老师正在上课中......" << endl;
    }
    void show()
    {
        cout << "测试1" << endl;
    }
protected:
private:
    string course;//    课程
};
int main()
{
    //    计算基类和派生类的所占内存大小
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
    Teacher worker("徐子宸", "男", 8000, "计算机");    //    创建派生类对象
    worker.show();
    worker.work();
    //worker.Worker::work();
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
//    工人类
class Worker :public Person
{
public:
    Worker(string name = "", string sex = "", int salary = 4000) :Person(name, sex), salary(salary)
    {
        cout << "Worker(string,string,int)" << endl;
    }
    ~Worker()
    {
        cout << "~Worker()" << endl;
    }
    void work()
    {
        cout << this->name << "工人正在工作中..." << endl;
    }
protected:
    int salary;
private:
};
class Teacher :protected Worker
{
public:
    Teacher(string name = "", string sex = "", int salary = 4000, string course = "C++")
        :Worker(name,sex,salary),course(course)
    {
        cout << "Teacher(string,string,int,string)" << endl;
    }
    ~Teacher()
    {
        cout << "~Teacher()" << endl;
    }
    void work()
    {
        cout << this->name << "老师正在上课中......" << endl;
    }
    void show()
    {
        cout << "测试1" << endl;
    }
protected:
private:
    string course;//    课程
};
int main()
{
    //    计算基类和派生类的所占内存大小
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
    Teacher worker("徐子宸", "男", 8000, "计算机");    //    创建派生类对象
    worker.show();
    worker.work();
    //worker.Worker::work();
    worker.Person::show();
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
//    父类
class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
    Person(string name, string sex)
    {
        cout << "Person(string,string)" << endl;
        this->name = name;
        this->sex = sex;
    }
    ~Person()
    {
        cout<<"~Person()"<<endl;
    }
    void show()
    {
        cout << "姓名为" << this->name << "的性别是" << this->sex << endl;
    }
    void eat()
    {
        cout << this->name << "正在吃饭......" << endl;
    }
    //    在自己的类内容可以直接访问,继承之后也是在子类可以访问的,但是在父类和子类外不行
protected:
    string name;
    string sex;
private:
};
//    工人类
class Worker :public Person
{
public:
    Worker(string name = "", string sex = "", int salary = 4000) :Person(name, sex), salary(salary)
    {
        cout << "Worker(string,string,int)" << endl;
    }
    ~Worker()
    {
        cout << "~Worker()" << endl;
    }
    void work()
    {
        cout << this->name << "工人正在工作中..." << endl;
    }
protected:
    int salary;
private:
};
class Teacher :public Worker
{
public:
    Teacher(string name = "", string sex = "", int salary = 4000, string course = "C++")
        :Worker(name,sex,salary),course(course)
    {
        cout << "Teacher(string,string,int,string)" << endl;
    }
    ~Teacher()
    {
        cout << "~Teacher()" << endl;
    }
    void work()
    {
        cout << this->name << "老师正在上课中......" << endl;
    }
    void show()
    {
        cout << "测试1" << endl;
    }
protected:
private:
    string course;//    课程
};
int main()
{
    //    计算基类和派生类的所占内存大小
    cout << sizeof(Person) << endl;
    cout << sizeof(Worker) << endl;
    cout << sizeof(Teacher) << endl;
    Teacher worker("徐子宸", "男", 8000, "计算机");    //    创建派生类对象
    worker.show();
    worker.work();
    //worker.Worker::work();
    worker.Person::show();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值