You are on page 1of 5

DEPARTMENT OF COMPUTING

FACULTY ART, COMPUTING AND INDUSTRY CREATIVE


UNIVERSITY PENDIDIKAN SULTAN IDRIS
COURSE

MTS 3033 Object Oriented Programming

LABSHEET
TITLE
MATRIC. NO & NAME
PROGRAM
DATE

:
:
:
:
:

4(20 marks)
Inheritance

Objectives:
By the end of this lab, students should be able to:
1. Define base class and derived class
2. Understand the concept of inheritance and the rules applies
Instructions
1. Each of the following class declarations and/or method definitions has errors. You
have to :
a) Find the errors.
b) Fix the errors.
A)

#include<iostream.h>
class Vehicle{
private:
int wheels_num;
public:
void set_wheels_num(int num);
int get_wheels_num();
void display_wheels_num();
};
void Vehicle::set_wheels_num(int num){
wheels_num=num;
}
int Vehicle::get_wheels_num(){
return wheels_num;
}
void Vehicle::display_wheels_num(){
cout<<"Wheels number is:"<<wheels_num<<endl;
}
class Truck:private Vehicle{
private:
double cargoWeight;
public:
void set_cargoWeight(double cW);
double get_cargoWeight();
void displaycargoWeight();
};
void Truck::set_cargoWeight(double cW){
set_wheels_num(16);
}

DEPARTMENT OF COMPUTING
FACULTY ART, COMPUTING AND INDUSTRY CREATIVE
UNIVERSITY PENDIDIKAN SULTAN IDRIS

double Truck::get_cargoWeight(){
return cargoWeight;
}
void Truck::displaycargoWeight(){
cout<<"Cargo Weight is:"<<cargoWeight<<endl;
}
void main(){
Truck tr;
tr.set_cargoWeight(100.00);
tr.get_cargoWeight();
tr.displaycargoWeight();
tr.set_wheels_num(16);
tr.get_wheel_num();
tr.display_wheels_num();
}

B)

#include<iostream.h>
class Student{
protected:
int matrik_num;
public:
void display_matrik_num();
};
void Student::display_matrik_num(){
cout<<"Matrik Number is:"<<matrik_num<<endl;
}
class LocalStudent:protected Student{
public:
int ic_num;
public:
void display_ic_num();
};
void LocalStudent::display_ic_num(){
cout<<"Ic Number is: "<<ic_num<<endl;
}
void main(){
LocalStudent LC;
LC.ic_num=8888;
LC.display_ic_num();
LC.matrik_num=686;
LC.display_matrik_num();
}

DEPARTMENT OF COMPUTING
FACULTY ART, COMPUTING AND INDUSTRY CREATIVE
UNIVERSITY PENDIDIKAN SULTAN IDRIS
C)

#include<iostream.h>
class Student{
private:
int matric;
protected:
int phne;
public:
char *name;
public:
void set(int m);
void displayStudent();
};
void Student::set(int m){
matric=m;
}
void Student::displayStudent(){
cout<<matric<<endl;
}
class LocalStudent:public Student{
private:
int ic_num;
public:
void set_ic(int i);
int get_ic();
void display_localStudent();
};
void LocalStudent::set_ic(int i){
ic_num=i;
}
int LocalStudent::get_ic(){
return ic_num;
}
void LocalStudent::display_localStudent(){
cout<<ic_num<<endl;
cout<<name<<endl;
cout<<phne<<endl;
set(345);
displayStudent();
}
void main(){
LocalStudent LS;
LS.phne=123;
LS.name="SALMIAH";
LS.set_ic(899);
LS.get_ic();
LS.display_localStudent();
}

DEPARTMENT OF COMPUTING
FACULTY ART, COMPUTING AND INDUSTRY CREATIVE
UNIVERSITY PENDIDIKAN SULTAN IDRIS
D)

E)

class SnowMobile:Vehicle{
protected:
int horsepower;
double wight;
public:
SnowMobile();
~SnowMobile();
};

class SnowMobile:public
Vehicle{
protected:
int horsepower;
double wight;
public:
SnowMobile();
~SnowMobile();
};

cass Table:public Furniture{


protected:
int numSeats;
public:
Table()
~Table();
};

class Table:public Furniture{


protected:
int numSeats;
public:
Table()
~Table();
};

2. A sub class inherits the function and variable of super class.


3. The super class named in the following line of code is dog.
class Pet:public Dog
4. In the following line of code, the class access specification for the super class is
public.
class Pet:public Dog
5. In the following line of code, the class access specification for the super class is
protected.
class Pet:Fish
6. Draw a diagram class following the the class declaration
i.
class ColorBox:public Box
ii.
class Fish:Pet
iii.
class PrivateFirm:public Firm
iv.
class Tutor:public Lecturer, public Staff

DEPARTMENT OF COMPUTING
FACULTY ART, COMPUTING AND INDUSTRY CREATIVE
UNIVERSITY PENDIDIKAN SULTAN IDRIS

You might also like