Lab 10
Lab 10
class B
{
private:
int i;
protected:
float f;
public:
B() { i =0; f = 0.0; d =0.0; }
double d;
void g1(B b){f = b.f;}
};
class X: public B
{
protected:
short s;
public:
X() {s=0;}
void g2(X x) {f = x.f;}
void g3(B b) {f = b.f;} //comment
};
int main()
{
B b1;
X x1;
x1.g1(b1);
return 0;
}
1/7
National Chiao Tung University Laboratory Manual 10
Department of Electrical and Computer Engineering May 14, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
If B is a private base, its public and protected members become private members of
derived class.
If B is a protected base, its public and protected members become protected
members of derived class.
If B is a public base, its public members become members of derived class and its
protected members become protected members of derived class.
The access control for protected member, similar to private member, is that only its
member and friend can access it. However, the protected member can become private,
protected or public members of derived class but private member cannot. Therefore,
protected members of a class are designed for use by derived classes and are not intended
for general use.
class Point2D
{
private:
int x;
int y;
public:
Point2D(){x = 0;y=0;}
void display() const;
// …
};
class Circle
{
private:
Point2D center;
double radius;
public:
void draw();
//…
};
class Triangle
{
2/7
National Chiao Tung University Laboratory Manual 10
Department of Electrical and Computer Engineering May 14, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
private:
Point2D *vertices;
public:
// …
~Triangle(){delete [] vertices;}
void draw();
};
int main()
{
Point2D p;
Point2D *vec = new Point2D [3];
Circle_in_Triangle ct(p,0,vec);
ct.draw();
return 0;
}
int main()
{
Point2D p;
Point2D *vec = new Point2D [3];
Circle_in_Triangle ct(p,0,vec);
ct.draw();
3/7
National Chiao Tung University Laboratory Manual 10
Department of Electrical and Computer Engineering May 14, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
The compiler shows the error message “request for member `area' is ambiguous” on
screen.
A using-declaration can bring different functions from base classes to a derived class and
then overload resolution can be applied. You can add “using Triangle::area;” in
Circle_in_Triangle and compiler the program again.
class Shape
{
protected:
int color;
};
class Circle: public Shape
{
// definition in lab10-3
}
class Triangle: public Shape
{
// definition in lab10-3
}
class Circle_in_Triangle: public Circle, public Triangle
{
public:
// …
void draw()
{
cout << "Circle's color: " << Circle::color << endl;
cout << "Triangle's color: " << Triangle::color << endl;
Circle::draw();
Triangle::draw();
}
4/7
National Chiao Tung University Laboratory Manual 10
Department of Electrical and Computer Engineering May 14, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
};
class Shape
{
protected:
int color;
};
class Circle: public virtual Shape
{
// definition in lab10-3
}
class Triangle: public virtual Shape
{
// definition in lab10-3
}
class Circle_in_Triangle: public Circle, public Triangle
{
public:
// …
void draw()
{
cout << "Circle's color: " << Circle::color << endl;
cout << "Triangle's color: " << Triangle::color << endl;
Circle::draw();
Triangle::draw();
}
};
5/7
National Chiao Tung University Laboratory Manual 10
Department of Electrical and Computer Engineering May 14, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
In the input file “performance.txt”, the first line shows the total number of students in the
school. Each row indicates a record for one student. As shown in the sample file, the first and
second columns denote the student’s ID and name, respectively. The later five numbers are the
final scores for different subjects and the last five 1/0s indicate win/loss on different sports
games. If a student is a winner in one sport game, he/she can obtain extra five points on his/her
evaluation report. For example, Tom’s average score on five subjects is 70 and he won three
games to obtain extra 15 points. Therefore, Tom’s final score is 85. The full score is 100.
6/7
National Chiao Tung University Laboratory Manual 10
Department of Electrical and Computer Engineering May 14, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
5
991001 Tom 50 60 80 90 70 1 0 0 1 1
991002 Jean 100 90 80 70 60 0 0 0 1 1
991003 Kevin 60 90 100 80 90 0 1 1 0 0
991004 John 100 80 90 70 80 0 0 0 0 1
991005 Marry 50 60 70 90 60 1 1 0 0 0
return 0;
}
7/7