#include
#include
#include
#include
using namespace std;
class B1
{
public:
B1(int a){
cout<<"B1"<
//基于VS2017
//未加虚基类的但可以实现完整输出的程序
#include "pch.h"
#include
#include
#include
using namespace std;
class Student
{
public:
Student(int, string, string, string, string); //声明构造函数
void display();
protected:
int Regnum;
string name;
string gender;
string school;
string Admission_Date;
};
//Student类成员函数的实现
Student::Student(int n, string nam, string gen,string sch,string time)//定义构造函数
{
Regnum = n;
name = nam;
gender = gen;
school = sch;
Admission_Date = time;
}
void Student::display()//定义输出函数
{
cout << "在校生Regnum:" << Regnum << "\nname:" << name << "\ngender:" << gender << "\nschool:"<< school << "\nAdmission_Date:"<< Admission_Date<<"\n\n";
}
//声明公用派生类Graduate
class Graduate :public Student
{
public:
Graduate(int, string, string, string, string, string, string);//声明构造函数
void display();//声明输出函数
private://派生类新增成员
string Graduate_Date;
string Graduate_degree;
};
// Graduate类成员函数的实现
void Graduate::display()//定义输出函数
{
cout << "毕业生Regnum:" << Regnum << "\nname:" << name << "\ngender:" << gender << "\nschool:" << school << "\nAdmission_Date:" << Admission_Date << "\nGraduate_Date:" << Graduate_Date << "\nGraduate_degree:"<< Graduate_degree<<"\n\n";
}
Graduate::Graduate(int n, string nam, string gen, string sch, string time, string Gratime, string Degree) : Student(n, nam, gen, sch, time), Graduate_Date(Gratime), Graduate_degree(Degree)
{
//Graduate_Date = Gratime;
//Graduate_degree = Degree;
}
int main()
{
Student stud1(1170230039, "xu", "male","浙江科技学院","2017.9.1");//定义Student类对象stud1
Graduate grad1(115000001, "zhou","female" ,"浙江科技学院","2015.9.1","2019.6","研究生" );//定义Graduate类对象grad1
stud1.display();
grad1.display();
system("pause");
return 0;
}
//未使用虚基类也可以输出的程序
//缺点是:如果一个继承的层级很深,有多重继承,那就需要定义多个指针,使得程序冗杂,所以虚基类就很有用了
//基于VS2017
#include "pch.h"
#include
#include
#include
using namespace std;
class Student
{
public:
Student(int, string, string, string, string); //声明构造函数
void display();
protected:
int Regnum;
string name;
string gender;
string school;
string Admission_Date;
};
//Student类成员函数的实现
Student::Student(int n, string nam, string gen,string sch,string time)//定义构造函数
{
Regnum = n;
name = nam;
gender = gen;
school = sch;
Admission_Date = time;
}
void Student::display()//定义输出函数
{
cout << "在校生Regnum:" << Regnum << "\nname:" << name << "\ngender:" << gender << "\nschool:"<< school << "\nAdmission_Date:"<< Admission_Date<<"\n\n";
}
//声明公用派生类Graduate
class Graduate :public Student
{
public:
Graduate(int, string, string, string, string, string, string);//声明构造函数
void display();//声明输出函数
private://派生类新增成员
string Graduate_Date;
string Graduate_degree;
};
// Graduate类成员函数的实现
void Graduate::display()//定义输出函数
{
cout << "毕业生Regnum:" << Regnum << "\nname:" << name << "\ngender:" << gender << "\nschool:" << school << "\nAdmission_Date:" << Admission_Date << "\nGraduate_Date:" << Graduate_Date << "\nGraduate_degree:"<< Graduate_degree<<"\n\n";
}
Graduate::Graduate(int n, string nam, string gen, string sch, string time, string Gratime, string Degree) : Student(n, nam, gen, sch, time), Graduate_Date(Gratime), Graduate_degree(Degree)
{
//Graduate_Date = Gratime;
//Graduate_degree = Degree;
}
int main()
{
Student stud1(1170230039, "xu", "male","浙江科技学院","2017.9.1");//定义Student类对象stud1
Graduate grad1(115000001, "zhou","female" ,"浙江科技学院","2015.9.1","2019.6","研究生" );//定义Graduate类对象grad1
//第二种方式访问:但要注意要使用虚函数方法才能显示派生类数据
Student *pt = &stud1;//定义指向基类对象的指针变量pt
Graduate *p = &grad1;//定义指向派生类对象的指针变量pt
pt->display();
p->display();
system("pause");
return 0;
}
//使用虚函数来简化
//基于VS2017
#include "pch.h"
#include
#include
#include
using namespace std;
class Student
{
public:
Student(int, string, string, string, string); //声明构造函数
virtual void display();
protected:
int Regnum;
string name;
string gender;
string school;
string Admission_Date;
};
//Student类成员函数的实现
Student::Student(int n, string nam, string gen,string sch,string time)//定义构造函数
{
Regnum = n;
name = nam;
gender = gen;
school = sch;
Admission_Date = time;
}
void Student::display()//定义输出函数
{
cout << "在校生Regnum:" << Regnum << "\nname:" << name << "\ngender:" << gender << "\nschool:"<< school << "\nAdmission_Date:"<< Admission_Date<<"\n\n";
}
//声明公用派生类Graduate
class Graduate :public Student
{
public:
Graduate(int, string, string, string, string, string, string);//声明构造函数
void display();//声明输出函数
private://派生类新增成员
string Graduate_Date;
string Graduate_degree;
};
// Graduate类成员函数的实现
void Graduate::display()//定义输出函数
{
cout << "毕业生Regnum:" << Regnum << "\nname:" << name << "\ngender:" << gender << "\nschool:" << school << "\nAdmission_Date:" << Admission_Date << "\nGraduate_Date:" << Graduate_Date << "\nGraduate_degree:"<< Graduate_degree<<"\n\n";
}
Graduate::Graduate(int n, string nam, string gen, string sch, string time, string Gratime, string Degree) : Student(n, nam, gen, sch, time), Graduate_Date(Gratime), Graduate_degree(Degree)
{
//Graduate_Date = Gratime;
//Graduate_degree = Degree;
}
int main()
{
Student stud1(1170230039, "xu", "male","浙江科技学院","2017.9.1");//定义Student类对象stud1
Graduate grad1(115000001, "zhou","female" ,"浙江科技学院","2015.9.1","2019.6","研究生" );//定义Graduate类对象grad1
//第二种方式访问:但要注意在基类方法中要使用虚函数方法才能显示派生类数据,即最左边加virtual
Student *pt = &stud1;//定义指向基类对象的指针变量pt——>基类指针就可实现所有的显示
pt->display();
pt = &grad1;//指向派生类的首地址
pt->display();
system("pause");
return 0;
}
//基于VS2017
// Test6_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include
#include
#include
using namespace std;
//基类
class Animal
{
protected:
string gender;
public :
Animal(){}
Animal(string);
virtual void soar()=0;
void eat();
};
Animal::Animal(string gen)
{
gender = gen;
}
void Animal::soar()
{}
void Animal::eat()
{}
//子类老虎
class Tiger :public Animal
{
public:
Tiger(string);
virtual void soar();
void eat();
};
Tiger::Tiger(string gender)
{
cout << "tiger is " << gender << endl;
}
void Tiger::soar()
{
cout << "tiger's soar is 嗷嗷嗷嗷……" << endl;
}
void Tiger::eat()
{
cout << "tiger eats meat mainly" << endl;
}
//子类羊
class Sheep :public Animal
{
public:
Sheep(string);
virtual void soar();
void eat();
};
Sheep::Sheep(string gender)
{
cout << "sheep is " << gender << endl;
}
void Sheep::soar()
{
cout << "sheep's soar is 咩咩咩咩……" << endl;
}
void Sheep::eat()
{
cout << "sheep eats grass mainly" << endl;
}
//主函数
int main()
{
/* 程序说明:
* 因无法做到将两个对象存入一个数组里,但能通过基类指针访问虚函数
* 又因两个函数一个是虚函数,一个不是, 所以无法都用基类指针访问
* 故只能实现一个用基类指针访问,一个用子类指针访问
*/
vector animal;//使用vector基类指针实现
vector ::const_iterator iter;
Tiger tiger[2] = {Tiger( "male"),Tiger("female")};
Sheep sheep[2] = {Sheep("female"),Sheep("male")};
//将地址给基类指针实现访问
animal.push_back(&tiger[0]);
animal.push_back(&sheep[1]);
animal.push_back(&tiger[1]);
animal.push_back(&sheep[0]);
Tiger *ptr1 = &tiger[0];
Sheep *ptr2 = &sheep[1];
Tiger *ptr3 = &tiger[1];
Sheep *ptr4 = &sheep[0];
int i = 0;
for (iter = animal.begin(); iter != animal.end(); iter++)
{
i++;
(*iter)->soar();//访问虚函数soar()
//访问普通函数eat()
switch (i)
{
case 1:
ptr1->eat();
break;
case 2:
ptr2->eat();
break;
case 3:
ptr3->eat();
break;
case 4:
ptr4->eat();
break;
}
}
system("pause");
return 0;
}