分类 C++ 下的文章

一、实验目的

  1. 理解继承的概念;
  2. 掌握虚函数的定义;
  3. 掌握虚函数实现多态的方法;

二、实验内容和要求

定义汽车类Auto(属性:轮子数wheel,颜色color,行为:前进move),由其派生出卡车类Truck(属性:装载量load,行为:开车门open),二者都包含虚函数speak()、构造函数和析构函数,声明一个Auto指针,分别指向Auto对象和Truck对象,调用speak()函数,观察运行结果。

三、实验代码

#include<iostream>
using namespace std;
class Auto
{
private:
    int wheel;
    const char *color;
public:
    Auto(int wheel,const char *color)
    {
        this->wheel=wheel;
        this->color=color;
    }
    ~Auto()
    {
        cout<<"析构函数:"<<wheel<<'\n';
    }
    void move()
    {
        cout<<"汽车前进\n"<<'\n';
    }
    virtual void speak()
    {
        cout << "汽车类虚函数" << '\n';
        cout << "轮子数:" << wheel << '\n';
        cout << "颜色:" << color << '\n';
        
    }
};
class Truck:public Auto
{
private:
    double load;         //装载量,单位t吨
public:
    Truck(int wheel,const char *color,double load):Auto(wheel,color)
    {
        this->load=load;
    }
    ~Truck()
    {
        cout<<"析构函数:"<<"Truck"<<'\n';
    }
    void open()
    {
        cout<<"汽车门打开\n"<<'\n';
    }
    void speak()
    {
        cout << "卡车类虚函数" << '\n';
        Auto::speak();
        cout<<"装载量:"<<load<<'\n';
        
    }
};
int main()
{
    Auto a1(4,"Black");
    Truck t1(6,"Blue",10);
    Auto *obj=&a1;
    obj->speak();
    a1.move();
    obj=&t1;
    obj->speak();
    t1.open();
    system ("pause");
    return 0;
}

#include<iostream>
using namespace std;
class Person
{
private:
    const char *name;
    int age;
    const char *sex;
public:
    Person(const char *name,int age,const char *sex)
    {
        this->name=name;
        this->age=age;
        this->sex=sex;
        cout<<"构造函数:"<<name<<"\n";
    }
    ~Person()
    {
        cout<<"析构函数:"<<name<<"\n";
    }
    void display()
    {
        cout<<"姓名:"<<name<<"\n";
        cout<<"年龄:"<<age<<"\n";
        cout<<"性别:"<<sex<<"\n";
    }
};
class Student:public Person
{
private:
    const char *Snumber;
    double score;
public:
    Student(const char *name,int age,const char *sex,const char *Snumber,double score):Person(name,age,sex)
    {
        this->Snumber=Snumber;
        this->score=score;
        cout<<"构造函数:"<<Snumber<<"\n";
    }
    ~Student()
    {
        cout<<"析构函数:"<<Snumber<<"\n";
    }
    void display()
    {
        Person::display();
        cout<<"学号:"<<Snumber<<"\n";
        cout<<"成绩:"<<score<<"\n";
    }
};
class Teacher:public Person
{
private:
    const char *Tnumber;
    double Tage;
public:
    Teacher(const char *name,int age,const char *sex,const char *Tnumber,double Tage):Person(name,age,sex)
    {
        this->Tnumber=Tnumber;
        this->Tage=Tage;
        cout<<"构造函数:"<<Tnumber<<"\n";
    }
    ~Teacher()
    {
        cout<<"析构函数:"<<Tnumber<<"\n";
    }
    void display()
    {
        Person::display();
        cout<<"工号:"<<Tnumber<<"\n";
        cout<<"教龄:"<<Tage<<"\n";
    }
};
void main()
{
    Teacher t1("Lina",26,"女","18120001",3);
    Student s1("Xiaoming",18,"男","10180100",98);
    t1.display();
    s1.display();
    system ("pause");
}

