Anda di halaman 1dari 11

LAB 8: Access Control, Overriding &

Abstract Class
Learning Outcomes

By the end of this lab, students should be able to:


1. Create objects that can access members of base class by using different access
control such a public and protected.
2. Implement the overriding and abstract class in the program.

Theory/ Terminologies

• Access control limits the most accessible level for the members inherited from the base
class.
• When a class inherits from another one, the members of the derived class can access the
protected members inherited from the base class, but not its private members.
• We can summarize the different access types according to who can access them in the
following way:
Access public protected private
members of the same class yes yes yes
members of derived
yes yes no
classes
not members yes no no
• When an object receives a message, it checks its own methods first before consulting its
superclass. This means that if the object's class and its superclass both contain a method
with the same name for a message, the object's method takes precedence. This is known as
overriding, because it gives a class an easy way to intercept messages before they get to its
superclass.

Activity 8A

1
The following program will demonstrate how the access specifier is used to secure data in a
class.

Procedure :

Step 1: Open Visual Studio C++ and type the following C++ code

#include <iostream>
using namespace std;

class CBox
{
private:

double m_Length;
double m_Width;
double m_Height;

protected:

void SetLength(double & i_length)


{
m_Length = i_length;
}

void SetWidth(double & i_width)


{
m_Width = i_width;
}

void SetHeight(double & i_height)


{
m_Height = i_height;
}

double GetLength()
{
return m_Length;
}

double GetWidth()
{
return m_Width;
}

double GetHeight()
{
return m_Height;
}

public:

2
CBox() //constructor
{};
};

class CCarton: public CBox


{
public:
double m_MaxWeight;

public:
CCarton(double & max) // Constructor
{
m_MaxWeight = max;
cout << "Maximum weight: " << m_MaxWeight << endl;
}

~CCarton() // Destructor
{};

void SetProperty()
{
float length, width, height;
cout << "Enter carton's length: ";
cin >> length;
cout << "Enter carton's width: ";
cin >> width;
cout << "Enter carton's height: ";
cin >> height;

SetHeight(height);
SetLength(length);
SetWidth(width);
}

double Volume() const


{
return GetLength()*GetWidth()*GetHeight();
}

};

void main()
{
float weight = 30;
CCarton my_carton(weight);
my_carton.SetProperty();
cout << "Carton volume is : " << my_carton.Volume() << endl;
}

Step 2: Compile the program.

Step 3: Run the program and write down the output.

3
Note:
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Activity 8B
The following program demonstrating the overriding was implemented in the constructor of a
class.

Procedure :
Step 1: Open Visual Studio C++ and type the following C++ code

// the following program demonstrate how the constructor of a base class


is // always called first whenever an object of derived class is created.

#include <iostream>
using namespace std;

class CBox
{
public:
// Base class constructor
CBox(double lv = 1.0, double wv = 1.0,
double hv = 1.0):

m_Length(lv), m_Width(wv), m_Height(hv)


{
cout << "CBox constructor called" <<
endl << endl;
}
};

class CCandyBox: public CBox


{
public:
char* m_Contents;

public:
// Constructor to set contents calls default CBox constructor
// automatically

CCandyBox(char* str = "Candy")


{

4
cout << "CCandyBox constructor 1 called"
<< endl << endl;
m_Contents = new char[ strlen(str) + 1 ];
strcpy_s(m_Contents, strlen(str) + 1,
str);
}

// Constructor to set dimensions and contents with explicit call of CBox


// constructor

CCandyBox(double lv, double wv, double hv,


char* str = "Candy"):CBox(lv, wv, hv)
{
cout << "CCandyBox constructor 2 called"
<< endl << endl;
m_Contents = new char[ strlen(str) + 1 ];
strcpy_s(m_Contents, strlen(str) + 1,
str);
}

~CCandyBox() // Destructor
{
delete[] m_Contents;
}
};

void main()
{
CBox myBox(4.0, 3.0, 2.0);
CCandyBox myCandyBox;
CCandyBox myMintBox(1.0, 2.0, 3.0, "Wafer Thin Mints");
cout << endl;
}

Step 2: Compile and run the program. What is the output?

Note:
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Activity 8C

5
The following program demonstrating overriding of access control and overloading of a function
in a program.

Procedure :

Step 1: Open Visual Studio C++ and type the following C++ code

#include <iostream>
#include <string>
using namespace std;

