Friend function:
If we want to declare an external function as friend of a class, thus
allowing this function to have access to the private and protected
members of this class, we do it by declaring a prototype of this
external function within the class, and preceding it with the keyword friend:
It simply has access to its private and protected members without being a member.
- #include <iostream>
-
using namespace std;
-
-
class CRectangle {
-
int width, height;
-
public:
-
void set_values (int, int);
-
int area () {return (width * height);}
-
friend CRectangle duplate (CRectangle);
-
};
-
-
void
-
CRectangle::set_values (int a, int b)
-
{
-
width = a;
-
height = b;
-
}
-
-
CRectangle
-
duplate(CRectangle rectparam)
-
{
-
CRectangle rectres;
-
rectres.width = rectparam.width;
-
rectres.height = rectparam.height;
-
-
return (rectres);
-
}
-
-
int
-
main(void)
-
{
-
CRectangle rect, rectb;
-
rect.set_values(2,3);
-
rectb = duplate(rect);
-
cout << rectb.area() << endl;
-
-
return (0);
-
}
Friend class:
- #include <iostream>
-
using namespace std;
-
-
class CSquare;
-
-
class CRectangle {
-
int width, height;
-
public:
-
int area () {return (width * height);}
-
void convert (CSquare a);
-
};
-
-
class CSquare {
-
private:
-
int side;
-
public:
-
void set_side (int a) {side = a;}
-
friend class CRectangle;
-
};
-
-
void
-
CRectangle::convert(CSquare a)
-
{
-
width = a.side;
-
height = a.side;
-
}
-
-
int
-
main(void)
-
{
-
CSquare sqr;
-
CRectangle rect;
-
sqr.set_side(4);
-
rect.convert(sqr);
-
cout << rect.area() << endl;
-
-
return (0);
-
}
Consider that friendships are not corresponded if we do not explicitly specify so.
Another property of friendships is that they are
not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.
Inheritance:
Format:
- class derived_class_name: public base_class_name
-
{ /*...*/ };
- #include <iostream>
-
using namespace std;
-
-
class CPolyon {
-
protected:
-
int width, height;
-
public:
-
void set_values (int a, int b) {
-
width = a;
-
height = b;
-
}
-
};
-
-
class CRectangle: public CPolyon {
-
public:
-
int area() {
-
return (width * height);
-
}
-
};
-
-
class CTriangle: public CPolyon {
-
public:
-
int area() {
-
return (width * height) / 2;
-
}
-
};
-
-
int
-
main(void)
-
{
-
CRectangle rect;
-
CTriangle trgl;
-
rect.set_values(4,5);
-
trgl.set_values(4,5);
-
-
cout << rect.area() << endl;
-
cout << trgl.area() << endl;
-
-
return (0);
-
}
In principle, a derived class inherits every member of a base class except:
- its constructor and its destructor
- its operator=() members
- its friends
- #include <iostream>
-
using namespace std;
-
-
class mother {
-
public:
-
mother() {
-
cout << "mother: no parameters\n";
-
}
-
mother(int a) {
-
cout << "mother: int parameters\n";
-
}
-
};
-
-
class daughter: public mother {
-
public:
-
daughter(int a) {
-
cout << "daughter: int parameters\n\n";
-
}
-
};
-
-
class son: public mother {
-
public:
-
son(int a) : mother(a) {
-
cout << "son: int parameters\n\n";
-
}
-
};
-
-
int
-
main(void)
-
{
-
daughter cynthia (0);
-
son daniel(0);
-
-
return (0);
-
}
Multiple inheritance:
- #include <iostream>
-
using namespace std;
-
-
class CPolygon {
-
protected:
-
int width, height;
-
public:
-
void set_values (int a, int b) {
-
width = a;
-
height = b;
-
}
-
};
-
-
class COutput {
-
public:
-
void output(int i);
-
};
-
-
void
-
COutput::output(int i)
-
{
-
cout << i << endl;
-
}
-
-
class CRectangle: public CPolygon, public COutput {
-
public:
-
int area() {
-
return (width * height);
-
}
-
};
-
-
class CTriangle: public CPolygon, public COutput {
-
public:
-
int area() {
-
return (width * height) / 2;
-
}
-
};
-
-
int
-
main(void)
-
{
-
CRectangle rect;
-
CTriangle trgl;
-
rect.set_values(4,5);
-
trgl.set_values(4,5);
-
rect.output(rect.area());
-
trgl.output(trgl.area());
-
-
return (0);
-
}
阅读(954) | 评论(0) | 转发(0) |