0% found this document useful (0 votes)
339 views

Lab Report 2 Inheritance

This document contains the lab report for an object-oriented programming assignment on inheritance. The objective was to understand single, multiple, public, protected and private inheritance and constructor definitions in class hierarchies. The lab involved creating an AdvancedElevator class that inherits from an Elevator class and adds the ability to order stop floors based on passenger requests. The code provided implements these classes with methods like setupButton(), setFloorNumber() and main() to test the elevator functionality.

Uploaded by

Kagenza Claudien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
339 views

Lab Report 2 Inheritance

This document contains the lab report for an object-oriented programming assignment on inheritance. The objective was to understand single, multiple, public, protected and private inheritance and constructor definitions in class hierarchies. The lab involved creating an AdvancedElevator class that inherits from an Elevator class and adds the ability to order stop floors based on passenger requests. The code provided implements these classes with methods like setupButton(), setFloorNumber() and main() to test the elevator functionality.

Uploaded by

Kagenza Claudien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

School of Information Science and Technology

Lab Report of Object-Oriented Programming A


Lab 2: Inheritance Credit hour: 3
Student Name: IHIMBAZWE KAGENZA JEAN CLAUDIEN
Student ID: 2018529627020
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄

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>

using namespace std;

exele Ele;

CDate today;

void print()

{
School of Information Science and Technology
for(int i=0;i<15;i++)

cout<<"-";

cout<<endl;

cout<<"|"<<" "<<"1.up"<<" "<<"|"<<endl;

cout<<"|"<<" "<<"2.stop"<<" "<<"|"<<endl;

cout<<"|"<<" "<<"3.end"<<" "<<"|"<<endl;

for(int i=0;i<15;i++)

cout<<"-";

cout<<endl;

cout<<"The elevator is now

at"<<Ele.getCurrentFloor()<<"floor"<<endl;

int main()

cout<<today.format("DDD")<<endl;

while(1)

print();

int op;

int x;

cout<<"Enter your choice"<<endl;


School of Information Science and Technology
cin>>op;

switch (op)

case 1:Ele.setupButton();cout<<"Please enter how

many people"<<endl;cin>>x;Ele.tiqu(x);break;

case 2:Ele.setdownButton();cout<<"Please enter how

many people"<<endl;cin>>x;Ele.tiqu(x);break;

case 3:cout<<""<<endl;exit(0);break;

default :cout<<"Incorrect input data"<<endl;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;

void Elevator::setfloorNumber(int floornumber)

if (up)

int i;

for (i = cf+1; i < floornumber; 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;

for (i = cf-1; i > floornumber; 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));

void exele::tiqu(int num)

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));

cout<<"Please enter the start and end points of each passenger

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
{

for(int i=0; i<2*num; i++)

if(judge[i]!=judge[i-1])

setfloorNumber(judge[i]);

up=false;

if(down)

for(int i=2*num-1; i>=0; i--)

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>

using namespace std;

class Elevator

public:

Elevator();

void setupButton(); //Press the up button

void setdownButton(); //Press the down button

void setfloorNumber(int x); //Start the operation

according to the floor elevator to be entered, and display the passing

floors layer by layer.

int getCurrentFloor(); //Get the current floor to start

running according to the floor elevator to enter, and display the

passing floors layer by layer.

protected:

int f;

bool up;

bool down;

int cf;

};
School of Information Science and Technology
class exele:public Elevator

private:

int judge[1000];

public:

void tiqu(int num);

exele();

};

DATE.CPP

#include"date.h"

CDate::CDate(int dd, int mm, int yy):df_s("ddd"),df_l("DDD")

//initialization

if((mm>=1 && mm<=12) && (dd>=1 && dd<=31))

m = mm; d = dd; y = yy;

}else {

m = 0; d = 0; y = 0;
School of Information Science and Technology

cout<<"the date created is wrong"<<endl;

CDate::CDate():df_s("ddd"),df_l("DDD") //initialization

time_t now;

time(&now);

struct tm *t_now;

t_now = localtime(&now);

y = t_now -> tm_year + 1900;

m = t_now -> tm_mon + 1;

d = t_now -> tm_mday;


School of Information Science and Technology
}

void CDate::add_year(int n) //Plus n years

y += n;

void CDate::add_month(int n) //Plus n years

m += n;

void CDate::add_day(int n) //Plus n years

d += n;

string CDate::format(string df)

char c_df[20];

if(df == df_s)

sprintf(c_df, "%d-%d-%d", y, m, d);

return string(c_df);

}
School of Information Science and Technology
if(df == df_l)

sprintf(c_df, "%d?%d?%d?", y, m, d);

return string(c_df);

return string("");

int CDate::get_day() const

return d;

int CDate::get_month() const

return m;

int CDate::get_year() const

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>

using namespace std;

class CDate{

int d,m,y;

const string df_s;

const string df_l;

public:

CDate(int dd,int mm=1,int yy=1999);

CDate();

void add_year(int n);

void add_month(int n);

void add_day(int n);

string format(string df);

int get_day() const;

int get_month() const;

int get_year() const;


School of Information Science and Technology
};

#endif

6 Output

7 Analysis and conclusions

Through this experiment


To master the principle of single inheritance and multiple inheritance;
To understand the difference among public, protected and private inheritance.
To master the definition of constructor in class hierarchy and the construction and
destruction order when creating object.
Realize the extension to class function by using inheritance mechanism and design
the appropriate member functions and constructors for the derived class.
School of Information Science and Technology

You might also like