Beruflich Dokumente
Kultur Dokumente
Abstract Class
Learning Outcomes
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:
double GetLength()
{
return m_Length;
}
double GetWidth()
{
return m_Width;
}
double GetHeight()
{
return m_Height;
}
public:
2
CBox() //constructor
{};
};
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);
}
};
void main()
{
float weight = 30;
CCarton my_carton(weight);
my_carton.SetProperty();
cout << "Carton volume is : " << my_carton.Volume() << endl;
}
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
#include <iostream>
using namespace std;
class CBox
{
public:
// Base class constructor
CBox(double lv = 1.0, double wv = 1.0,
double hv = 1.0):
public:
// Constructor to set contents calls default CBox constructor
// automatically
4
cout << "CCandyBox constructor 1 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;
}
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::ShowProperties()
{
cout << "Department: " << department_name << endl;
cout << "Course: " << course_id << endl;
cout << "Credit Hour: " << credit_hour << endl;
cout << "Tuition Fee: " << tuition_fee << endl;
}
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);
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();
}
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);
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 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(){};
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::GetArea()
{
int area;
_____(g)_____ = length * width;
void main()
{
10
int l, w;
Rectangle my_rectangle;
______(j)___.SetLength(l);
my_rectangle.SetWidth(____(j)_____);
my_rectangle.GetArea();
}
10 Marks
Total : 10 Marks
CONCLUSION:
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
11