#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;
}