#include <iostream>
using namespace std;
class Human{
private:
string name;
int age;
public:
Human(){cout<<"human的无参构造函数"<<endl;}
Human(string n,int a)
:name(n),age(a)
{
cout<<"human的有参构造函数"<<endl;
}
~Human(){cout<<"human的析构函数"<<endl;}
void show()
{
cout<<"human的show()"<<endl;
cout<<"human::"<<"姓名:"<<name<<" 年龄:"<<age<<endl;
}
void setName(string n)
{
name=n;
}
};
//学生类
class Student:virtual public Human
{
private:
double score;
public:
Student(){cout<<"student的无参构造函数"<<endl;}
Student(string n,int a,double s)
:Human(n,a),score(s){cout<<"student的有参构造函数"<<endl;}
~Student(){cout<<"student的析构函数"<<endl;}
void show()
{
cout<<"student的show()"<<endl;
Human::show();
cout<<"成绩:"<<score<<endl;
}
void show(string s)
{
cout<<"student的show()"<<endl;
if(s=="score")
{
cout<<"成绩:"<<score<<endl;
return ;
}
Human::show();
cout<<"成绩:"<<score<<endl;
}
void setScore(double s)
{
score=s;
}
};
//党员类
class Party:virtual public Human{
private:
string activity; //党组织活动
string party;
public:
Party(){cout<<"party的无参构造函数"<<endl;}
Party(string n,int a,string act,string p)
:Human(n,a),activity(act),party(p){cout<<"party的有参构造函数"<<endl;}
~Party(){cout<<"party的析构函数"<<endl;}
void show()
{
cout<<"part的show()"<<endl;
Human::show();
cout<<"党组织活动:"<<activity<<" 党派:"<<party<<endl;
}
void show(string s)
{
cout<<"part的show()"<<endl;
if(s == "party")
{
cout<<"党派:"<<party<<endl;
return ;
}
Human::show();
cout<<"党组织活动:"<<activity<<" 党派:"<<party<<endl;
}
};
//学生干部
class StudentCadre:public Student,public Party{
private:
string position;//职务
public:
StudentCadre(){cout<<"studentcadre的无参构造函数"<<endl;}
StudentCadre(string n,int a,double s,string act,string p,string pos):Human(n,a),Student(n,a,s),Party(n,a,act,p),position(pos){cout<<"studentcadre的有参构造函数"<<endl;}
~StudentCadre(){cout<<"studentcadre的析构函数"<<endl;}
void show()
{
cout<<"studentcadre的show()"<<endl;
Human::show();
Student::show("score");
Party::show("party");
cout<<"职务:"<<position<<endl;
}
};
int main()
{
StudentCadre sc("张三",18,90,"党员活动","共产党","班长");
cout<<" sizeof(sc) = "<< sizeof(sc)<<endl;
sc.show();
return 0;
}
对于函数模板和类模板来说,它们的实现机制主要依赖于编译器在编译时期生成特定类型的代码。这意味着对于每一种需要的类型,编译器都会生成一个模板的实例。例如,如果你有一个函数模板 max 并且用 int 和 double 类型调用了它,编译器将生成两个不同的函数版本,一个用于 int 类型,另一个用于 double 类型。
这种实例化是在编译时完成的,因此不会影响运行时性能。但是,它可能会增加编译时间和生成的代码量,因为每个实例都是独立的函数或类。
#include "iostream"
#include "cmath"
using namespace std;
class graphical{
protected:
float perm;
float area;
public:
float get_perm()
{
return perm;
}
float get_area()
{
return area ;
}
};
class rectangle:public graphical{
private:
float length;
float wide;
public:
rectangle(float l,float w)
{
length = l;
wide = w;
perm = 2*(length+wide);
area = length*wide;
}
};
class circle:public graphical{
private:
float radius;
public:
circle(float r)
{
radius = r;
perm = 2*3.14*radius;
area = 3.14*radius*radius;
}
};
//定义三角形类
class triangle:public graphical{
private:
float a;
float b;
float c;
public:
triangle(float a,float b,float c)
{
this->a = a;
this->b = b;
this->c = c;
perm = a+b+c;
area = sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a))/4;
}
};
//定义一个全局函数,出入任意一个图形,输出该图形的周长和面积
void func(graphical &g)
{
cout << "周长:" << g.get_perm() << endl;
cout << "面积:" << g.get_area() << endl;
}
int main()
{
rectangle r(3,4);
circle c(5);
triangle t(3,4,5);
func(r);
func(c);
func(t);
return 0;
}