Lab Report 2 Inheritance
Lab Report 2 Inheritance
1 Objective
1.1 To master the principle of single inheritance and multiple inheritance;
1.2 To understand the difference among public, protected and private
inheritance.
1.3 To master the definition of constructor in class hierarchy and the
construction and destruction order when creating object.
2 Introduction to lab principle
Realize the extension to class function by using inheritance mechanism and
design the appropriate member functions and constructors for the derived
class.
3 Lab requirement
3.1 Software: C++ compiler under Windows or linux
3.2 Hardware: CPU(>Pentium III 450), main memory(>64MB), free
secondary memory(>200M), monitor and printer.
4 Lab content
Extend the function of lab 1. AdvancedElevator class is the derived class of
Elevator class and it can realize that when several person waiting for the same
elevator (up or down) in different floors, it can order the stop-floor according to
the requirement of the person.
Requirement:
4.1 For the convenience of realization, we assume that a group of person
waiting the elevator has the same direction, that is, up or down.
4.2 Testify the AdvancedElevator class in the main function. The method to
testify is that firstly the direction(up or down) for some specific time and
specific group of person should be determined, and then get the number
School of Information Science and Technology
of the person of this group and the current floor and destination floor for
each person in this group as input. During the running of the object
created from the AdvancedElevator class, if the direction of the elevator
is up(down), it can stop according to the current floor and destination
floor of the passenger from bottom to top(from top to bottom)
4.3 When test this program, please be careful of tackling the problem that
when several passengers in the same floor or several passengers’
destinations are the same.
Tip:
In order to describe the passenger, we can define a Person class that is used to
describe the current floor and destination floor of each person.
AdvancedElevator class is derived from Elevator class and it obtain the current
floor and destination floor from each person ready to take the elevator at some
period, and then order the floor and display the stop-floor according to the
order by using the member function setFloorNumber which derived from the
base class Elevator.
Thought Questions (Optional):
If passenger’s weight is taken into account, how to realize that the overload
information can be displayed appropriately at some specific stop-floor.
5 Code list
MAIN.CPP
#include"Elevator.h"
#include<windows.h>
exele Ele;
CDate today;
void print()
{
School of Information Science and Technology
for(int i=0;i<15;i++)
cout<<"-";
cout<<endl;
for(int i=0;i<15;i++)
cout<<"-";
cout<<endl;
at"<<Ele.getCurrentFloor()<<"floor"<<endl;
int main()
cout<<today.format("DDD")<<endl;
while(1)
print();
int op;
int x;
switch (op)
many people"<<endl;cin>>x;Ele.tiqu(x);break;
many people"<<endl;cin>>x;Ele.tiqu(x);break;
case 3:cout<<""<<endl;exit(0);break;
ELEVATOR.CPP
#include "Elevator.h"
#include<bits/stdc++.h>
#include <string.h>
Elevator::Elevator()
cf = 1;
down=up=false;
f = 10;
School of Information Science and Technology
}
void Elevator::setdownButton()
down = true;
void Elevator::setupButton()
up = true;
if (up)
int i;
cout << "--" << i << "--" << endl; cout << endl;
Sleep(1000);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
School of Information Science and Technology
FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "First" << i << "Layered up" << endl; cout << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
Sleep(1000);
cf = i;
if (down)
int i;
cout << "--" << i << "--" << endl; cout << endl;
Sleep(1000);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "First" << i << "Layered up" << endl; cout << endl;
School of Information Science and Technology
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
Sleep(1000);
cf = i;
int Elevator::getCurrentFloor()
return cf;
exele::exele()
memset(judge,0,sizeof(judge));
int upp[1000],downn[1000];
memset(judge,20,sizeof(judge));
School of Information Science and Technology
memset(upp,0,sizeof(upp));
memset(downn,0,sizeof(downn));
separately"<<endl;
int t=0;
for(int i=0;i<num;i++)
cin>>upp[i]>>downn[i];
if((up&&upp[i]>=downn[i])||upp[i]>10||downn[10]>10||(down&&u
pp[i]<=downn[i]))
cout<<"data error"<<endl;
return ;
judge[t]=upp[i];
judge[++t]=downn[i];
t++;
sort(judge,judge+1000);
if(up)
School of Information Science and Technology
{
if(judge[i]!=judge[i-1])
setfloorNumber(judge[i]);
up=false;
if(down)
if(judge[i]!=judge[i-1])
setfloorNumber(judge[i]);
down=false;
ELEVATOR.H
#define ELEVATOR_H
#include <iostream>
School of Information Science and Technology
#include <windows.h>
#include <stdlib.h>
class Elevator
public:
Elevator();
protected:
int f;
bool up;
bool down;
int cf;
};
School of Information Science and Technology
class exele:public Elevator
private:
int judge[1000];
public:
exele();
};
DATE.CPP
#include"date.h"
//initialization
}else {
m = 0; d = 0; y = 0;
School of Information Science and Technology
CDate::CDate():df_s("ddd"),df_l("DDD") //initialization
time_t now;
time(&now);
struct tm *t_now;
t_now = localtime(&now);
y += n;
m += n;
d += n;
char c_df[20];
if(df == df_s)
return string(c_df);
}
School of Information Science and Technology
if(df == df_l)
return string(c_df);
return string("");
return d;
return m;
return y;
DATE.H
#ifndef DATE_H
School of Information Science and Technology
#define DATE_H
#include <iostream>
#include<ctime>
#include<cstdlib>
#include<string>
#include<cstdio>
class CDate{
int d,m,y;
public:
CDate();
#endif
6 Output