#ifndef __OBJEDT_H__
#define __OBJEDT_H__
#include <string>
#include <iostream>
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
}
protected:
string m_name; //姓名
int m_age; //年龄
};
class Teacher : public Person
{
public:
Teacher(const string& name, int age, const string& title)
: Person(name, age), m_title(title)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
cout << "职称:" << m_title << endl;
}
private:
string m_title; //职称
};
class Student : public Person
{
public:
Student(const string& name, int age, int studyId)
: Person(name, age), m_studyId(studyId)
{
}
void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
cout << "学号:" << m_studyId << endl;
}
private:
int m_studyId; //学号
};
#endif //__OBJEDT_H__
void test()
{
Person* pPerson = new Person("张三", 22);
Teacher* pTeacher = new Teacher("李四", 35, "副教授");
Student* pStudent = new Student("王五", 18, 20151653);
pPerson->ShowInfo();
cout << endl;
pTeacher->ShowInfo();
cout << endl;
pStudent->ShowInfo();
cout << endl;
delete pPerson;
delete pTeacher;
delete pStudent;
}
void test()
{
Person* pPerson = new Person("张三", 22);
Person* pTeacher = new Teacher("李四", 35, "副教授");
Person* pStudent = new Student("王五", 18, 20151653);
pPerson->ShowInfo();
cout << endl;
pTeacher->ShowInfo();
cout << endl;
pStudent->ShowInfo();
cout << endl;
delete pPerson;
delete pTeacher;
delete pStudent;
}
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
virtual void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
}
protected:
string m_name; //姓名
int m_age; //年龄
};
class Person
{
public:
Person(const string& name, int age) : m_name(name), m_age(age)
{
}
virtual void ShowInfo()
{
cout << "姓名:" << m_name << endl;
cout << "年龄:" << m_age << endl;
}
string GetName(); //正确,普通函数如果不被使用,可以只有声明没有定义
virtual int GetAge(); //错误,虚函数必须要有定义,即使是一个空实现,因为编译器无法确定会使用哪个函数
protected:
string m_name; //姓名
int m_age; //年龄
};
class Person
{
public:
virtual void SetAge(int age = 0)
{
m_age = age;
}
//... 省略
};
class Teacher : public Person
{
public:
virtual void SetAge(int age = 1)
{
m_age = age;
}
//... 省略
};
class Student : public Person
{
public:
virtual void SetAge(int age = 2)
{
m_age = age;
}
//... 省略
};
void test()
{
Person* pPerson = new Person("张三", 22);
Teacher* pTeacher = new Teacher("李四", 35, "副教授");
Student* pStudent = new Student("王五", 18, 20151653);
pPerson->SetAge();
pTeacher->SetAge();
pStudent->SetAge();
pPerson->ShowInfo();
cout << endl;
pTeacher->ShowInfo();
cout << endl;
pStudent->ShowInfo();
cout << endl;
delete pPerson;
delete pTeacher;
delete pStudent;
}
void test()
{
Person* pPerson = new Person("张三", 22);
Person* pTeacher = new Teacher("李四", 35, "副教授");
Person* pStudent = new Student("王五", 18, 20151653);
pPerson->SetAge();
pTeacher->SetAge();
pStudent->SetAge();
pPerson->ShowInfo();
cout << endl;
pTeacher->ShowInfo();
cout << endl;
pStudent->ShowInfo();
cout << endl;
delete pPerson;
delete pTeacher;
delete pStudent;
}
class Person
{
public:
//... 省略
virtual void DoWork() = 0;
//... 省略
};
Person* pPerson = new Person("张三", 22);
class Teacher : public Person
{
public:
//... 省略
virtual void DoWork()
{
cout << "教书..." << endl;
}
//... 省略
};
class Student : public Person
{
public:
//... 省略
virtual void DoWork()
{
cout << "学习..." << endl;
}
//... 省略
};
void test()
{
Person* pTeacher = new Teacher("李四", 35, "副教授");
Person* pStudent = new Student("王五", 18, 20151653);
pTeacher->DoWork();
cout << endl;
pStudent->DoWork();
cout << endl;
delete pTeacher;
delete pStudent;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有