1.图形面积、周长计算器
#include <iostream>
#include <cmath>
using namespace std;
//图形
class graphical {
protected:
float perm;
float area;
public:
virtual void show()=0;
};
//矩形
class Rectangle:public graphical {
private:
float length;
float wide;
public:
//构造函数
Rectangle(float l ,float w):length(l),wide(w){
cout<<"矩形构造函数"<<endl;
perm=(length+wide)*2;
area=length*wide;
}
//矩形show虚函数
void show() override {
cout<<"perm= "<< perm<<endl;
cout<<"area= "<<area<<endl;
}
};
//圆形
class Circle:public graphical {
private:
float radius;
public:
//圆形构造函数
Circle(float r):radius(r){
cout<<"圆形构造函数"<<endl;
perm=3.14*2*radius;
area=3.14*radius*radius;
}
//圆形show虚函数
void show() override {
cout<<"perm= "<< perm<<endl;
cout<<"area= "<<area<<endl;
}
};
//三角形
class Triangle:public graphical {
private:
float a;
float b;
float c;
public:
//三角形构造函数
Triangle(float side1,float side2,float side3):a(side1),b(side2),c(side3){
cout<<"三角形构造函数"<<endl;
perm=a+b+c;
int p=(a+b+c)/2;
area = sqrt(p * (p - a) * (p - b) * (p - c));
}
//三角形show虚函数
void show() override {
cout<<"perm= "<< perm<<endl;
cout<<"area= "<<area<<endl;
}
};
//全局函数利用夫引用调用子类中的寻函数
void display(graphical &s){
s.show();
}
int main(){
cout<<"矩形面积"<<endl;
Rectangle r1(5.5, 3.2);
display(r1);
cout<<"圆形面积"<<endl;
Circle c1(2);
display(c1);
cout<<"三角形面积"<<endl;
Triangle t1(3, 4, 5);
display(t1);
}
2.作业完善
#include <iostream>
using namespace std;
class Human
{
public:
Human()
{
cout << "Human无参构造" << endl;
}
Human(string a, int b) : name(a), age(b)
{
cout << "Human有参构造" << endl;
}
~Human()
{
cout << "Human析构函数" << endl;
}
virtual void show()
{
cout << "Human::name " << name << endl;
cout << "Human::age " << age << endl;
cout << "Human show" << endl;
}
protected:
string name = "范飞龙";
int age = 23;
};
class Student :virtual public Human
{
public:
// 无参构造
Student() : Human()
{
cout << "Student无参构造" << endl;
}
// 有参构造
Student(string a, int b, int c) : Human(a, b), score(c)
{
cout << "Student有参构造" << endl;
}
// 析构函数
~Student()
{
cout << "Student析构函数" << endl;
}
void show()override
{
cout << "Human::name " << name << endl;
cout << "Human::age " << age << endl;
cout << "Student::score" <<score<<endl;
}
protected:
int score=100;
};
class Party :virtual public Human
{
public:
// 无参构造
Party() : Human()
{
cout << "Party无参构造" << endl;
}
// 有参构造
Party(string a, int b, string c, string d) : Human(a, b), action(c), organization(d)
{
cout << "Party有参构造" << endl;
}
// 析构函数
~Party()
{
cout << "Party析构函数" << endl;
}
void show()override
{
cout << "name " << name << endl;
cout << "age " << age << endl;
cout << "action" <<action<<endl;
cout << "organization" <<organization<<endl;
}
protected:
string action="打篮球";
string organization="xxx";
};
class cadre : public Student, public Party
{
public:
// 无参构造
cadre() : Student(), Party(), posts("")
{
cout << "cadre无参构造" << endl;
}
// 有参构造
cadre(string a, int b, int c, string d, string e, string f):Human(a,b), Student(a, b, c), Party(a, b, d, e), posts(f)
{
cout << "cadre有参构造" << endl;
}
// 析构函数
~cadre()
{
cout << "cadre析构函数" << endl;
}
void show()override
{
cout << "name " << name << endl;
cout << "age " << age << endl;
cout << "action" <<action<<endl;
cout << "organization" <<organization<<endl;
cout << "score" <<score<<endl;
cout << "posts" <<posts<<endl;
}
protected:
string posts="zzz";
};
// 子类继承父类的show()
// void func(Human &s)
// {
// s.show();
// }
// void func2(Student &s)
// {
// s.show();
// }
// void func3(Party &s)
// {
// s.show();
// }
void func(Human &s)
{
s.show();
}
int main(int argc, const char *argv[])
{
cadre c1;
func(c1);
// Human h1;
// std::cout<<"sizeof(h1)="<<sizeof(h1)<<std::endl;
// Student s1;
// std::cout<<"sizeof(s1)="<<sizeof(s1)<<std::endl;
// Party p1;
// std::cout<<"sizeof(p1)="<<sizeof(p1)<<std::endl;
// cadre c1;
// std::cout<<"sizeof(c1)="<<sizeof(c1)<<std::endl;
// 测试各实例对象中对父类的继承
// cout << "Student test" << endl;
// Student s1;
// func(s1);
// cout << "Party test" << endl;
// Party p1;
// func(p1);
// cout << "cadre test1" << endl;
// cadre c1;
// func2(c1);
// cout << "cadre test2" << endl;
// func3(c1);
}