实验三 继承与派生

实验名称

实验三 继承与派生

实验目的和要求

(1)掌握类的继承和派生概念;

(2)掌握派生类的定义与使用;

(3)掌握派生类的构造函数与析构函数的应用及调用顺序;

(4)理解赋值兼容原则的应用。

实验内容

1. 利用继承和派生建立3个类,分别为点类、圆类、圆柱类,点类派生得到圆类,圆类派生得到圆柱类。功能分别要求点类能输出点的坐标;圆类能输出圆的半径和面积;圆柱类能输出其高度、表面积和体积,请编写程序实现。(功能扩充,请对运算符“ 《”进行重载,实现一个点、圆、圆柱体对象的输出)

2. 声明一个车(Vehicle)基类,有RunStop等成员函数,由此派生出自行车(Bicycle)类、汽车(car)类,从自行车和汽车类派生出摩托车(Motorcycle)类,它们都有Run()、Stop()等成员函数。利用继承和派生解决问题。

(3)定义个人信息类Person,其数据成员有姓名、性别、出生年月。并以Person为基类定义一个学生的派生类Student,增加描述学生的信息:班级、学号、专业、英语成绩和数学成绩。再由基类Person定义一个职工的派生类Employee,增加描述职工的信息:部门、职务、工资。编写程序实现学生与职工信息的输入与输出。

主要仪器设备

台式或笔记本电脑

实验记录

1. 

#include <iostream>

#include <iomanip>

using namespace std;

const double Pi = 3.14159;

class Point{

friend ostream &operator<< (ostream &, const Point &);

  public:

       Point(int a, int b) {x = a; y = b;}// 带默认参数的构造函数

       int getX(){ return x ; }

       int getY(){ return y ; }

  protected:

       int x, y;    // Point类的数据成员

};

class Circle : public Point{

      friend ostream &operator<< (ostream &, const Circle &);// 友元函数

  public:

      Circle( double r, int a, int b ): Point( a, b ) { radius= r ; }// 构造函数

      double getRadius()

      { return  radius; }     //返回半径

      double area(){ return  Pi * radius * radius ; }// 返回面积

protected:    double radius;// 数据成员,半径

};

class Cylinder:public Circle{

       friend ostream & operator<<(ostream &, const Cylinder &);    // 友元函数

   public:

       Cylinder(double h=0.0, double r=0.0, int x=0, int y=0):Circle(r,x,y){ height = h; }      // 构造函数

       double getHeight()   { height = ( height >= 0 ? height : 0 ); return height;}    //返回高度值

       double area()  { return  2*Circle::area()+2*Pi*radius*height; } //返回面积

       double volume() { return  Circle::area()*height; }    //返回体积

   protected:

       double height;// 数据成员,高度

};

 

ostream &operator<<( ostream &output , const Point &p ) // 重载插入运算符,输出对象数据

{    output << "Center = " << "[" << p.x << "," << p.y << "]" ;

//     output << "Center = " << "[" << p.getX() << "," << p.getY() << "]" ;

     return output ;

 }

ostream & operator<< ( ostream &output, const Circle &c) // 输出圆心坐标和半径值

{  output << "Center = " << '[' << c.x << "," << c.y << "]" << "; Radius = " << c.radius ;

   return  output ;

}

 

 

 

// 输出数据成员圆心坐标、半径和高度值

ostream &operator<< ( ostream &output, const Cylinder &cy )

{ output << "Center = " << '[' << cy.x << "," << cy.y << "]" << "; Radius = " << cy.radius

                << "; Height = " << cy.height << endl ;

  return output;

}

 

int main()

{

    int a, x, y, z;

    cin >> a >> x >> y >> z;

    Point p (a, x ) ;//定义点对象并初始化

   cout << "The initial location of p is " << p << endl ;//重载<<

   Circle c (y, a, x) ;//定义圆对象并初始化

   cout<<"\nThe initial location and radius of c are\n"<<c<<"\nArea = "<<c.area()<<"\n" ;

   Cylinder cyl (z, y, a, x) ;//定义圆柱体对象并初始化

   //输出圆柱体各数据和表面积,体积

   cout << "\nThe initial location, radius and height of cyl are\n" << cyl

      << "Area = " << cyl.area() << "\nVolume = " << cyl.volume() << '\n';

   return 0;

}