#include<iostream>
using namespace std;
enum color { White, Black, Red, Brown, Green };
class Automobile
{
private:
    char *model;
    color c;
    int price;
    double mileage;
    static int countC;
public:
    Automobile(const char *model, color c, int price, double mileage);
    ~Automobile();
    void Assign(Automobile & p);
    void display();
    static int getCount();
    friend bool Mileage(Automobile &p);
};
Automobile::Automobile(const char *model, color c, int price, double mileage)
{
    this->model = new char[strlen(model) + 1];
    strcpy_s(this->model, strlen(model) + 1, model);
    this->c = c;
    this->price = price;
    this->mileage = mileage;
    ++countC;
    cout << "构造函数:" << model << "\n";
}
Automobile::~Automobile()
{
    cout << "析构函数:" << model << "\n";
    delete model;
    --countC;
}
void Automobile::Assign(Automobile & p)
{
    this->model = new char[strlen(p.model) + 1];
    strcpy_s(this->model, strlen(p.model) + 1, p.model);
    this->c = p.c;
    this->price = p.price;
    this->mileage = p.mileage;
}
int Automobile::getCount()
{
    return countC;
}
void Automobile::display()
{
    const char *cl=0;
    switch (c)
    {
    case 0:cl = "White"; break;
    case 1:cl = "Black"; break;
    case 2:cl = "Red"; break;
    case 3:cl = "Brown"; break;
    case 4:cl = "Green"; break;
    }
    cout << "\t该汽车型号:" << model << "\n";
    cout << "\t颜色:" << cl << "\n";
    cout << "\t价格:" << price << "\n";
    cout << "\t里程:" << mileage << "\n";
}
bool Mileage(Automobile &p)
{
    if (p.mileage >= 1000)
        return true;
    else
        return false;
}
int Automobile::countC = 0;
void main()
{
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    Automobile c1[3] = { Automobile("A1",White,100000,371.5),
                                   Automobile("A3",Red,150000,951.4),
                                      Automobile("A5",Black,200000,1024.1) };
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    Automobile *c2 = &c1[1];
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    Automobile *c3 = new Automobile("A7", Green, 25, 747);
    cout << "当前汽车数量:" << Automobile::getCount() << "\n";
    c3->Assign(c1[2]);
    c3->display();
    cout <<"如果里程大于1000为1(真),否则为0(假):"<< Mileage(*c3) << "\n";
    delete c3;
    system("pause");
}

#include<iostream>
using namespace std;
class Rectangle 
{
private:
    double length;
    double width;
public:
    Rectangle(double l, double w)
    {
        this->length = l;
        this->width = w;
    }
    Rectangle(Rectangle & r)
    {
        cout << "拷贝构造函数:";
        length = r.length;
        width = r.width;
    }
    ~Rectangle() 
    {
        cout << "析构函数" << "\n";
    }
    void get();
    void input();
    double RArea();
    double RCircumference();
};
void Rectangle::input()
{
    double l, w;
    cout << "请分别输入矩形的长和宽:";
    cin >> l >> w;
    length = l;
    width = w;
}
void Rectangle::get()
{
    cout << "长为" << length << "\t宽为" << width << "的矩形\n";
}
double Rectangle::RArea()
{
    double area = 0;
    area = length * width;
    return area;
}
double Rectangle::RCircumference()
{
    double Circumference = 0;
    Circumference = 2 * (length + width);
    return Circumference;
}
void main()
{
    Rectangle r1(6.7, 8.6);
    Rectangle r2(12.13,13.14);
    r1.get();
    cout << "面积为:" << r1.RArea() << "\n";
    cout << "周长为:" << r1.RCircumference() << "\n";
    r2.input();
    r2.get();
    cout << "面积为:" << r2.RArea() << "\n";
    cout << "周长为:" << r2.RCircumference() << "\n";
    system("pause");
}

#include<iostream>
using namespace std;
class CDate
{
private:
    int Y, M, D;
public:
    CDate()
    {
        Y = 2018, M = 11, D = 21;
    }
    CDate(int y, int m, int d)
    {
        Y = y, M = m, D = d;
    }
    ~CDate()
    {
        cout << "析构函数\n";
    }
    void SetTime(int y, int m, int d);
    void print();
};

void CDate::SetTime(int y, int m, int d) //设置时间
{
    Y = y, M = m, D = d;
}
void CDate::print()//输出时间
{
    cout << Y << "年-" << M << "月-" << D << "日\n";
}
void main()
{
    CDate c1, c2(2000, 1, 1);
    c1.print();
    c2.SetTime(2018, 11, 11);
    c2.print();
    system("pause");
}