class CollegeCourse
{
protected:
string department_name;
string course_id;
int credit_hour;
float tuition_fee;

void ShowProperties();

public:
CollegeCourse(){};
void SetProperties(string i_department_name, string i_course_id,
int i_credit_hour, float i_tuition_fee);
};

void CollegeCourse::SetProperties(string i_department_name, string


i_course_id, int i_credit_hour,
float i_tuition_fee)
{
department_name = i_department_name;
course_id = i_course_id;
credit_hour = i_credit_hour;
tuition_fee = i_tuition_fee;
}

void CollegeCourse::ShowProperties()
{
cout << "Department: " << department_name << endl;
cout << "Course: " << course_id << endl;
cout << "Credit Hour: " << credit_hour << endl;
cout << "Tuition Fee: " << tuition_fee << endl;
}

class LabCourse: public CollegeCourse


{
private:
float lab_fee;

public:
LabCourse(){};

6
// overloading the function with the same name in the base class
void SetProperties(string i_department_name, string i_course_id,
int i_credit_hour, float i_tuition_fee,
float i_lab_fee);

// overiding the protected access control


CollegeCourse::ShowProperties;
};

void LabCourse::SetProperties(string i_department_name, string i_course_id,


int i_credit_hour, float i_tuition_fee,
float i_lab_fee)
{
//overriding a function with similar name but in the base class
CollegeCourse::SetProperties(i_department_name, i_course_id,
i_credit_hour, i_tuition_fee);
lab_fee = i_lab_fee;
}

void main()
{
CollegeCourse my_college;
my_college.SetProperties("JTMK", "F3031", 4, 200);

LabCourse my_lab;
my_lab.SetProperties("JTMK", "F3031", 4, 200, 100);
my_lab.ShowProperties();
}

Step 2: Compile and run the program. What is the output?

Step 3: Try to comment this line : my_lab.ShowProperties(); . What is the output?

Note:
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Activity 8D

7
The following program demonstrating overriding of access control and overloading of a function
in a program.

Procedure :

Step 1: Open Visual Studio C++ and type the following C++ code

#include <iostream>
using namespace std;

// abstract class
class IColorable
{
public:
// pure virtual function
virtual void BoxColor() = 0;
~IColorable(){};
};

class CBox
{
private:

double m_Length;
double m_Width;
double m_Height;

public:

// initialization constructor
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0);

// pure virtual function


virtual void ShowName() = 0;
};

CBox::CBox(double lv, double wv, double hv):


m_Length(lv), m_Width(wv), m_Height(hv)
{
cout << "You just created a box with the following dimension: " << endl;
cout << "Length: " << m_Length << endl;
cout << "Width: " << m_Width << endl;
cout << "Heigth: " << m_Height << endl;
}

class CCandyBox: public CBox, public IColorable


{

public:

CCandyBox() // Constructor
{}

void ShowName()

8
{
cout << "Your box is a Candy Box" <<
endl;
}

void BoxColor()
{
cout << "The box color is red." << endl
<< endl;
}

~CCandyBox() // Destructor
{};
};

void main()
{
CCandyBox candy_box;
candy_box.ShowName();
candy_box.BoxColor();
}

Step 2: Compile and run the program. What is the output?

Step 3: Try to add this line: CBox my_box; after candy_box.BoxColor();.Compile and run
the program. What did you see and why it’s happening?

Note:
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

Lab Exercise :

9
Complete the following code:

#include <iostream>
using namespace std;

____(a)____ IGetArea
{
public:
~IGetArea(){};

virtual void _______(b)_________ = 0;


};

class Rectangle
{
private:
int _____(c)______;
int width;

______(d)______:
Rectangle();
void SetLength(int & i_length);
____(e)___ SetWidth(int & i_width);
virtual void GetArea();
};

Rectangle::Rectangle()
{}

void Rectangle::SetLength(________(f)_____)
{
length = i_length;
}

void Rectangle::SetWidth(int & i_width)


{
width = i_width;
}

void Rectangle::GetArea()
{
int area;
_____(g)_____ = length * width;

cout << "The area for Rectangle is: " <<


area << endl;
}

void main()
{

10
int l, w;

cout << "Enter length: ";


______(h)_____ >> l;
cout << "Enter width: ";
cin >> w;

Rectangle my_rectangle;
______(j)___.SetLength(l);
my_rectangle.SetWidth(____(j)_____);
my_rectangle.GetArea();
}

10 Marks

Total : 10 Marks

CONCLUSION:
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________

11

Anda mungkin juga menyukai