#include<iostream>
#include"Person.h"
#include"Teacher.h"
#include"Cadre.h"
#include"Teacher_Cadre.h"
using std::ostream;
using std::cin;
using std::string;
using std::cout;
using std::endl;
int main()
{
//cout << "2.1 对象名,指向对象的指针,对象的引用三种方式" << endl;
Person a("PingXuan", "福州市工业路5号");
Person* b = new Person("Walle", "福州市工业路10号");
Person& c = a;
a.change_addr("zhafdskl;ja");
a.display();
b->display();
c.display();
//cout << "2.2对象数组的方式" << endl;
Person arr[3] = { {"PingXuan","福州市工业路5号"},
{"Walle","福州市工业路10号"},{"Walle","福州市工业路10号"} };
for (int i = 0; i < 3; i++)
{
arr[i].display();
}
//cout << "2.3 定义指向Person::display()函数的指针" << endl;
void (Person:: * pf)()const = &Person::display;
for (int i = 0; i < 3; i++)
{
(arr[i].*pf)();
}
//cout << "2.4 定义Person 的常对象,访问display(),change(char* add)" << endl;
const Person const_obj = Person("a", "福州市工业路20号");
const_obj.display();
//cout << "2.5 定义一个inquiry普通函数,并将其声明为Person 的友元" << endl;
//cout << "2.6 增加一个新指针表示父亲" << endl;
char* father_ = new char[20];
cout << "输入字符串:" << endl;
cin >> father_;
Person* father = new Person("father", "福州市工业路20号");
delete father;
//cout << "2.7 Person1 的孪生双胞胎" << endl;
Person bother1 =Person ("bother1","福州市工业路100号",10);
Person bother2(bother1);
//cout << "2.8 四个人买东西的平均价格" << endl;
Person A(2);
Person B(5);
Person C(8);
Person D(16);
Person E(2 + 5 + 8 + 16);
cout << "花" << A.discount() << "元" << endl;
cout << "花" << B.discount() << "元" << endl;
cout << "花" << C.discount() << "元" << endl;
cout << "花" << D.discount() << "元" << endl;
cout << "总的买的平均单价:" << E.discount() / 4 << endl;
//cout << " 2.9 定义类模板,并在函数外定义 见template_new_class" << endl;
//cout << " 2.10 定义新数据成员 ID" << endl;
Person id1("350524198110022046");
Person id2("350524197810022046");
//cout << " 2.11 重载== < >运算符" << endl;
if (id1 == id2) {
cout << "id1 和id2 的年龄一样" << endl;
}
else if (id1 >id2)
{
cout << "id1 的年龄大" << endl;
}
else
{
cout << "id2的年龄大" << endl;
}
cout << "新增age数据成员 " << endl;
Person Pingxuan("Pingxuan", "地址:福州", 25);
int age = Pingxuan + 1;
//3.1派生Teacher类,Cadre类
//3.2在Teacher_Cadre类中增加子对象Person father
//3.3 测是基类 派生类的构造函数,析构函数顺序,见输出
cout<<Pingxuan.get_age()<<"岁"<<endl;
//3.4在引用姓名,年龄,地址,电话等数据成员没有指定作用域做如何?
//:编译器会报错,有歧义,编译器无法判断是哪个类的数据成员。
//3.5 将person类声明为虚基类
//3.7
Cadre Li("Cadre_Li", "福州工业园",18,'Y', "10000","351100");
Teacher wang("Teacher_wang","福州工业路",20,'Y',"10086","teacher");
Teacher_Cadre zhou(1000,"Zhou", "福建省",30,'x',"13666666");
//3.6 Person 的指针指向Teacher_Cadre对象
Person* p_person = &zhou;
p_person->display();//调用的是Teacher_Cadre类的display
//解释:因为派生类对象中含有与其基类对应的组成部分,所以我们能将基类的指针或引用绑定到派生类对象中的基类部分上
//3.7
//1将display 定义为虚函数,并重定义,见上
//2 Person 类定义为抽象类,//ps:抽象类不能构造出实体,会报错。
return 0;
}