2. 

#include<iostream>

using namespace std;

class vehicle

{

public:

    void run(){}

    void stop(){}

};

class bicycle:virtual public vehicle

{

public:

    bicycle(){}

    void run();

    void stop();

};

class car:virtual public vehicle

{

public:

    car(){}

    void run();

    void stop();

};

class motocycle:public bicycle,public car

{

public:

    motorcycle(){}

    void run();

    void stop();

};

void bicycle::run()

{

    cout<<"bicycle:run"<<endl;

}

void car::run()

{

    cout<<"car:run"<<endl;

}

void motocycle::run()

{

    cout<<"motocycle:run"<<endl;

}

void bicycle::stop()

{

    cout<<"bicycle:stop"<<endl;

}

void car::stop()

{

    cout<<"car:stop"<<endl;

}

void motocycle::stop()

{

    cout<<"motocycle:stop"<<endl;

}

int main()

{   bicycle a1;

    vehicle *p = &a1;

//    motocycle *m = &a1;//错误

    a1. run();

    a1. stop();

    p->run();//无结果输出

    p->stop();

//    m->run();

//    m->stop();

 

    car a2;

    p=&a2;

    a2. run();

    a2. stop();

    p->run();

    p->stop();

 

    motocycle a3;

    bicycle *b  = &a3;//有结果输出

    p=&a3;

    a3. run();

    a3. stop();

    p->run();

    p->stop();

    b->run();

    b->stop();

 

return 0;

}

3.

#include <iostream>
using namespace std;
class Person
{
public:
 string name, sex, birth;//姓名、性别、出生年月
   Person(string a, string b, string c):name(a), sex(b), birth(c){}

};
class Student:public Person
{
string cc, number, major, eg, mg;//班级、学号、专业、英语成绩和数学成绩
public:
Student(string a, string b, string c, string d, string e, string f, string g, string h):Person(a,b,c)//这个当时犯了和内嵌子对象一样的错误,,,
{
cc = d;
number = e;
major = f;
eg = g;
mg = h;
}
void show()
{
  cout << "STUDENT:" << endl << "name:" << name << " sex:" << sex << " birthday:" << birth << " class:" << cc << " number:" << number << " major:" << major << " English grade:" << eg << " math grade:" << mg << endl << endl; 
}
};


class Employee:public Person
{
public:
string department, appointation, wage;//部门、职务、工资    我想在这用protected  但目前还没调试,等调试了就改
Employee(string a, string b, string c, string d, string e, string f):Person(a,b,c)//这儿的形参,我一直出错。
{
department = d;
appointation = e;
wage = f;
}
void show()
{
  cout << "EMPLOYEE:" << endl << "name:" << name << " sex:" << sex << " birthday:" << birth << " department:" << department << " appointation:" << appointation << " wage:" << wage << endl << endl; 
}
};
int main()

{
cout << "请输入学生相关信息" << endl;
string a, b, c, d, e, f, g, h;

cin >> a >> b >> c >> d >> e >> f >> g >> h;
Student A(a, b, c, d, e, f, g, h);

cout << endl << endl << "输出学生相关信息:" << endl;

A.show();

cout << "请输入职员相关信息" << endl;
//string i, j, k, l, m, n;

cin >> a >> b >> c >> d >> e >> f;
Employee B(a, b, c, d, e, f);

cout << endl << endl << "输出职员相关信息:" << endl;

B.show();


 
return 0;
}

遇到的问题和解决方法

1. 第一题和第二题  个人感觉老师的代码就挺好的

2. 第二题vehicle基类指针指向派生类对象无输出,可能因为里面为空,用bicycle为基类指针指向派生类是  输出为自身同名函数结果。

心得体会

了解了继承和派生,但对指针的用法还不熟

猜你喜欢

转载自blog.csdn.net/klftespace/article/details/80798256
今日推荐